2012-06-22 95 views
1

我登記我的reciver中的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> 

我沒有收到我的通知。如果我這樣做是靜態的,然後它工作

+0

這是一個非常好的問題,因爲別人稍後會遇到這種情況。 :)動態註冊廣播接收機..:D – t0mm13b

+0

@ t0mm13b動態註冊廣播接收機很容易,維基做的是正確的。這個問題是我昨天在回答中指出的:您需要暫停活動(取消註冊接收器)以打開/關閉飛行模式。因此你錯過了播放。這是*不應該被動態註冊的接收器的一個例子。 –

回答

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 ha ndler:

@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"/> 
+0

不工作兄弟崩潰 –

+0

您是否嘗試創建一個包含廣播接收器的類,而不是在運行時動態註冊的類? – t0mm13b

+0

雅我創建它的工作,但我需要這種方式動態的方式可以幫助你! –

0

一切看起來比的事實,你不能打開飛行模式或關閉不停頓的活動,其註銷接收機對我好等。當你回到你的活動,恢復和重新註冊,你錯過了廣播。

我不知道您的最終目標是什麼,但是如果您希望您的應用程序接收飛行模式意圖,最好的辦法是在清單中註冊。 (從技術上講,你可以動態地做到這一點,但這需要在比onResume()/onPause()更持久的地方進行註冊/取消註冊,我不認爲這真的是你想要的。)

+0

嗨Darshan我喜歡它staic,但我希望它動態地做到這一點,你可以告訴我我該怎麼做 –

+0

你可以在'onCreate()'中註冊,並在'onDestroy()'中註銷,除非你的Activity可以在任何時候被銷燬,否則它將停止工作。這是一個壞主意,你不應該這樣做。如果您希望系統實際實例化您的BroadcastReceiver而不考慮您的Activity是否正在運行(您幾乎可以肯定這麼做),那麼您應該在您的Manifest中聲明您的接收器。 –

+0

如果我regsiter它onCreate()和取消註冊onDestory(),但在onDestroy()方法我不能呼籲super.onPause() –