@linux1s1s
2016-11-07T19:50:48.000000Z
字数 3840
阅读 2191
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如下:
public class BCIntentServer extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public BCIntentServer() {
super("BCIntentServer");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(MainActivity.TAG, "BCIntentServer onStart()");
}
@Override
protected void onHandleIntent(Intent intent) {
toWork();
}
public static void toWork() {
Log.i(MainActivity.TAG, "Current Thread is: " + Thread.currentThread().getName());
try {
Thread.sleep(11000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class BCBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//方式一,直接在主线程中做耗时操作
// toWorkWithUIThread();
//方式二,直接开启子线程做耗时操作
// toWordWithSubThread();
//方式三,使用IntentServer做耗时操作
toWorkWithServerThread(context);
}
private void toWorkWithUIThread() {
toWork();
}
private void toWordWithSubThread() {
new Thread() {
@Override
public void run() {
toWork();
}
}.start();
}
private void toWorkWithServerThread(Context context) {
context.startService(new Intent(context, BCIntentServer.class));
}
}
public class MainActivity extends AppCompatActivity {
public static final String TAG = "mutex";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MainActivity onCreate()");
}
public void sendNow(View view) {
Intent intent = new Intent("fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST");
sendBroadcast(intent);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fresco.mutex.com.lifecycle.MainActivity">
<Button
android:id="@+id/activity_main_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="jumpNow"
android:text="Jump SecondActivity Now!"
android:textSize="20dp" />
<Button
android:id="@+id/activity_main_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/activity_main_jump"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:onClick="sendNow"
android:text="Send BroadCasatReceiver Now!"
android:textSize="20dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fresco.mutex.com.lifecycle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.DELETE" />
</intent-filter>
</activity>
<service android:name=".BCService" />
<service android:name=".BCIntentServer" />
<receiver android:name=".BCBroadcastReceiver">
<intent-filter>
<action android:name="fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST" />
</intent-filter>
</receiver>
</application>
</manifest>
生命周期正常
生命周期正常