2013-09-16 8 views
0

我的Android服務必須檢查當前時間是否與保存的時間匹配。我使用了一個具有定時器的服務,每分鐘檢查當前時間是否與保存時間匹配。如果Android進入睡眠模式,他會停止檢查。我嘗試了WakeLocker來解決這個問題。它可以工作,但它會排出我的電池。Android服務 - 如果匹配節省的時間,請繼續檢查當前時間

我目前使用的代碼如下。

public class AlarmService extends Service { 

    private static Timer timer = new Timer(); 
    private Context ctx; 
    private WakeLock wakeLock; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return Service.START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show(); 
     super.onCreate(); 
     ctx = this; 
     startService(); 

     /* Wake locker 
     PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE); 
     wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Wake locker"); 
     wakeLock.acquire(); 
     */ 

    } 

    private void startService() 
    {   
     timer.scheduleAtFixedRate(new mainTask(), 0, 60000); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show(); 
     startService(); 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 

    private class mainTask extends TimerTask 
    { 
     public void run() 
     { 
      toastHandler.sendEmptyMessage(0); 
     } 
    } 

    private final Handler toastHandler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message msg) 
     { 
      Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Amsterdam")); 
      Date currentLocalTime = cal.getTime(); 
      DateFormat date = new SimpleDateFormat("HH:mm", new Locale("nl", "nl")); 
      date.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam")); 
       String currenttime = date.format(currentLocalTime); 

       DateFormat date2 = new SimpleDateFormat("EEEE", new Locale("nl", "nl")); 
       date2.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam")); 
        String currentday = date2.format(currentLocalTime); 

      if(currentday.equals("maandag")) { // "maandag" is dutch for Monday 
       String tijd1 = "16:00"; 

       if(currenttime.equals(tijd1)) { 
        NotificationCompat.Builder mBuilder = 
          new NotificationCompat.Builder(AlarmService.this) 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setContentTitle("Test message") .setAutoCancel(true) 
          .setAutoCancel(true) 
          .setContentText("This is a test message"); 
        Intent resultIntent = new Intent(AlarmService.this, MainActivity.class); 
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(AlarmService.this); 
        stackBuilder.addParentStack(MainActivity.class); 
        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
        mBuilder.setContentIntent(resultPendingIntent); 
        NotificationManager mNotificationManager = (NotificationManager) AlarmService.this.getSystemService(Context.NOTIFICATION_SERVICE); 
        mNotificationManager.notify(1, mBuilder.build()); 

       }     
      } 
     } 
    }; 
} 

有人有解決這個問題,這不會耗盡我的電池?所以它必須檢查當前時間是否與保存的時間匹配,即使Android處於睡眠狀態。

謝謝!

回答

2

使用AlarmManager可以安排一段代碼在需要時運行。

您需要一個AlarmManager和一個擴展BroadcastReceiver的類。然後重寫onReceive()方法並在那裏放置你想要運行的代碼。設備將在接收器內的onReceive()方法持續時間內喚醒。

AlarmManager alarmManager = context.getSystemService(Context.ALARM_SERVICE) //Please note that context is "this" if you are inside an Activity 

Intent intent = new Intent(context, MyReceiver.class); 
PendingIntent event = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
alarmManager.set(AlarmManager.RTC_WAKEUP, millisToFireEvent, event);