2017-01-03 16 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 + ""); 
} 

を作成するための方法Iであり、これは私の放送受信機クラスです。マニフェストで

:私はこのために、あなたは次のようにそれを作成する必要があり、あなたは、アプリケーションが実行されている独立しているか否か、アラームを作成するために受信機を使用する必要があります

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

は、設定された新しい時間を印刷し、どのようにあなたが何であるかOSのバージョンに...またアンドロイドマニフェスト –

+0

をテストしていますあなたは走っている? –

+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

手の込んだしてください...何OSのバージョンにあなたは走っていますか? あなたは、Android 6.0以上で実行されている可能性がありますので、あなたはDozeを検討する必要があります...

関連する問題