2013-12-11 4 views
2

매일 오전 6시에 내 앱이 "좋은 아침"메시지와 함께 알림을 표시하도록합니다. 내가 읽었을 때, 나는 앱을 백그라운드에서 실행해야하므로 서비스를 사용해야한다.지정된 시간에 매일 알림을 생성하는 서비스

다음 코드를 시도했지만 막혔습니다.

MainActivity.java

public void onClickStartService(View v) 
    { 
     startService(new Intent(this,MyService.class)); 
    } 

    public void onClickStopService(View v) 
    { 
     stopService(new Intent(this,MyService.class)); 
    } 

및 MyService.java 내가 시작하고 서비스를 중지하는 버튼이 있고 작동

public class MyService extends Service{ 

    private static final String TAG = "MyService"; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onCreate"); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onStart"); 
    //Note: You can start a new thread and use it for long background processing from here. 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onDestroy"); 
    } 

} 

입니다. 이제는 게시물의 시작 부분에서 언급 한 것처럼 서비스에서 알림을 생성하려고합니다. 어떻게해야합니까?

답변

3

당신은 PendingIntent에서 시작 AlarmManager

Tutorial here

망가은 또한

mAlarmManager.cancel(pendingIntent); 

와 알람 매니저를 취소 할 가능성을 추가하는 것을 잊지 당신은 android.intent.action.BOOT_COMPLETED을 차단 할 수 있습니다 일정에 따라 서비스를 시작하려면 재부팅 후 바로 시작하는 앱을 만듭니다.

4

특정 시간에 서비스를 시작하려면 Alarm에 의해 트리거 된 BroadcastReceiver를 작성하여 서비스를 시작하는 것이 좋습니다.

먼저이 같은 브로드 캐스트 리시버 쓰기 :

public class AlarmReceiver extends BroadcastReceiver { 


    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     context.startService(new Intent(context, MyService.class)); 
    } 


    /** 
    * Schedule the next update 
    * 
    * @param context 
    *   the current application context 
    */ 
    private static void scheduleServiceUpdates(final Context context) { 
     // create intent for our alarm receiver (or update it if it exists) 
     final Intent intent = new Intent(context, AlarmReceiver.class); 
     final PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // compute first call time 1 minute from now 
     Calendar calendar = Calendar.getInstance(); 
     calendar.add(Calendar.MINUTE, 10); 
     long trigger = calendar.getTimeInMillis(); 

     // set delay between each call : 24 Hours 
     long delay = 24 * 60 * 60 * 1000; 

     // Set alarm 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC, trigger, delay, pending); 
     // you can use RTC_WAKEUP instead of RTC to wake up the device 
    } 
} 

그럼 그냥 reccuring 이벤트를 시작합니다 scheduleServiceUpdate 메소드를 호출 할 필요가 있습니다. RTC 유형 만 사용하는 경우 알람이 서비스를 트리거해야하는 경우 전화기가 잠겨 있으면 사용자가 장치를 잠금 해제 할 때까지 대기하지 않습니다. RTC_Wakeup을 사용하면 주어진 시간에 정확하게 서비스가 시작됩니다.

이벤트를 트리거하는 다른 방법은 AlarmManager입니다.

관련 문제