There are two ways to send BroadcastReceiver broadcast:
1、普通广播---接收方没有先后顺序
2、有序广播---接收方有先后顺序,按照设置的顺序接收广播
BroadcastReceiver broadcast has two registration methods:
1、静态注册----在清单文件中注册
2、动态注册----在程序中注册(动态注册的广播使用完一定要注销)
1. Static registration -send ordinary broadcast
* 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. Static registration -send ordered broadcast
The procedure is the same as sending an ordinary broadcast, except that an attribute is added when registering the broadcast in the manifest file: the greater the value set in android: priority = ”11”, the higher the priority of receiving the broadcast first. Ordered broadcast high-priority receivers can stop the continued propagation of the broadcast by calling the abortBroadcast () method in onReceive (). After calling this method in a certain receiver, low-priority receivers cannot receive this. broadcast.
<receiver android:name=".MyBroadcast2" >
<intent-filter android:priority="11" > <!-- 数值越大,越先接收广播 -->
<action android:name="abc" />
</intent-filter>
</receiver>
Interrupt the broadcast:
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. Dynamic registration —monitoring changes in android battery
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. Static registration -to realize the startup of the 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);
}
}
Power monitoring source code:
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>
Link: http://blog.csdn.net/q296264785/article/details/53445206