一、工程整体图
二、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerReceiver"
/> <Button
android:id="@+id/btn_unregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unregisterReceiver"
/> <Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
/></LinearLayout>
三、AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jltxgcy.broadcastreceiverdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.jltxgcy.receiver" />
</intent-filter>
</receiver>
<service
android:name=".HelloIntentService">
</service>
</application></manifest>
四、MainActivity.java
package com.jltxgcy.broadcastreceiverdemo;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;public class MainActivity extends Activity {
public static final String ACTION="com.jltxgcy.receiver";
public BroadcastReceiver mBroadcastReceiver= new MyReceiver(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btn_register).setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
/*IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, filter);*/
}
}); findViewById(R.id.btn_unregister).setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
//unregisterReceiver(mBroadcastReceiver);}
}); findViewById(R.id.btn_send).setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
sendBroadcast(new Intent(ACTION));
}
});
}@Override
protected void onStop() {
//unregisterReceiver(mBroadcastReceiver);
super.onStop();
}}
五、MyReceiver.java
package com.jltxgcy.broadcastreceiverdemo;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;public class MyReceiver extends BroadcastReceiver {@Override
public void onReceive(Context context, Intent arg1) {
Log.d("jltxgcy", "onReceiver");
Intent intent = new Intent(context, HelloIntentService.class);
context.startService(intent);
}}
六、HelloIntentService.java
package com.jltxgcy.broadcastreceiverdemo;import android.app.IntentService;
import android.content.Intent;
import android.util.Log;public class HelloIntentService extends IntentService {
public static final String TAG="jltxgcy";public HelloIntentService() {
super("HelloIntentService");
// TODO Auto-generated constructor stub
}@Override
protected void onHandleIntent(Intent intent) {
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
Log.d(TAG, "onHandleIntent"+Thread.currentThread().getId());
}@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}}
七、详解
动态注册,不需要在AndroidManifest.xml中写上receiver。Activity退出后一定要关闭掉动态注册的receiver,否则会报告异常。
静态注册,需要在AndroidManifest.xml中写上receiver。即使Activity退出后,也在接受消息。
发送广播有两类,一个是系统发送的广播,一个是自己定义发送的广播。
在onReceiver,如果超过10s,那么就会产生ANR,最好的方法是开启一个IntentService,完成任务后自动关闭IntentService。
如果直接启动一个新的线程,那么无法控制线程的关闭。