2013-10-02 60 views
0

有誰知道這條線應該在Android文檔中的意思是(enter link description here):「有關BroadcastReceiver的誤導性文檔?

注意:如果您的Activity.onResume()實現註冊接收器,您應該註銷它Activity.onPause( )(你在暫停時不會收到意圖,這將減少不必要的系統開銷)

第一句話是清楚的,如果不需要,應該釋放資源。括號內的文本?顯然,後臺的一個應用程序收到廣播意圖,如果它停止的話(至少在Android 4.2上),它不會在播放時得到廣播銷燬。要試的代碼:

public class MyActivity extends Activity { 

    private BroadcastReceiver mBroadcastReceiver; 

    String a = "1234"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mBroadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.d("|BR", "onReceive() - 1" + " intent: " + intent); 
       a = intent.getStringExtra("ASDF"); 
      } 
     }; 
     this.registerReceiver(mBroadcastReceiver, new IntentFilter(MyService.RECEIVE)); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d("|BR", "onResume()" + " a: " + a); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.d("|BR", "onPause()"); 
    } 

    /** Event handler for a button in the main.xml */ 
    public void createService(View view) { 
     Log.d("|BR", "createService()"); 
     Intent intent = new Intent(MyService.DO); 
     this.startService(intent); 
    } 
} 


public class MyService extends Service { 

    public static final String DO = MyService.class.getName() + ".DO"; 
    public static final String RECEIVE = MyService.class.getName() + ".RECEIVE"; 

    public Executor executor; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     executor = Executors.newSingleThreadExecutor(); 
    } 

    @Override 
    public int onStartCommand(final Intent intent, final int flags, final int startId) { 
     String action = intent.getAction(); 

     Log.d("|BR", "onStartCommand() - 1"); 
     if(DO.equals(action)) { 
      executor.execute(new MyRunnable()); 
      Log.d("|BR", "onStartCommand() - 2"); 

     } 
     return START_NOT_STICKY; 
    } 


    @Override 
    public android.os.IBinder onBind(final Intent intent) { 
     return null; 
    } 

    private class MyRunnable implements Runnable { 

     @Override 
     public void run() { 
      Log.d("|BR", "run() - 1"); 

      try { 
       Thread.sleep(1000 * 8); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 


      Log.d("|BR", "run() - 2"); 

      Intent intent = new Intent(RECEIVE); 
      intent.putExtra("ASDF", "QWER"); 
      MyService.this.sendBroadcast(intent); 

      Log.d("|BR", "run() - 3"); 

     } 
    } 
} 

按下按鈕,按回家按鈕,等待8秒鐘。結果:

09:05:48.622 D/|BR: onResume() a: 1234 
09:05:53.297 D/|BR: createService() 
09:05:53.297 D/|BR: onStartCommand() - 1 
09:05:53.307 D/|BR: onStartCommand() - 2 
09:05:53.307 D/|BR: run() - 1 
09:05:54.558 D/|BR: onPause() 
09:06:01.306 D/|BR: run() - 2 
09:06:01.316 D/|BR: onReceive() - 1 intent: Intent { act=com.example.broadcastReceive.MyService.RECEIVE flg=0x10 (has extras) } 
09:06:01.316 D/|BR: run() - 3 
09:06:14.139 D/|BR: onResume() a: QWER 
+0

我不明白這是如何誤導。您的應用暫停,並且您收到廣播,因爲您的接收器未被註銷。 – njzk2

回答

2

它只是意味着,如果你在你的onPause()已經註銷的BroadcastReceiver您將不會收到廣播。

+0

Aw。所以括號內的文本是onPause()的結果,而不是在後臺的結果。這就說得通了。我的世界現在又到位了。謝謝。 (但我認爲這有點誤導)。 –

0

在接收器中做什麼,可能會非常耗費CPU /電量。如果是這樣,你希望它被儘可能少地執行,那這句話想說什麼。

你有沒有跑過代碼,接收GPS信息?它可讓您的電池最多持續2個小時。所以,每一秒鐘都是重要的,當它開啓時。 onResume和是打開/關閉此功能的最佳位置。使用onStartonStop或甚至onCreate,onDestroy意味着該功能打開(並消耗電池)時間更長,然後是必要的。