[关闭]
@linux1s1s 2016-11-07T19:50:48.000000Z 字数 3840 阅读 2214

Android 生命周期 - BroadcastReceiver

AndroidRefine 2016-11


系列博文:

Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View

BroadcastReceiver的生命周期回调方法只有onReceive(Context context, Intent intent)方法,该方法运行在UI线程中,所以不可以做耗时操作,推荐使用IntentServer或者自定义带有子线程的Server,当然如果功能简单,可以直接启用子线程去完成耗时操作(前提是该app具有至少一个Activity,以防止该进程被Android系统直接回收),实例Demo如下:

测试Code

BCIntentServer.java

  1. public class BCIntentServer extends IntentService {
  2. /**
  3. * Creates an IntentService. Invoked by your subclass's constructor.
  4. */
  5. public BCIntentServer() {
  6. super("BCIntentServer");
  7. }
  8. @Override
  9. public void onStart(Intent intent, int startId) {
  10. super.onStart(intent, startId);
  11. Log.i(MainActivity.TAG, "BCIntentServer onStart()");
  12. }
  13. @Override
  14. protected void onHandleIntent(Intent intent) {
  15. toWork();
  16. }
  17. public static void toWork() {
  18. Log.i(MainActivity.TAG, "Current Thread is: " + Thread.currentThread().getName());
  19. try {
  20. Thread.sleep(11000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }

BCBroadcastReceiver.java

  1. public class BCBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. //方式一,直接在主线程中做耗时操作
  5. // toWorkWithUIThread();
  6. //方式二,直接开启子线程做耗时操作
  7. // toWordWithSubThread();
  8. //方式三,使用IntentServer做耗时操作
  9. toWorkWithServerThread(context);
  10. }
  11. private void toWorkWithUIThread() {
  12. toWork();
  13. }
  14. private void toWordWithSubThread() {
  15. new Thread() {
  16. @Override
  17. public void run() {
  18. toWork();
  19. }
  20. }.start();
  21. }
  22. private void toWorkWithServerThread(Context context) {
  23. context.startService(new Intent(context, BCIntentServer.class));
  24. }
  25. }

MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. public static final String TAG = "mutex";
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. Log.i(TAG, "MainActivity onCreate()");
  8. }
  9. public void sendNow(View view) {
  10. Intent intent = new Intent("fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST");
  11. sendBroadcast(intent);
  12. }
  13. }

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/activity_main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="fresco.mutex.com.lifecycle.MainActivity">
  11. <Button
  12. android:id="@+id/activity_main_jump"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:onClick="jumpNow"
  17. android:text="Jump SecondActivity Now!"
  18. android:textSize="20dp" />
  19. <Button
  20. android:id="@+id/activity_main_send"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/activity_main_jump"
  24. android:layout_centerInParent="true"
  25. android:layout_marginTop="20dp"
  26. android:onClick="sendNow"
  27. android:text="Send BroadCasatReceiver Now!"
  28. android:textSize="20dp" />
  29. </RelativeLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="fresco.mutex.com.lifecycle">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <activity android:name=".SecondActivity">
  17. <intent-filter>
  18. <action android:name="android.intent.action.DELETE" />
  19. </intent-filter>
  20. </activity>
  21. <service android:name=".BCService" />
  22. <service android:name=".BCIntentServer" />
  23. <receiver android:name=".BCBroadcastReceiver">
  24. <intent-filter>
  25. <action android:name="fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST" />
  26. </intent-filter>
  27. </receiver>
  28. </application>
  29. </manifest>

测试结果

方式一

此处输入图片的描述

方式二

此处输入图片的描述
生命周期正常

方式三

此处输入图片的描述
生命周期正常

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注