13 BroadcastReceiver
- 复写BroadcastReceiver中的onReceive()方法。
- intent-filter过滤器:
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver> - 发送intent
protected static final String ACTION = "com.szy.broadcast.ACTION";
public void onClick(View v)
{
Intent intent=new Intent();
intent.setAction(ACTION);
sendBroadcast(intent);
} - 生命周期:每次发送的时候都创建,执行完之后都销毁。
在程序中注册,而不在xml配置文件中注册:
protected static final String ACTION = "com.szy.broadcast.ACTION";
private SecondReceiver receiver;
//发送消息
public void onClick(View v)
{
Intent intent=new
Intent();
intent.setAction(ACTION);
sendBroadcast(intent);
}
//注册广播
public void onClick(View v)
{
receiver=new
SecondReceiver();
IntentFilter filter=new
IntentFilter();
filter.addAction(ACTION);
registerReceiver(receiver,
filter);
}
//注销广播
public void onClick(View
v)
{
unregisterReceiver(receiver);
}