android入门到熟练(五)---广播
1.广播类型:标准广播和有序广播。标准广播是异步广播在广播发出之后所有接收器几乎会同一时刻接收到,没有先后顺序,效率高,但无法被截断。有序广播则是同步广播,同一时刻只能一个接收器接收这条消息,等执行完毕后才会继续传递,优先级高的接收器可以先接受,并且可以截断该广播传递。
@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();
}
}
private NetworkChangeReciver networkChangeReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReciver=new NetworkChangeReciver();
registerReceiver(networkChangeReciver,intentFilter);
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(networkChangeReciver);
}
@Override
public void onReceive(Context context,Intent intent)
{
Toast.makeText(context,"启动完成",Toast.LENGTH_SHORT).show();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhb.test2" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.zhb.Com.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@Override
public void onReceive(Context context,Intent intent)
{
String value=intent.getStringExtra("key");
Toast.makeText(context,value,Toast.LENGTH_SHORT).show();
}
}
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
btnBRCTestOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.putExtra("key","我是广播消息哦!");
sendBroadcast(intent);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brocasttest);
Button btnBRCTestOne = (Button) findViewById(R.id.btnBRCTestOne);
btnBRCTestOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.putExtra("key","我是广播消息哦!");
//sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
}
}
);
}
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
@Override
public void onReceive(Context context,Intent intent)
{
String value=intent.getStringExtra("key");
Toast.makeText(context,value,Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
private IntentFilter intentFilter;//框架的过滤器
private LocalReceiver localReceiver;//自定义的接收器
private LocalBroadcastManager localBroadcastManager;//框架的本地广播管理器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brocasttest);
localBroadcastManager=LocalBroadcastManager.getInstance(this);//获取实例
Button btnBRCTestOne = (Button) findViewById(R.id.btnBRCTestOne);
btnBRCTestOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送本地广播
Intent intent1=new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent1);
}
}
);
intentFilter=new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver=new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
}
@Override
protected void onDestroy()
{
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
class LocalReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context,Intent intent)
{
Toast.makeText(context,"接收到本地广播",Toast.LENGTH_SHORT).show();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。