国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中的廣播和廣播接收器代碼實例

2020-04-11 11:33:57
字體:
供稿:網(wǎng)友

BroadcastReceiver不僅可以接收系統(tǒng)廣播,也可接收自定義的廣播
 
1.定義一個廣播接收器

復(fù)制代碼 代碼如下:

public class MyReceiver extends BroadcastReceiver
{
         public MyReceiver()
         {
                   Log.i(TAG,"MyReceiver");
         }
       
         //可用Intent的getAction()區(qū)分接收到的不同廣播
         @Override
         public void onReceive(Context arg0, Intent arg1)
         {
                   String action=intent.getAction();
                   //TO-DO LIST
         }
 
}

 
2.發(fā)送廣播,定義好action標(biāo)志,用Intent發(fā)送
復(fù)制代碼 代碼如下:

//實例化該BroadcastReceiver
MyReceiver mReceiver=new MyReceiver();
//設(shè)置唯一action,用Intent發(fā)送
Intent intent=new Intent();
intent.setAction(str);
sendBroadcast(intent);

3.注冊只接收指定action的廣播接收器
復(fù)制代碼 代碼如下:

IntentFilter filter1=new IntentFilter();
filter1.addAction(str);
registerReceiver(mReceiver,filter1);

4.取消該廣播接收器
復(fù)制代碼 代碼如下:

unregisterReceiver(mReceiver);

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 修水县| 东宁县| 中卫市| 通山县| 大理市| 修武县| 昌江| 格尔木市| 平舆县| 遂平县| 喀喇沁旗| 苗栗市| 文安县| 昆明市| 阳谷县| 五寨县| 澄城县| 盐源县| 长宁区| 安吉县| 连州市| 明星| 石林| 武乡县| 固镇县| 会昌县| 洪湖市| 嵊泗县| 临安市| 泰来县| 阿尔山市| 游戏| 哈巴河县| 奉贤区| 昭平县| 鄂托克前旗| 当阳市| 睢宁县| 宾阳县| 松阳县| 临城县|