Android广播及其实例

广播机制简介

在Android中,广播主要分为两种,标准广播和有序广播。注意:标准广播是一种完全异步执行的广播,发出的广播无法拦截;有序广播是一种同步执行的广播,在广播发出后,同一时刻只有一个广播接收器能接收到广播,而且该广播可以被当前广播接收器进行拦截,以阻止其继续发送下去。

1.接收系统广播之动态注册监听网络变化。

下面是实例:

package com.example.broadcasttest1;


import android.os.Bundle;
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.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//定义一个类继承BroadCast类并重写ta的onreceiver方法
class NetworkChangeReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info!=null&&info.isAvailable())
{
Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
}

}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
Toast.makeText(getApplicationContext(), "receiver unregister", Toast.LENGTH_SHORT).show();
}
}

这里需要在清单文件中获得访问网络状态的权限:"android.permission.ACCESS_NETWORK_STATE"


2.静态注册广播实现开机启动。

这个程序相对简单:在清单文件中注册一个广播接收器,并且把ta接收的广播也声明好即可。

3.发送本地广播:

步骤:首先通过LocalBroadcastManager  的getInstance方法得到ta的一个实例,然后在注册的时候调用的是LocalBroadcastManager的registerReceiver方法,在发送广播的时候调用的是LocalBroadcastManager的sendBroadcast方法。通过定义好的一个 Button设置好 监听来发送广播。

package com.example.broadcasettest;


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.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
private IntentFilter intentFilter;
private Button bt;
private LocalReceiver localreceiver;
private LocalBroadcastManager localBroadcastManager;//localBrocadcastManager起了关键作用~
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager = LocalBroadcastManager.getInstance(this);//得到实例
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.example.ddbroadcasettest.LOCAL_BROADCAST");//指定发送的广播
localBroadcastManager.sendBroadcast(intent);//发送本地广播
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.ddbroadcasettest.LOCAL_BROADCAST");//指定接收的广播
localreceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localreceiver, intentFilter);//注册好广播接收器(广播接收器和接收的广播)
}
class LocalReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "receiverd in local broadcast", Toast.LENGTH_LONG).show();//弹出一个toast
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
localBroadcastManager.unregisterReceiver(localreceiver);//解绑广播接收器
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值