2012-10-10 2 views
0

다음 활동이 있습니다. 기본적으로 사용자가 응용 프로그램에 로그인하면 AlarmManager가 설정됩니다. 그런 다음 AlarmManager는 주기적으로 전화기의 DB에서 트랜잭션을 삭제하는 다른 작업을 호출합니다. 이 모든게 잘 작동합니다.BootReceiver에 AlarmManager를 연결하는 방법

// 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을 :

당신은해야한다 (당신은이 onReceive에서 수신)? 당신은 BroadcastReceiver를 의미합니다. – RvdK

+0

@PoweRoy 네, 죄송합니다. 시스템 부팅을받는 BroadcastReceiver – turtleboy

+0

'캘린더 객체에 5 분 추가 '? 1 분? –

답변

1

getSystemServiceContext 볼 수 있습니다.

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

내가 제안한 것으로 변경했지만 불행히도 ALARM_SERVICE를 다시 열 수 없습니다. – turtleboy

+0

당신은 또한 'Context.ALARM_SERVICE'가 필요합니다. –

+0

@DavidWasser : 감사합니다. – RvdK

관련 문제