程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-Broadcast service 沒有被調用

java-Broadcast service 沒有被調用

編輯:編程綜合問答
Broadcast service 沒有被調用

我使用下面的代碼調用 Broadcast service

Intent i = new Intent(this, BootUpReceiverRecall.class);
        sendBroadcast(i);
 <receiver  android:process=":remote" android:name="BootUpReceiverRecall"></receiver>
public class BootUpReceiverRecall extends BroadcastReceiver 
{
      // Restart service every 30 seconds
      private static final long REPEAT_TIME = 1000 * 30;
      @Override
      public void onReceive(Context context, Intent intent) 
      {
        AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, BootUpReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        // Start 30 seconds after boot completed
        cal.add(Calendar.SECOND, 30);
        //
        // Fetch every 30 seconds
        // InexactRepeating allows Android to optimize the energy consumption
        service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), REPEAT_TIME, pending);
        // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
        // REPEAT_TIME, pending);
      }

但是我的BootUpReceiver 從來沒有被調用,哪裡出錯了呢?

最佳回答:


Intent i = new Intent(this, BootUpReceiverRecall.class);
        sendBroadcast(i);

為什麼要這樣寫呢? 我是沒這樣寫過
廣播分2中注冊方式:
1中是代碼注冊,局部廣播 可以寫在Activity中

YourReceiver reveiver = new YourReceiver ();
IntentFilter filter = new IntentFilter();
        filter.addAction("yourAction");
        registerReceiver(reveiver , filter);

Activity生命周期中可以管理register 和 unregister
2.manifest配置 節點下
創建 YourReceiver在xx.xx.xx.receiver(隨便寫)包下 extends BrocastReceiver
根據action過濾

你這個可改成

Intent i = new Intent(“action”);
        sendBroadcast(i);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved