即使设置了相同的挂起意图,AlarmManager.cancel()也不起作用

时间:2019-07-31 10:10:51

标签: java android alarmmanager

因此,我试图取消setExact警报,该警报是通过我的应用程序上的接收器手动进行的,但无法正常工作。

我试图匹配待定的意图使其起作用,但仍然不起作用。

这是设置的警报代码:

 public void setAlarm(Bundle bundle){

        Class intentClass = getMainActivityClass();
        if (intentClass == null) {
            return;
        }

        String notificationIdString = bundle.getString("id");
            Log.e(TAG, notificationIdString);
            int notificationID = Integer.parseInt(notificationIdString);

//            String message = bundle.getString("message");

            String fireDate = bundle.getString("fire_date");
            if (fireDate == null) {
                Log.e(TAG, "failed to schedule notification because fire date is missing");
                return;
            }
            Log.e(TAG, fireDate);
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);
            Log.e(TAG, String.valueOf(sdf));
            Date date = null;
            Long fd = null;
            try {
                date = sdf.parse(fireDate);
                fd = date.getTime();
            } catch (ParseException e){
                e.printStackTrace();
                return;
            } catch (Exception e){
                e.printStackTrace();
                return;
            }

            Log.e(TAG, "setAlarm: " + fd);
            Log.e(TAG, "setAlarm: " + date);
         Intent intent = new Intent(mContext.getApplicationContext(), ANAlarmReceiver.class);
         intent.putExtra("bundle",bundle);
         intent.putExtra("notificationId",notificationID);

//            intent.setAction("com.emekalites.react.alarm.notification.START_ALARM");

//            intent.setAction("START_ALARM");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    mContext.getApplicationContext(), notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
//            alarmManager.setExact(AlarmManager.RTC_WAKEUP,fd, pendingIntent);
            Log.e("ALARM_TIME", String.valueOf(fd));
            int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
            if(alarmManager != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, fd, pendingIntent);
                    Log.e("VERSION_CODE", "MASHMALLOW");
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    alarmManager.setExact(ALARM_TYPE, fd, pendingIntent);
                    Log.e("VERSION_CODE", "KITKAT");
                } else {
                    alarmManager.set(ALARM_TYPE, fd, pendingIntent);
                    Log.e("VERSION_CODE", "BELOW KITKAT");
                }
            }

                SharedPreferences.Editor editor = sharedPreferences.edit();
                try {
                    JSONObject json = BundleJSONConverter.convertToJSON(bundle);
                    editor.putString(notificationIdString, json.toString());
                    editor.apply();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        }

这是我的取消代码:

 public void cancelAlarm(String notificationIdString) {
        Log.e(TAG, "cancelAlarm: " + notificationIdString );
        int notificationID = Integer.parseInt(notificationIdString);
        Intent intent = new Intent(mContext.getApplicationContext(), ANAlarmReceiver.class);
//        Log.e(TAG, "is this the same intent :" + notificationIntent.filterEquals(getComparableIntents(false,thisBundle,thisId)));

            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), notificationID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            pendingIntent.cancel();
            final AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
            am.cancel(pendingIntent);
    }

这是我从广播接收器手动重复setExact的方法:

 @Override
    public void onReceive(Context context, Intent intent) {
        anHelper = new ANHelper((Application)context.getApplicationContext());
        boolean stateApp = isAppOnForeground(context);
        Bundle bundle = intent.getBundleExtra("bundle");
        int id = intent.getIntExtra("notificationId", 0);
        Log.e(TAG, "bundle: " + bundle.getString("id"));
        Log.e(TAG, "stateApp: " + stateApp);
        Log.e(TAG, "notificationId " + String.valueOf(id));
        String tausiyah = "";
        String largeIcon = "";
        String suaraAdzan = "";
        String message = "";
        JSONObject obj = null;
            try {
                obj = BundleJSONConverter.convertToJSON(bundle);
                tausiyah = obj.getString("ticker");
                suaraAdzan = obj.getString("sound_name");
                largeIcon = obj.getString("large_icon");
                message = obj.getString("message");
                Log.e(TAG, "obj: " + obj);
//            Log.e(TAG, "notificationIdString: " + notificationIdString );
//            Intent alarmIntent = new Intent("android.intent.action.MAIN");
                Intent scheduledIntent = new Intent(context, AlertDialogAlarmClass.class);
                scheduledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setClass(context, AlertDialogAlarmClass.class);
                scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                scheduledIntent.putExtra("TEXT_SHOLAT", message);
                scheduledIntent.putExtra("TEXT_TAUSIYAH", tausiyah);
                scheduledIntent.putExtra("SOUND", suaraAdzan);
                scheduledIntent.putExtra("IMG", largeIcon);
                scheduledIntent.putExtra("stateApp", stateApp);
                context.startActivity(scheduledIntent);
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE, 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                Date date = calendar.getTime();
                String dateFormat = simpleDateFormat.format(date);
//            String dateFormat = simpleDateFormat.format(calendar.getTime());
                Log.e("DATEFORMAT", dateFormat);
                Log.e("FIREDATE", obj.getString("fire_date"));
                obj.put("fire_date", dateFormat);
                Log.e("FIREDATE", obj.getString("fire_date"));
                Bundle b = BundleJSONConverter.convertToBundle(obj);
                Log.e("BUNDLE", String.valueOf(b));
//            context.registerReceiver(this,new IntentFilter(Intent.ACTION_TIME_TICK));
                anHelper.setAlarm(b);
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e(TAG, "onReceive: " + e.getMessage());
            }


//        Intent service = new Intent(context, ANService.class);
//        service.putExtras(intent);
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            context.startForegroundService(service);
//        } else {
//            context.startService(service);
//        }
    }

我期望的结果是该警报不会在特定时间出现,但是即使我取消了警报,警报仍然会出现。

0 个答案:

没有答案