2014-11-24 66 views
0

我想在特定的時間每天開始一項服務,然後每分鐘發送一次服務啓動通知,服務單獨良好,但當警報管理人員呼叫時什麼也沒有發生。在每天的特定時間開始服務,每天由警務人員不工作

這裏報警代碼:

Calendar cur_cal = new GregorianCalendar(); 
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar 

Calendar cal = new GregorianCalendar(); 
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR)); 
cal.set(Calendar.HOUR_OF_DAY, 20); 
cal.set(Calendar.MINUTE, 23); 
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND)); 
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND)); 
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE)); 
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH)); 
Intent intent = new Intent(this, NotifService.class); 
PendingIntent pintent = PendingIntent.getBroadcast(this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30 * 1000, pintent); 

這裏NotiService.Class:

public class NotifService extends Service { 

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); 
    private static final int HELLO_ID = 1; 

    @Override 
    public IBinder onBind(Intent arg0) { 

     return null; 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 

     super.onStart(intent, startId); 
     final Intent intent1 = new Intent(this, DialogoActivity.class); 

     scheduler.scheduleWithFixedDelay(new Runnable() { 

      @Override 
      public void run() { 

       // Look up the notification manager server 
       NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

       // Create your notification 
       //int icon = R.drawable.ic_launcher; 
       CharSequence tickerText = "Dominion"; 
       //long when = System.currentTimeMillis(); 
       //Notification notification = new Notification(icon, tickerText, when); 
       Context context = getApplicationContext(); 
       CharSequence contentTitle = "your turn"; 
       String contentText = "click here"; 

       PendingIntent pIntent = PendingIntent.getActivity(NotifService.this, 0, intent1, 0); 

       Notification notification = new NotificationCompat.Builder(getApplicationContext()).setContentIntent(pIntent).setTicker(tickerText).setContentTitle(contentTitle).setContentText(contentText).setSmallIcon(R.drawable.ic_launcher).addAction(R.drawable.ic_launcher, "Si", pIntent).setVibrate(new long[] {100, 250, 100, 500}).build(); 

       notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
       // Send the notification 

       nm.notify(HELLO_ID, notification); 
      } 
     }, 1, 1, TimeUnit.MINUTES); 
    } 

    @Override 
    public void onDestroy() { 

     super.onDestroy(); 
    } 
} 
+0

廣播接收機實際調用它嗎? – Elltz 2014-11-24 20:18:55

+0

您可能需要從服務調用未處理的意圖而不是廣播 SEE PendingIntent.getService(context,requestCode,intent,flags) – 2014-11-24 20:20:43

+0

我嘗試PendingIntent.getService(context,requestCode,intent,flags)但沒有任何錯誤,但不工作太 – user2964637 2014-11-25 13:23:55

回答

相關問題