BroadcastReceiver广播有两种发送的方式:
1、普通广播---接收方没有先后顺序
2、有序广播---接收方有先后顺序,按照设置的顺序接收广播
BroadcastReceiver广播有两种注册方式:
1、静态注册----在清单文件中注册
2、动态注册----在程序中注册(动态注册的广播使用完一定要注销)
1、静态注册 —-发送普通广播
* 1、创建一个BroadcastReceiver的子类,重写onReceive()方法。
public class MyBroadcast2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "广播2", Toast.LENGTH_SHORT).show();
}
}
* 2、在清单文件中注册广播
意图过滤器的name属性用于广播接收的时候接收指定的广播,和ID类似
<!-- 注册广播 -->
<receiver android:name=".MyBroadcast" >
<intent-filter> <!-- 添加意图过滤器 -->
<action android:name="com.example.class_broadcastreceiver" />
</intent-filter>
</receiver>
* 3、通过sendBroadcast(intent)发送广播
Intent intent = new Intent();
intent.setAction("com.example.class_broadcastreceiver");
sendBroadcast(intent);// 发送广播
2、静态注册 —-发送有序广播
步骤和发送普通广播一样,不同的是在清单文件中注册广播时添加一条属性:android:priority=”11”设置的值越大先接收到广播的优先级就越高。有序广播高优先级的接收方可以通过在onReceive()中调用abortBroadcast()方法停止广播的继续传播,在某一个接收方中调用该方法后,低优先级的接收方就不能接收到这条广播。
<receiver android:name=".MyBroadcast2" >
<intent-filter android:priority="11" > <!-- 数值越大,越先接收广播 -->
<action android:name="abc" />
</intent-filter>
</receiver>
中断广播的传播:
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getStringExtra("name");
System.out.println("---1->>" + name);
abortBroadcast();// 终止当前广播
}
}
3、动态注册 —-监听android电量变化
1、创建BroadcastReceiver的子类并且重写onReceive方法,在onReceive方法中可以进行一些接收到系统广播的逻辑操作,发送通知或者Toast但不建议弹出Dialog。
public class PowerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("sacle", 100);
textView.setText("当前手机电量为: " + (level * 100) / scale + "%");
}
}
}
2、实例化BroadcastReceiver的子类,调用registerReceiver()方法注册广播。
receiver = new PowerBroadcastReceiver();
// 意图过滤器
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// 注册接收
registerReceiver(receiver, filter);
3、注销广播,调用unregisterReceiver()方法注销广播
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);// 注销广播
}
动态注册的广播不再需要在清单文件中进行注册!监听手机电量变化需要在清单文件中申请权限:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
4、静态注册 —-实现App的开机启动
1、清单文件中添加系统启动监听权限
<!-- 监听系统开机广播权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2、清单文件中注册广播
<!-- 注册广播 -->
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
3、创建BroadcastReceiver的子类并且重写onReceive方法,在onReceive方法中启动我们需要开机启动的Activity
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(context, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
电量监控源码:
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private PowerBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) this.findViewById(R.id.textView1);
receiver = new PowerBroadcastReceiver();
// 意图过滤器
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// 注册接收
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);// 注销广播
}
public class PowerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("sacle", 100);
textView.setText("当前手机电量为: " + (level * 100) / scale + "%");
}
}
}
}
<RelativeLayout 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: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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="141dp"
android:text="TextView" />
</RelativeLayout>