當前位置:編程學習大全網 - 遊戲軟體 - Android 怎麽在onReceive方法中再發壹條廣播

Android 怎麽在onReceive方法中再發壹條廣播

起壹個線程,每發壹個廣播後就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);——註冊廣播,參數為(廣播接收器,意圖過濾器)

  • 上一篇:立冬圖片唯美句子
  • 下一篇:英雄不流淚演員表
  • copyright 2024編程學習大全網