2012-10-10 30 views
0

我在活動中有以下內容,它基本上在用戶登錄到應用程序時設置AlarmManager。 AlarmManager然後定期呼叫另一個活動,從電話的數據庫中刪除交易。所有這些工作正常。如何將AlarmManager附加到BootReceiver中

// get a Calendar object with current time 
Calendar cal = Calendar.getInstance(); 
// add 5 minutes to the calendar object 
cal.add(Calendar.MINUTE, 1); 
Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class); 
intent.putExtra("alarm_message", "deleting transactions"); 
// In reality, you would want to have a static variable for the request code instead of 192837 
PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837, 
            intent, PendingIntent.FLAG_UPDATE_CURRENT); 
// Get the AlarmManager service 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender); 

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      Bundle bundle = intent.getExtras(); 
      String message = bundle.getString("alarm_message"); 
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
      Intent myIntent = new Intent(context, SendOutstandingTransactions.class); 
      myIntent.setAction("com.carefreegroup.startatboot.MyService"); 
      context.startService(myIntent); 
     } catch (Exception e) { 
      Toast.makeText(context, "There was an error somewhere, but we still" 
          + " received an alarm", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 
} 

public class SendOutstandingTransactions extends IntentService { 

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName(); 
    NfcScannerApplication nfcscannerapplication; 
    Cursor c; 

    @Override 
    public void onCreate() { 
     nfcscannerapplication = (NfcScannerApplication)getApplication(); 
     super.onCreate(); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     nfcscannerapplication.loginValidate.deleteTableTransactions(); 
    } 

    public SendOutstandingTransactions() { 
     super("SendOutstandingTransactions"); 
    } 
}// end of class 

定期刪除事務的服務僅在用戶首次登錄到應用程序時調用。它從那時起無限期地運行。如果用戶重新啓動電話會怎麼樣?該服務只會在用戶下次登錄時恢復。

我有一個BootReceiver,但我不知道如何連接AlarmManager。我試過以下,但ALARM_SERVICE無法解析。這是正確的嗎?

public class MyBootReceiver extends BroadcastReceiver { 

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

     // Intent myIntent = new Intent(context, SendOutstandingTransactions.class); 
     // myIntent.setAction("com.carefreegroup.startatboot.MyService"); 
     // context.startService(myIntent); 

     // get a Calendar object with current time 
     Calendar cal = Calendar.getInstance(); 
     // add 5 minutes to the calendar object 
     cal.add(Calendar.MINUTE, 1); 
     Intent intent = new Intent(MyBootReceiver.this, AlarmReceiver.class); 
     intent.putExtra("alarm_message", "deleting transactions"); 
     // In reality, you would want to have a static variable for the request code instead of 192837 
     PendingIntent sender = PendingIntent.getBroadcast(context, 192837, 
            intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Get the AlarmManager service 
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender); 
    } 
} 
+0

BootReceiver?你的意思是BroadcastReceiver。 – RvdK

+0

@PoweRoy對不起一個接收系統啓動的BroadcastReceiver – turtleboy

+0

'給日曆對象添加5分鐘?你的意思是1分鐘? –

回答

1

getSystemService可在Context上獲得。 (您收到的onReceive

你應該這樣做:

AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
+0

我已將其更改爲您建議的內容,但不幸的是ALARM_SERVICE仍然無法重新使用 – turtleboy

+0

您還需要'Context.ALARM_SERVICE' –

+0

@DavidWasser:謝謝添加 – RvdK