13 BroadcastReceiver

  1. 复写BroadcastReceiver中的onReceive()方法。
  2. intent-filter过滤器:

    <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
    </receiver>

  3. 发送intent
    protected static final String ACTION = "com.szy.broadcast.ACTION";

    public void onClick(View v)
    {
    Intent intent=new Intent();
    intent.setAction(ACTION);
    sendBroadcast(intent);
    }

  4. 生命周期:每次发送的时候都创建,执行完之后都销毁。

在程序中注册,而不在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);
}

 

 

posted @ 2014-04-29 09:20  阿黄的苹果  阅读(100)  评论(0编辑  收藏  举报