1

我在 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>

我没有收到通知。如果我静态地这样做,那么它可以工作

4

2 回答 2

0

编辑#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"/>
于 2012-06-22T20:16:30.633 回答
0

一切对我来说都很好,除了你不能在不暂停活动的情况下打开或关闭飞行模式,这会取消注册你的接收器。当您回到您的活动、恢复并重新注册时,您已经错过了广播。

我不知道你的最终目标是什么,但如果你想让你的应用程序接收飞行模式 Intent,最好的办法是在你的清单中注册。(从技术上讲,您可以动态执行此操作,但这需要在比onResume()/更永久的地方进行注册/注销onPause(),我认为这并不是您真正想要的。)

于 2012-06-22T21:42:23.330 回答