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

アクティビティを一時停止しないと機内モードをオンまたはオフにできず、レシーバーの登録が解除されるという事実を除けば、すべてが私には良さそうです。アクティビティに戻り、再開して再登録するまでに、ブロードキャストを見逃しています。

ここでの最終的な目標はわかりませんが、アプリで機内モードのインテントを受け取りたい場合は、マニフェストに登録することをお勧めします。onResume()(技術的には動的に行うことができますが、それには/より永続的な場所で登録/登録解除を行う必要がありますが、onPause()これは本当にあなたが望むものではないと思います.)

于 2012-06-22T21:42:23.330 に答える