android怎么发送特定广播的?

如题所述

起一个线程,每发一个广播后就sleep一分钟,如此循环。(或者接受系统的timechanged这个广播,这个广播好像一分钟发一次)。

Android 在发送广播时的方法 sendBroadcast(Intent)。

①:Intent myIntent = new Intent();——【创建Intent对象】

②:myIntent.setAction(String)——【设置一般的要执行的动作。参数:动作一个动作的名称,如ACTION_VIEW。应用程序的具体行动,应与供应商的包名作为前缀。】

③:myIntent.putExtra(String,Object)——【广播中额外发送的数据,String为自定义key,Object表示多种数据类型】

④:sendBroadcast(myIntent);——【发送广播】

接收广播

Android在接收广播的方法是注册一个广播接收器 registerReceiver(MyReceiver,IntentFilter)。

①:首先创建MyReceiver类(类名自定义) 继承 BroadcastReceiver类。——【创建广播接收器】

②:在MyReceiver中重写public void onReceive(Context context, Intent intent)方法。这个方法在接收到广播后触发。——【重写处理方法】

③:在Activity或者Service启动时 onCreate()、onStartCommand()等方法中实例化 MyReceiver类——【启动时实例化广播接收器】

④:IntentFilter filter = new IntentFilter();——【创建IntentFilter对象 意图过滤器】

⑤:filter.addAction(String);——【在过滤器中加入过滤条件,说明接收什么广播】

⑥:registerReceiver(cmdReceiver, filter);——【注册广播,参数为(广播接收器,意图过滤器)】
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-13
Intent mIntent = new Intent(“com.android.custom.broadcast”);
context.sendBroadcast(mIntent);

这是发的,当然得写相应的接收
相似回答