5

我有一個服務「GroupsTaskAlarmChecker」是這種方式稱爲每20秒AlarmManager中的onCreate Groups.class活動:AlarmManager內廣播接收器時BOOT_COMPLETED

int seconds = 20; 

      Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class); 
      pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0); 

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 10); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent); 

這完美的作品。但是當設備啓動時我需要這樣做。 後來我知道我不得不作出AndroidManifest這樣的:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<receiver android:name=".ReceiverBoot"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"> 
      <category android:name="android.intent.category.HOME"> 
     </category></action></intent-filter> 
    </receiver> 

然後英里廣播接收器是這樣的:

public class ReceiverBoot extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    int seconds = 20; 

     Intent myIntent = new Intent(context, GroupsTaskAlarmChecker.class); 
     pendingIntent = PendingIntent.getService(context, 0, myIntent, 0); 

     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.SECOND, 10); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent); 

    } 
} 

但這的onReceive我不知道我該怎麼做,我以前做了同樣的內(意圖和alarmManager每20秒啓動一次服務)。 錯誤在這行:

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

是可能的,我不能讓裏面的BroadcastReceiver的AlarmManager?

我非常感謝你,我是一名Android成員,我需要你的幫助。對不起,我的英語;在你的onReceive)

+0

我忘了在這個問題中寫這行,但沒有寫在代碼@Jacob中。和接收廣播的init工程(在我測試init活動和它的ok之前),我只想知道如何寫入我寫入方法onReceive的第一個代碼。這可能嗎? – carlosdiazp 2013-03-05 10:31:51

回答

1

ALARM_SERVICE既不在類ReceiverBoot也不在廣播接收器定義。

您應該引用Context.ALARM_SERVICE作爲getSystemService(String)的參數。

0

if ("android.intent.action.BOOT_COMPLETED".equals (intent.getAction())){ 

    //start it again here 

} 
+0

但我的問題是使代碼: 我必須使broadcastReceive內的實例和AlarmManager。我不知道該怎麼做:/ – carlosdiazp 2013-03-05 11:02:52

+0

嘗試使用與 – MariusBudin 2013-03-05 11:06:40

+0

相同的代碼(如果我更改上下文)沒有警告和錯誤,但是在此行中是: AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); 廣播接收機內的這種方法和變量的問題... 可以我不能讓BroadcastReceiver裏面的AlarmManager? – carlosdiazp 2013-03-05 11:17:20

3

總結上面的答案和評論:onReceive處理程序接收上下文,該上下文可用於訪問getSystemService和ALARM_SERVICE 。示例代碼:

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     // Start periodic service. 
     Calendar cal = Calendar.getInstance(); 
     Intent srvIntent = new Intent(context, MyService.class); 
     PendingIntent pIntent = PendingIntent.getService(context, 0, srvIntent, 0); 
     // Use context argument to access service 
     AlarmManager alarm = 
         (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

     // Repeat every 5 seconds 
     alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
           5000, pIntent); 
     } 
    } 
} 

創建一個新的類與此代碼,當然變化MyReceiver爲MyService在你執行的名字。

1

這裏有一點貢獻,我相信這可以爲實現這個問題的目標添加更完整的視角。

第一個:在您的應用程序中配置AndroidManifest內的「接收器」。

<receiver 
    android:name=".AlarmBroadcastReceiver" 
    android:enabled="true"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 

:與擴展抽象類廣播接收器一類,你應該確定意向行動是「BOOT_COMPLETED」。如果條件滿足,您可以從您的班級調用一種方法,該方法將所有構造都報告給您的報警。

請參閱下面的代碼段。

public class AlarmBroadcastReceiver extends BroadcastReceiver { 

    String TAG = "ALARMS"; 
    String CLASS = this.getClass().getSimpleName() + ": "; 

    Context alarmContext; 

    @Override 
    public void onReceive(final Context context, Intent intent) { 

     Log.d(TAG, CLASS + "[START] onReceive()... "); 

     alarmContext = context; 

     if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 
      Log.d(TAG, CLASS + "BOOT_COMPLETED action has been received."); 
      setAlarmOnBoot(); 
     } 

      Log.d(TAG, CLASS + "[END] onReceive()... "); 

    } 

    public void setAlarmOnBoot() { 

     Log.d(TAG, CLASS + "[START] - setAlarmOnBoot()"); 

     final long beginAt = SystemClock.elapsedRealtime() + 60 * 1000; 
     final long interval = 300000; // 5 minutes 

     try { 
      AlarmManager alarm = (AlarmManager) alarmContext.getSystemService(Context.ALARM_SERVICE); 
      Intent intent   = new Intent(alarmContext, AlarmBroadcastReceiver.class); 
      PendingIntent pIntent = PendingIntent.getService(alarmContext, 0, intent, 0); 
      alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, beginAt, interval, pIntent); 
      Log.d(TAG, CLASS + "the Alarm has been configured successfully (5 minutes) of interval."); 
     } catch (Exception e) { 
      Log.d(TAG, CLASS + "an exception has ocurred while setting the Alarm..."); 
      e.printStackTrace(); 
     } 

     Log.d(TAG, CLASS + "[END] - setAlarmOnBoot()"); 

    } 

} 
+1

驚人的解決方案,並感謝爲我這樣的初學者容易理解的完整代碼 – Sasa 2017-05-31 05:18:52

+1

不客氣,朋友!我相信這段代碼並不難,並且可以改進我們在這裏得到答案的解決方案。問候 :)。 – ivanleoncz 2017-05-31 05:24:06