广播接收器注册一共有两种形式 : 静态注册和动态注册. 两者及其接收广播的区别: 1.动态注册的广播 永远要快于 静态注册的广播,不管静态注册的优先级设置的多高,不管动态注册的
广播接收器注册一共有两种形式 : 静态注册和动态注册.
两者及其接收广播的区别:
1.动态注册的广播 永远要快于 静态注册的广播,不管静态注册的优先级设置的多高,不管动态注册的优先级有多低。
2.动态注册广播不是 常驻型广播 ,也就是说广播跟随activity的生命周期。注意: 在activity结束前,移除广播接收器。
静态注册是常驻型 ,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。
3.在同一个优先级下,谁先启动的快,谁将先接收到广播。
下面这个Demo,界面如下,注册两个动态接收器(接收自定义广播和系统广播)和注册静态广播(接收自定义静态和广播、手机USB的插拔事件、接收短信并读取短信息)
工程目录如下:注意看代码中的注释
xml布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/send_static"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送自定义静态注册广播"/>
<Button android:id="@+id/send_dynamic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送自定义动态注册广播"/>
<Button
android:id="@+id/send_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送系统动态注册广播"/>
</LinearLayout>
Manifest.xml如下,这里面定义的权限信息和注册的广播
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nii.receiver" >
<!--监听短信息-->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<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>
<!-- 注册自定义静态广播接收器 -->
<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="com.byread.static" />
</intent-filter>
</receiver>
<!-- 注册系统静态广播接收器 -->
<receiver android:name=".SystemReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.hardware.usb.action.USB_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.Java
package com.nii.receiver;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.nii.receiver.type.RequestPermissionType;
import com.nii.receiver.util.PermissionUtil;
public class MainActivity extends AppCompatActivity
{
private Button sendStaticBtn;
private Button sendDynamicBtn;
private Button sendSystemBtn;
private static final String STATICACTION = "com.byread.static";
private static final String DYNAMICACTION = "com.byread.dynamic";
private static final String SYSTEMacTION = Intent.ACTION_POWER_CONNECTED;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mContext = this;
sendStaticBtn = (Button) findViewById(R.id.send_static);
sendDynamicBtn = (Button) findViewById(R.id.send_dynamic);
sendSystemBtn = (Button) findViewById(R.id.send_system);
sendStaticBtn.setOnClickListener(new MyOnClickListener());
sendDynamicBtn.setOnClickListener(new MyOnClickListener());
sendSystemBtn.setOnClickListener(new MyOnClickListener());
//申请读取短信的权限
PermissionUtil.requestPermission(mContext, Manifest.permission.READ_SMS,
RequestPermissionType.REQUEST_CODE_ASK_READ_SMS);
PermissionUtil.requestPermission(mContext, Manifest.permission.RECEIVE_SMS,
RequestPermissionType.REQUEST_CODE_ASK_RECEIVE_SMS);
}
class MyOnClickListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
// 发送自定义静态注册广播消息
if (v.getId() == R.id.send_static)
{
Log.e("MainActivity", "发送自定义静态注册广播消息");
Intent intent = new Intent();
intent.setAction(STATICACTION);
intent.putExtra("msg", "接收静态注册广播成功!");
sendBroadcast(intent);
}
// 发送自定义动态注册广播消息
else if (v.getId() == R.id.send_dynamic)
{
Log.e("MainActivity", "发送自定义动态注册广播消息");
Intent intent = new Intent();
intent.setAction(DYNAMICACTION);
intent.putExtra("msg", "接收动态注册广播成功!");
sendBroadcast(intent);
}
// 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。
else if (v.getId() == R.id.send_system)
{
Log.e("MainActivity", "发送系统动态注册广播消息");
Intent intent = new Intent();
intent.setAction(SYSTEMACTION);
intent.putExtra("msg", "正在充电。。。。");
}
}
}
@Override
protected void onStart()
{
super.onStart();
Log.e("MainActivity", "注册广播事件");
// 注册自定义动态广播消息
IntentFilter filter_dynamic = new IntentFilter();
filter_dynamic.addAction(DYNAMICACTION);
reGISterReceiver(dynamicReceiver, filter_dynamic);
// 注册系统动态广播消息
IntentFilter filter_system = new IntentFilter();
filter_system.addAction(SYSTEMACTION);
registerReceiver(systemReceiver, filter_system);
}
private BroadcastReceiver dynamicReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.e("MainActivity", "接收自定义动态注册广播消息");
if (intent.getAction().equals(DYNAMICACTION))
{
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
private BroadcastReceiver systemReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.e("MainActivity", "接收系统动态注册广播消息");
if (intent.getAction().equals(SYSTEMACTION))
{
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
}
StaticReceiver.java
package com.nii.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;
public class StaticReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
SystemReceiver.java这里面处理一些系统的事件
package com.nii.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.text.fORMat.DateFormat;
import android.util.Log;
import android.widget.Toast;
import com.nii.receiver.util.DialogUtil;
import java.util.Date;
public class SystemReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
{
Log.e("SystemReceiver", "电量低提示");
Toast.makeText(context, "您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show();
}
else if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{//如果数据不为空
//获得收到的短信数据
Object[] objarray = (Object[]) bundle.get("pdus");
//根据objArray的大小创建一个SmsMessage数组,用于封装短信内容
SmsMessage[] smsMessage = new SmsMessage[objArray.length];
StringBuffer sb = new StringBuffer();
sb.append("时间:" + new DateFormat().format("yyyy - MM - dd hh.mm.ss", new Date()));
//遍历smsMessage数组取出所有短信
for (int i = 0; i < smsMessage.length; i++)
{
//将每条字节类型的短信数据转换成SmsMessage对象
smsMessage[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);
//获取短信发送地址
sb.append("发送者:" + smsMessage[i].getOriginatingAddress());
//获取短信内容
sb.append("短信内容:" + smsMessage[i].getDisplayMessageBody() + "\n");
}
DialogUtil.showPrompt(context,sb.toString());
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
else if(intent.getAction().equals("android.hardware.usb.action.USB_STATE"))
{
if (intent.getExtras().getBoolean("connected"))
{
Toast.makeText(context, "USB插入", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, "USB拔出", Toast.LENGTH_LONG).show();
}
}
}
}
RequestPermissionType.java是权限请求的时候,Qequest Code(请求码)
package com.nii.receiver.type;
public interface RequestPermissionType
{
int REQUEST_CODE_ASK_CALL_PHONE = 100;
int REQUEST_CODE_ASK_RECEIVE_SMS = 101;
int REQUEST_CODE_ASK_READ_SMS = 102;
}
DialogUtil.java是界面弹窗工具类
package com.nii.receiver.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
public class DialogUtil
{
public static AlertDialog showAlert(Context context, String title, String message,
String confirmButton, DialogInterface.OnClickListener confirmButtonClickListener,
String centerButton, DialogInterface.OnClickListener centerButtonClickListener,
String cancelButton, DialogInterface.OnClickListener cancelButtonClickListener,
DialogInterface.OnShowListener onShowListener, boolean cancelable,
DialogInterface.OnCancelListener onCancelListener,
DialogInterface.OnDismissListener onDismissListener)
{
AlertDialog.Builder promptBuilder = new AlertDialog.Builder(context);
if (title != null)
{
promptBuilder.setTitle(title);
}
if (message != null)
{
promptBuilder.setMessage(message);
}
if (confirmButton != null)
{
promptBuilder.setPositiveButton(confirmButton, confirmButtonClickListener);
}
if (centerButton != null)
{
promptBuilder.setNeutralButton(centerButton, centerButtonClickListener);
}
if (cancelButton != null)
{
promptBuilder.setNegativeButton(cancelButton, cancelButtonClickListener);
}
promptBuilder.setCancelable(true);
if (cancelable)
{
promptBuilder.setOnCancelListener(onCancelListener);
}
AlertDialog alertDialog = promptBuilder.create();
if (!(context instanceof Activity))
{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.setOnDismissListener(onDismissListener);
alertDialog.setOnShowListener(onShowListener);
alertDialog.show();
return alertDialog;
}
public static AlertDialog showConfirm(Context context, String title, String message,
DialogInterface.OnClickListener confirmButtonClickListener,
DialogInterface.OnClickListener cancelButtonClickListener)
{
return showAlert(context, title, message, "Yes",
confirmButtonClickListener, null, null, "No", cancelButtonClickListener, null, true, null, null);
}
public static AlertDialog showPrompt(Context context, String message, String confirmButton)
{
return showAlert(context, null, message, confirmButton, null, null, null, null, null, null, true, null, null);
}
public static AlertDialog showPrompt(Context context, String message)
{
return showAlert(context, null, message, "OK", null, null, null, null, null, null, true, null, null);
}
}
PermissionUtil.java是权限申请工具类
package com.nii.receiver.util;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
public abstract class PermissionUtil
{
public static void requestPermission(Context mContext,String permission,int requestCode)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
return;
}
int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext,permission);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions((Activity) mContext, new String[]{permission},requestCode);
}
}
}
您可能感兴趣的文章:Android中BroadcastReceiver(异步接收广播Intent)的使用Android 监听apk安装替换卸载广播的实现代码Android中的广播和广播接收器代码实例android连接wifi时获取广播地址代码Android 开机广播的使用及配置Android广播接收机制详细介绍(附短信接收实现)Android基于广播事件机制实现简单定时提醒功能代码Android编程四大组件之BroadcastReceiver(广播接收者)用法实例Android中的广播(BroadCast)详细介绍Android实现静态广播监听器的方法
--结束END--
本文标题: Android的广播Receiver动态注册和静态注册示例
本文链接: https://lsjlt.com/news/22090.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0