[英]Not able to register Broadcast receiver Dynamically
我正在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 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);
}
從onResume處理程序中:
@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!");
}
從onPause處理程序中:
@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"/>
對我來說,所有事情看起來都不錯,除了您必須暫停“活動”就無法打開或關閉飛行模式,這會取消接收器的注冊。 當您回到“活動”,繼續並重新注冊時,您已經錯過了廣播。
我不知道您的最終目標是什么,但是如果您希望您的應用接收飛行模式Intent,最好的辦法是在清單中注冊。 (從技術上講,您可以動態地執行此操作,但這需要在比onResume()
/ onPause()
更永久的位置進行注冊/注銷,我認為這並不是您真正想要的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.