警报服务通知不会取消

时间:2016-05-19 09:09:41

标签: java android notifications alarmmanager

我正在尝试构建此应用程序,该应用程序在一个时间间隔内检查文件夹大小,如果文件夹大小比用户设置的大,则会显示通知。问题是当用户禁用通知时,它仍然继续通知(" ShowNotif"被调用),我做错了什么?

ScheduleFragment.java

public class ScheduleFragment extends Fragment {

SharedPreferences.Editor editor;
AlarmManager manager;
PendingIntent pendingIntent;
SharedPreferences share;
EditText telegSizeText;
CheckBox telegramSize;
private boolean telegramSizeIsSet = false;
CheckBox notif;

public ScheduleFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_schedule, container, false);

    telegramSize = (CheckBox)view.findViewById(R.id.telegramSize);
    telegSizeText = (EditText)view.findViewById(R.id.telegSizeText);
    notif = (CheckBox) view.findViewById(R.id.notif);

    share = getActivity().getSharedPreferences("setting", 0);
    editor = this.share.edit();

    telegramSize.setChecked(share.getBoolean("telegram_selected", false));
    telegramSizeIsSet = true;
    int var2 = share.getInt("telegram_size", -1);
    if(var2 != -1) {
        telegSizeText.setText(String.valueOf(var2));
    } else {
        telegSizeText.setText("");
    }


    this.telegramSize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton var1, boolean var2) {
            if (telegramSizeIsSet) {
                if(!var2) {
                    manager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
                    Intent var11 = new Intent(getActivity(), ShowNotif.class);
                    pendingIntent = PendingIntent.getBroadcast(getActivity(), 1,var11,PendingIntent.FLAG_UPDATE_CURRENT);
                    PendingIntent pendingIntentNoti =  PendingIntent.getBroadcast(getActivity(), 1, var11,PendingIntent.FLAG_UPDATE_CURRENT);
                    manager.cancel(pendingIntent);
                    manager.cancel(pendingIntentNoti);
                    Toast.makeText(getContext(), "Disabled!", Toast.LENGTH_SHORT).show();
                    editor.putBoolean("telegram_selected", false);
                    editor.putInt("telegram_size", -1);
                    telegSizeText.setText("");
                    editor.commit();
                    return;
                }

                if(telegSizeText.getText().toString() == null || telegSizeText.getText().toString().equals("")) {
                    Toast.makeText(getContext(), "Can't be empty", Toast.LENGTH_SHORT).show();
                    telegramSizeIsSet = false;
                    telegramSize.setChecked(false);
                    telegramSizeIsSet = true;
                    return;
                }

                Intent var9 = new Intent(getActivity(), CheckTelegramSizeReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, var9, 0);
                manager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
                manager.cancel(pendingIntent);
                editor.putInt("telegram_size", Integer.valueOf(telegSizeText.getText().toString()).intValue());
                editor.putBoolean("telegram_selected", true);
                editor.commit();
                manager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
                manager.setRepeating(0, System.currentTimeMillis(), (long) 10000, pendingIntent); //check every 10 seconds
                Toast.makeText(getContext(), "Enabled!", Toast.LENGTH_SHORT).show();
            }

        }
    });

    return view;
}}

CheckTelegramSizeReceiver.java

public class CheckTelegramSizeReceiver extends BroadcastReceiver {

private AlarmManager manager;
private AlarmManager manager2;
private AlarmManager manager3;
private boolean notificationCanceled;
boolean notificationCreated = false;
String path;
private PendingIntent pendingIntent;
private PendingIntent pendingIntent2;
long totalSizes = 0L;
int userSelectedSize = 0;


private long getFileSize(String var1) {
    long var2 = 0L;
    File[] var4 = (new File(var1)).listFiles();
    if (var4 != null) {
        for (int var5 = 0; var5 < var4.length; ++var5) {
            File var6 = var4[var5];
            if (var6.isDirectory()) {
                var2 += this.getFileSize(var6.getPath());
            } else {
                var2 += var6.length();
            }
        }
    }

    return var2;
}

public void onReceive(final Context var1, Intent var2) {
    File var3 = Environment.getExternalStorageDirectory();
    this.path = var3 + "/Telegram";
    (new AsyncTask<Void, Integer, Integer>() {
        protected Integer doInBackground(Void... var1x) {
            totalSizes = getFileSize(path) / 1048576L;
            //
            SharedPreferences var2 = var1.getSharedPreferences("setting", 0);
            userSelectedSize = var2.getInt("telegram_size", -1);
            return 1;
        }

        protected void onPostExecute(Integer var1x) {
            SharedPreferences var2 = var1.getSharedPreferences("setting", 0);
            Editor var3 = var2.edit();
            CheckTelegramSizeReceiver.this.userSelectedSize = var2.getInt("telegram_size", -1);
            CheckTelegramSizeReceiver.this.notificationCreated = var2.getBoolean("notificationIsShowing", false);
            CheckTelegramSizeReceiver.this.notificationCanceled = var2.getBoolean("notificationIsShowingCanceled", false);
            if ((int) CheckTelegramSizeReceiver.this.totalSizes > CheckTelegramSizeReceiver.this.userSelectedSize && !CheckTelegramSizeReceiver.this.notificationCreated) {
                Intent var11 = new Intent(var1, ShowNotif.class);
                CheckTelegramSizeReceiver.this.pendingIntent = PendingIntent.getBroadcast(var1, 1, var11, 0);
                CheckTelegramSizeReceiver.this.manager2 = (AlarmManager) var1.getSystemService(Context.ALARM_SERVICE);
                CheckTelegramSizeReceiver.this.manager2.setRepeating(0, System.currentTimeMillis(), (long) 28800000, CheckTelegramSizeReceiver.this.pendingIntent);
                var3.putBoolean("notificationIsShowing", true).commit();
                var3.putBoolean("notificationIsShowingCanceled", false).commit();
                CheckTelegramSizeReceiver.this.notificationCreated = true;
                CheckTelegramSizeReceiver.this.notificationCanceled = false;
            }

            if ((int) CheckTelegramSizeReceiver.this.totalSizes < CheckTelegramSizeReceiver.this.userSelectedSize && CheckTelegramSizeReceiver.this.notificationCreated && !CheckTelegramSizeReceiver.this.notificationCanceled) {
                System.out.println("test");
                Intent var5 = new Intent(var1, ShowNotif.class);
                CheckTelegramSizeReceiver.this.pendingIntent = PendingIntent.getBroadcast(var1, 1, var5, 0);
                CheckTelegramSizeReceiver.this.manager2 = (AlarmManager) var1.getSystemService(Context.ALARM_SERVICE);
                CheckTelegramSizeReceiver.this.manager2.cancel(CheckTelegramSizeReceiver.this.pendingIntent);
                CheckTelegramSizeReceiver.this.notificationCreated = false;
                CheckTelegramSizeReceiver.this.notificationCanceled = true;
                var3.putBoolean("notificationIsShowingCanceled", true).commit();
                var3.putBoolean("notificationIsShowing", false).commit();
            }

        }

    }).execute(null, null, null);
}}

ShowNotif.java

public class ShowNotif extends BroadcastReceiver {

public void onReceive(Context var1, Intent var2) {
    int var3 = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    if (var3 <= 22 && var3 >= 10) {
        Uri var4 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Bitmap var5 = BitmapFactory.decodeResource(var1.getResources(), R.drawable.icon_notif);
        NotificationCompat.Builder var6 = (new NotificationCompat.Builder(var1)).setSmallIcon(R.drawable.icon_notif).setLargeIcon(var5).setContentTitle("My App").setContentText("Notification Text").setSound(var4).setAutoCancel(true);
        var6.setContentIntent(PendingIntent.getActivity(var1, 0, new Intent(var1, MainActivity.class), 0));
        ((NotificationManager) var1.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, var6.build());
    }

}}

提前致谢。

1 个答案:

答案 0 :(得分:0)

if (telegramSizeIsSet) {
            if(!var2) {
                Intent var3 = new Intent(getActivity(), CheckTelegramSizeReceiver.class);

                manager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);

                pendingIntent = PendingIntent.getBroadcast(var1, 1,var11,PendingIntent.FLAG_UPDATE_CURRENT);
                pendingIntentNoti =  PendingIntent.getBroadcast(var1, 1, var11,PendingIntent.FLAG_UPDATE_CURRENT);
                manager.cancel(pendingIntent);
                manager.cancel(pendingIntentNoti);
                Toast.makeText(getContext(), "Disabled!", Toast.LENGTH_SHORT).show();
                editor.putBoolean("telegram_selected", false);
                editor.putInt("telegram_size", -1);
                telegSizeText.setText("");
                editor.commit();
                return;
            }   

基本上你只需取消CheckTelegramSizeReceiver接收器的警报。但是你忘记取消由ShowTelegramSizeReceiver接收器设置的ShowNoti接收器的警报。所以ShowNoti接收器一次又一次地显示通知所以你需要取消ShowNoti alaram并使用FLAG_UPDATE_CURRENT而不是0.谢谢它可以帮到你!