Android 无法动态注册广播接收器
我正在OnResume中注册我的收信人,在OnPause中注销是因为我的代码有问题Android 无法动态注册广播接收器,android,broadcastreceiver,onpause,Android,Broadcastreceiver,Onpause,我正在OnResume中注册我的收信人,在OnPause中注销是因为我的代码有问题 package com.bd2; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import androi
package com.bd2;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
public class BroadcastReceiver2Activity extends Activity {
/** Called when the activity is first created. */
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
IntentFilter intentfilter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intentfilter = new IntentFilter();
intentfilter.addAction("android.intent.action.AIRPLANE_MODE");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
/*intfilter = new IntentFilter();
intfilter.addAction("android.intent.action.AIRPLANE_MODE");*/
registerReceiver(receiver, intentfilter);
// sendBroadcast();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(receiver);
}
private BroadcastReceiver receiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.icon,"Time Reset!",System.currentTimeMillis());
PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
notifyDetails.setLatestEventInfo(context, "Time has been Reset", "Click on me to view Contacts", myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Log.i(getClass().getSimpleName(),"Sucessfully Changed Time");
}
};
}
////////清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bd2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadcastReceiver2Activity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我没有收到通知。如果我以静态方式执行此操作,则它会工作编辑3:
定义全局变量:
private IntentFilter mIntentFilter;
从onCreate处理程序:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntentFilter.addAction("android.intent.action.SERVICE_STATE");
mIntentFilter.addAction("android.intent.action.AIRPLANE_MODE");
registerReceiver(receiver, mIntentfilter);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// Be careful here... mIntentFilter might be gc'd! add checking to this!
registerReceiver(receiver, mIntentFilter);
Log.d(TAG, "onResume() - Registered!");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
Log.d(TAG, "onPause() - Unregistered!");
super.onPause();
}
从onResume处理程序:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntentFilter.addAction("android.intent.action.SERVICE_STATE");
mIntentFilter.addAction("android.intent.action.AIRPLANE_MODE");
registerReceiver(receiver, mIntentfilter);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// Be careful here... mIntentFilter might be gc'd! add checking to this!
registerReceiver(receiver, mIntentFilter);
Log.d(TAG, "onResume() - Registered!");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
Log.d(TAG, "onPause() - Unregistered!");
super.onPause();
}
从onPause处理程序:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntentFilter.addAction("android.intent.action.SERVICE_STATE");
mIntentFilter.addAction("android.intent.action.AIRPLANE_MODE");
registerReceiver(receiver, mIntentfilter);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// Be careful here... mIntentFilter might be gc'd! add checking to this!
registerReceiver(receiver, mIntentFilter);
Log.d(TAG, "onResume() - Registered!");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
Log.d(TAG, "onPause() - Unregistered!");
super.onPause();
}
从广播接收器:
private BroadcastReceiver receiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent != null && intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
Log.d(TAG, "receiver/onReceive() - GOT THE INTENT!!!!");
}else{
Log.d(TAG, "receiver/onReceive() - NOPE! NO INTENT!");
}
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.icon,"Time Reset!",System.currentTimeMillis());
PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
notifyDetails.setLatestEventInfo(context, "Time has been Reset", "Click on me to view Contacts", myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Log.i(getClass().getSimpleName(),"Sucessfully Changed Time");
}
};
此时,从清单中删除我前面提到的意图接收器,因为我们正在以编程方式进行此操作。另外,定义权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
在我看来,一切都很好,除了你不能在不暂停活动的情况下打开或关闭飞机模式,这会注销你的接收器。当你回到你的活动中,恢复并重新注册时,你已经错过了广播
我不知道你的最终目标是什么,但如果你想让你的应用程序接收飞行模式意图,最好的办法是在你的清单中注册。从技术上讲,你可以动态地做,但是,这需要在比onResume/onPause更持久的地方进行注册/注销,我认为这不是你真正想要的。嗨,Darshan,我在staic中完成了,但我希望它能够动态执行。你能告诉我我应该做什么吗?你可以在onCreate中注册,在onDestroy中注销,这会很有效,除了你的活动可能在任何时候被破坏,在这一点上它将停止工作。那是个坏主意,你不应该这么做。如果您希望系统实际实例化您的BroadcastReceiver,而不考虑您的活动是否正在运行,您几乎肯定会这样做,那么你应该在你的清单中声明你的接收者。如果我在onCreate中注册它并在onDestroy中注销它,但是在onDestroy方法中我不能调用super呢。onPause@Vicky我已经解释了为什么飞机模式广播确实应该在舱单中声明。我一点也不清楚你到底想在这里完成什么:为什么你要坚持动态地完成它?这是一个家庭作业吗?你有没有尝试过创建一个包含广播接收器的类,而不是在运行时动态注册它…?是的,我创建的它正在工作,但我需要它以这种方式动态的方式可以帮助你!好啊试试这个。在上面的代码中,我注意到您在调用super.onPause后正在注销事件;你能切换一下,看看会发生什么,即先注销,然后再调用super.onPause;在onPause处理程序中?您可以复制运行的整个代码的logcat并将其放入pastebin吗?退出应用程序后,留出大约5秒钟的时间,以确保刷新logcat以获取整个捕获:一件事-您在onCreate中创建了一个意图过滤器,当活动暂停/恢复时,意图过滤器仍然存在吗?它可以被垃圾收集吗?为了不浪费时间,在处理程序中分散每个路径并记录日志,以查看哪里出了问题,并在这样做时检查日志猫!:这是一个非常好的问题,因为毫无疑问其他人稍后会遇到这种情况动态注册广播接收器..:D@t0mm13b动态注册广播接收机很容易,Vicky做得很好。问题是我昨天在回答中指出的:您需要暂停取消注册接收器的活动,以便打开/关闭飞机模式。因此你错过了广播。这是不应动态注册的接收器的示例。