关闭我的应用程序时,通知无效

时间:2017-01-03 14:40:06

标签: android alarmmanager

我正在创建一个应用程序,用户可以在特定日期安排预约。为了测试,当我通过设置警报5或10分钟进一步检查它然后它正常工作。但是,如果我进一步设置闹钟数小时或几天,或者我关闭我的应用程序,那么它不会发出通知.. 以下是用于创建通知的方法i

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());
    }

}

2 个答案:

答案 0 :(得分: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));
    }
 }

答案 1 :(得分:0)

请详细说明......您正在运行哪些操作系统版本? 您可能在Android 6.0+上运行,因此您需要考虑Doze ...