首页 技术 正文
技术 2022年11月19日
0 收藏 995 点赞 4,471 浏览 4020 个字

一、工程整体图

BroadcastReceiver总结

二、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。

如果直接启动一个新的线程,那么无法控制线程的关闭。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779