13 BroadcastReceiver

时间:2014-04-29 09:24:46   收藏:0   阅读:357
  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);
}

 

 

13 BroadcastReceiver,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!