有关BroadcastReceiver的误导性文档?

时间:2013-10-02 09:13:23

标签: android android-intent

有没有人知道这行在Android文档中的意思(enter link description here):“

  

注意:如果在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

2 个答案:

答案 0 :(得分:2)

这只是意味着如果您在BroadcastReceiver中取消注册了onPause(),就不会收到广播。

答案 1 :(得分:0)

根据您在接收器中的操作,可能会消耗CPU /电池电量。如果是,你希望它尽可能少地执行,这就是这句话想说的。

您是否曾经运行过接收GPS信息的代码?它让您的电池最多可以使用2小时。因此,每当开启时,每一秒都很重要。 onResumeonPause是打开/关闭此功能的最佳位置。使用onStartonStop甚至onCreateonDestroy意味着,该功能会在必要时打开(并消耗电池)。