赞
踩
android中的广播有两种类型为:标准广播和有序广播。
1. 标准广播为完全异步的广播,在广播发出后所有的广播接收器都能接收到此条广播,也意味着标准广播不能被截断。
2. 有序广播是有先后顺序的按照优先级的高低来传递广播,意味着可以被截断。
package com.test.brodcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
//设置意图传播器
private IntentFilter intentFilter;
//定义为全局变量方便调用,NetworkChangeReceiver为内部类
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化IntentFilter
intentFilter =new IntentFilter();
//添加新的活动,android.net.conn.CONNECTIVITY_CHANGE这些活动都是android内部定义的活动,可查看api
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//得到自己继承于BroadcastReceiver的类的对象
networkChangeReceiver = new NetworkChangeReceiver();
//注册广播接收机,传入继承过BroadcastReceiver的类的对象,以及要监听的活动
registerReceiver(networkChangeReceiver, intentFilter);
}
@Override
protected void onDestroy() {
// 活动销毁时记得取消注册
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
/**
*自己定义的广播类,必须继承BroadcastReceiver
*/
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//连接管理类
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//获得网络连接的信息
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
//判断网络连接的状态
if(networkInfo != null&& networkInfo.isAvailable()){
Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "network is unaviailable", Toast.LENGTH_SHORT).show();
}
}
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
继承BroadcastReceiver,不要再onReceive()添加过多的逻辑或者进行任何耗时的操作,因为在广播接收器中是不允许开启线程的
public class BootCompleteReceiver extends BroadcastReceiver{
@override
public void onReceive(Context context,Intent intent){
Toast.makeText(context,"Boot Complete",Toast.LENGTH).show();
}
}
在AndroidMainfest.xml中增加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
在application下添加
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
package com.example.standardbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "receives in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();;
}
}
<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="com.example.standardbroadcast.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
package com.example.standardbroadcast;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//为对应关系,可以随便取名,但是一般定义为com.example.standardbroadcast.MY_BROADCAST
//包名加后缀的形式方便识别,创建一个intent
"当然你可以在intent中携带数据进行传输"
Intent intent = new Intent("com");
//将广播发送出去
sendBroadcast(intent);
}
});
}
注意添加到application 的分坐标上
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<!--监听发出"com"的事件,只要有就会相应,前提是没有流产-->
<action android:name="com"/>
</intent-filter>
</receiver>
</application>
package com.example.standardbroadcast;
import android.support.v4.content.LocalBroadcastManager;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
//内容传播者
private IntentFilter mIntentFilter;
//建立自己创建类的对象
private LocalReceiver mLocalReceiver;
//为系统内部类,本地广播管理器
private LocalBroadcastManager mLocalManger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
//得到本地广播管理的实例
mLocalManger =LocalBroadcastManager.getInstance(this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//为对应关系,可以随便取名,但是一般定义为com.example.standardbroadcast.MY_BROADCAST
//包名加后缀的形式方便识别
"建立内部的广播传递信息的标志"
Intent intent = new Intent("comaction");
//自己创建的类去发送广播
mLocalManger.sendBroadcast(intent);
}
});
//初始化内容传播者
mIntentFilter = new IntentFilter();
//添加要接受的活动
mIntentFilter.addAction("comaction");
//创建自己所建类的实例
mLocalReceiver = new LocalReceiver();
//用本地广播去注册
mLocalManger.registerReceiver(mLocalReceiver, mIntentFilter);
}
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "receive in localBroadcast", Toast.LENGTH_SHORT).show();
}
}
}
1. 可以明确的知道正在发送的广播不会离开我们的程序,因此不用担心机密数据的泄露。
2. 其他的程序无法将广播发送到我们的程序内部,不用担心有安全漏洞。
3.发送本地广播比起发送系统全局广播会更加高效。
abortBroadcast();
//为对应关系,可以随便取名,但是一般定义为com.example.standardbroadcast.MY_BROADCAST
//包名加后缀的形式方便识别
Intent intent = new Intent("com");
sendOrderedBroadcast(intent, null);
<intent-filter android:priority="100">
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.remove(activity);
}
public static void finishAll(){
for (Activity activity:activities){
if(!activity.isFinishing()){
activity.finish();
}
}
}
}
import android.app.Activity;
import android.os.Bundle;
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
package com.test.teacherbroadcaststembereight;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button button;
private Button mButtonCancel;
private Button mButtonAlarm;
private MyBroadcast broadcast;
private AlarmManager mAlarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcast=new MyBroadcast();
IntentFilter filter=new IntentFilter();
filter.addAction("com.test.mytest");
registerReceiver(broadcast, filter);
mButtonAlarm=(Button) findViewById(R.id.alarm_button);
mButtonAlarm.setOnClickListener(this);
mButtonCancel=(Button) findViewById(R.id.alarm_cancle);
mButtonCancel.setOnClickListener(this);
button= (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
mAlarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent intent=new Intent();
intent.setAction("com.test.mytest");
sendBroadcast(intent);
break;
case R.id.alarm_button:
Intent intent1=new Intent();
intent1.setAction("com.test.mytest");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this, 2, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,3000,pendingIntent);
break;
case R.id.alarm_cancle:
Intent intent2=new Intent();
intent2.setAction("com.test.mytest");
PendingIntent pendingIntent2=PendingIntent.getBroadcast(MainActivity.this, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2.cancel();
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcast);
}
}
package com.test.teacherbroadcaststembereight;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "guangbo", Toast.LENGTH_SHORT).show();
}
}
<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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"/>
<Button
android:id="@+id/alarm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="闹钟"/>
<Button
android:id="@+id/alarm_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancle"/>
</LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。