2016-05-12 121 views
0

我想在指定的時間觸發通知和鬧鐘。我已經把日誌信息提供給控制檯,看看是否設置了正確的時間,這很好。但是,警報仍然沒有被觸發。請幫忙。在指定時間沒有觸發鬧鐘和通知

/代碼創建通知和報警/

btn_add_task.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       db.addTask(new DataModel(input_task_title.getText().toString(),input_task_desc.getText().toString(), 
         checkBox.isChecked(),txtDate.getText().toString(),txtTime.getText().toString(),isComplete)); 

       Calendar calendar = Calendar.getInstance(); 
       //Set notification for the set date and time 
       calendar.set(Calendar.MONTH, month); 
       calendar.set(Calendar.YEAR, year); 
       calendar.set(Calendar.DAY_OF_MONTH, day); 
       calendar.set(Calendar.HOUR_OF_DAY, hour); 
       calendar.set(Calendar.MINUTE, minute); 
       calendar.set(Calendar.SECOND, 0); 
       if(mHour>12) { 
        calendar.set(Calendar.AM_PM, Calendar.PM); 
       } else { 
        calendar.set(Calendar.AM_PM, Calendar.AM); 
       } 

       Log.i("Alarm at: ",calendar.get(Calendar.DAY_OF_MONTH)+"-"+ 
         calendar.get(Calendar.MONTH)+"-"+ 
         calendar.get(Calendar.YEAR)+" at "+ 
         calendar.get(Calendar.HOUR_OF_DAY)+":"+ 
         calendar.get(Calendar.MINUTE)); 

       Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class); 
       PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT); 
       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
       am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi); 

       Toast.makeText(getApplicationContext(),R.string.new_task_added,Toast.LENGTH_LONG).show(); 
       startActivity(new Intent(AddTask.this,MainActivity.class)); 
      } 
     }); 

/通知消息類/

package com.apps.katz.doer; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

/** 
* Created by katz on 4/5/16. 
*/ 
public class NotificationMessage extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     showNotification(context); 
    } 

    private void showNotification(Context context) { 
     Log.i("notification","visible"); 
     PendingIntent contentIntent = PendingIntent.getActivity(context,0, 
       new Intent(context,NotificationMessage.class),0); 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Doer Notification") 
       .setContentText("This is a Doer Notification"); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); 
     mBuilder.setDefaults(Notification.DEFAULT_LIGHTS); 
     mBuilder.setAutoCancel(true); 
     NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(1,mBuilder.build()); 
    } 
} 

請幫我在做什麼錯?

回答

1

看看在setRepeating方法的documentation

注:爲API 19日,所有重複報警是不準確的。如果您的應用程序需要精確的交付時間,那麼它必須使用一次性精確警報,如上所述每次重新安排時間。其targetSdkVersion早於API 19的傳統應用程序將繼續擁有其所有警報,包括重複警報,並將其視爲確切對待。

您的報警將始終使用setRepeating,嘗試使用setExact並在報警觸發時重新編程。

此外,請嘗試擴展WakefulBroadcastReceiver而不是BroadcastReceiverNotificationMessage以便能夠在應用程序未運行時喚醒服務。

+0

嗨Jofre,我試着使用setRepeating和setExactAndAllowWhileIdle,但沒有任何工作方式。請提供其他提示嗎? –

+0

你是什麼意思沒有工作?沒有在確切的時間觸發鬧鐘,或根本沒有工作? –

+0

根本沒有觸發警報。 –

0

btn_add_task監聽器裏,設置報警如下 -

Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class); 
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);  
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi); 

現在,把日誌裏面NotificationMessage類的onReceive以確保警報準時發射。希望,現在你的程序將運行。

+0

沒有工作:(。 –

+0

你可以請嘗試這個 - (http://stackoverflow.com/questions/37179957/broadcastreceiver-cannot-show-notification-when-app-封閉殺死/ 37207265#37207265) –