2017-01-03 76 views
0

我正在創建一個應用程序,用戶可以在該應用程序中安排特定日期的預約。進行測試,當我通過設置5或10分鐘的警報進行檢查時,它會正常工作。但是,如果我設置爲數小時或數天再報警或關閉我的應用程序,然後它是不會放棄的通知.. 下面是一個方法我爲創建通知當我關閉我的應用程序時,通知無法正常工作

public void setAlarm() { 
    Calendar c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, ihour); 
    c.set(Calendar.MINUTE, iminute); 
    c.set(Calendar.DAY_OF_MONTH, idate); 
    c.set(Calendar.MONTH, imonth); 
    c.set(Calendar.YEAR, iyear); 

    SharedPreferences sharedPreferences = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE); 
    SharedPreferences.Editor editor = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE).edit(); 
    request_id = sharedPreferences.getInt(variable.APPOINTMENT_NO, 0); 
    if (request_id == 0) { 
     request_id = 1; 
     editor.putInt(variable.APPOINTMENT_NO, request_id).commit(); 
    } else { 
     request_id++; 
     editor.putInt(variable.APPOINTMENT_NO, request_id).commit(); 
    } 

    Intent intent1 = new Intent(Doctor_Appointment_Set.this, AppointmentSetReciever_inner.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      Doctor_Appointment_Set.this, request_id, intent1, 
      0); 
    AlarmManager am = (AlarmManager) Doctor_Appointment_Set.this 
      .getSystemService(Doctor_Appointment_Set.this.ALARM_SERVICE); 
    am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); 
    Log.d(getClass().getSimpleName(), request_id + ""); 
} 

,這是我的廣播接收器類。我創建它作爲一個內部類

public static class AppointmentSetReciever_inner extends BroadcastReceiver { 


    @Override 
    public void onReceive(Context context, Intent intent) { 

     Random randomGenerator = new Random(); 
     int index = randomGenerator.nextInt(3); 
     Log.d(getClass().getSimpleName(), request_id + ""); 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(context, Show_Appointment_Notification.class); 
     notificationIntent.putExtra("NotificationMessage", dr_name + " | " + st_time + " | " + st_date); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, request_id, 
       notificationIntent, 0); 


     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
       context) 
       .setContentTitle("Appointment") 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.male)) 
       .setSmallIcon(R.mipmap.logo) 
       .setContentText("Today You Have an Appointment with Dr. " + dr_name) 
       //.setStyle(new NotificationCompat.BigTextStyle().bigText(tip.get(index))) 
       .setSound(alarmSound) 
       .setAutoCancel(true).setWhen(when) 
       .setContentIntent(pendingIntent) 
       .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) 
       .setPriority(Notification.PRIORITY_MAX); 
     notificationManager.notify(request_id, mNotifyBuilder.build()); 
    } 

} 
+0

打印已設置新的時間和你如何在什麼操作系統版本測試 –

+0

此外Android清單 –

+0

請詳細...你在跑? – liorlis

回答

0

你應該用一個接收器來創建應用程序獨立是否正在運行的報警,對於這一點,你必須按照以下步驟創建它:

在清單:

<receiver android:name=".OnBootReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

代碼:

public class OnBootReceiver extends BroadcastReceiver { 

    private static final String TAG = OnBootReceiver.class.getSimpleName(); 

    private static final int PERIOD = 1000 * 60 * 10; // 10 minutes 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      setAlarm(context); 
     } 
    } 

    public static void setAlarm(Context context) { 
     AlarmManager mgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

     boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
       new Intent(context, Doctor_Appointment_Set.class),PendingIntent.FLAG_NO_CREATE) != null); 

     if (!alarmUp){  
      mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
        SystemClock.elapsedRealtime()+60000, 
        PERIOD, 
        getPendingIntent(context)); 
     }else{ 
      Log.i(TAG, "OnBootReceiver : Alarm is already active"); 
     } 
    } 

    public static void cancelAlarm(Context ctxt) { 
     AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); 

     mgr.cancel(getPendingIntent(ctxt)); 
    } 

    private static PendingIntent getPendingIntent(Context ctxt) { 
     Intent i=new Intent(ctxt, OnAlarmWakefulReceiver.class); 

     return(PendingIntent.getBroadcast(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)); 
    } 
} 
0

請詳細說明......在什麼操作系統版本你運行嗎? 你可能會在Android 6.0+上運行,因此你需要考慮Doze ...

相關問題