Android广播的使用(自定义广播和本地广播)

Posted (?ω?)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android广播的使用(自定义广播和本地广播)相关的知识,希望对你有一定的参考价值。

1》自定义广播

//定义接收器
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收我的广播",Toast.LENGTH_LONG).show();
}
}
·//注册事件
<receiver
android:name=".MyBroadcastReceiver"//类名
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttext.MY_BROADCAST"/>广播名称
</intent-filter>
</receiver>
//定义按钮发送广播

 

 


Button button=(Button)findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent("com.example.broadcasttext.MY_BROADCAST");
sendBroadcast(intent);
}
});
  

 

 
 

广播在另外的程序上进行接收,默认的发送方式是标准广播,若想发送有序广播,需要一点点修改

MainActivity.java

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent("com.example.broadcasttext.MY_BROADCAST");
sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
}
});

加入这一行代码后广播的接收就有了顺序

注册时候可以设置接收广播的优先级

<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttext.MY_BROADCAST"/>
</intent-filter>
</receiver>

优先接收到广播后就可以判断是否继续发送广播

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收我的广播",Toast.LENGTH_LONG).show();
abortBroadcast();
}
}

abortBroadcast();可以截断广播

2》本地广播

上述例子中发送的广播可以被其他程序所接受,这对安全造成了威胁,所以安卓引入了一套本地广播的体制

  MainActivity.java


package com.example.guangbo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.nio.channels.NetworkChannel;

public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
//本地广播
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//本地广播
localBroadcastManager= LocalBroadcastManager.getInstance(this);

intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver=new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
Button button=(Button)findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//本地广播
Intent intent=new Intent("com.example.broadcasttext.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);

}
});
//动态注册本地广播
intentFilter=new IntentFilter();
intentFilter.addAction("com.example.broadcasttext.LOCAL_BROADCAST");
localReceiver=new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
//本地广播
localBroadcastManager.unregisterReceiver(localReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null&&networkInfo.isAvailable()){
Toast.makeText(context,"网络可用",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"网络不可用",Toast.LENGTH_SHORT).show();
}


}
}
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context,"接收本地广播",Toast.LENGTH_SHORT).show();



}
}
}
 

啊本地广播主要是通过LocalBriadcaseManager来进行管理

 

以上是关于Android广播的使用(自定义广播和本地广播)的主要内容,如果未能解决你的问题,请参考以下文章

Android四大组件——BroadcastReceiver普通广播有序广播拦截广播本地广播Sticky广播系统广播

Android——BroadcastReceiver广播机制(自定义广播)

Android——BroadcastReceiver广播机制(自定义广播)

Android——BroadcastReceiver广播机制(自定义广播)

Android连载22-自定义广播之标准广播发送

android 广播自定义广播接收问题