Domanda

I want to create notifications on multiple specific dates & time, which are stored in database. I can get Notifications on correct date and time, but what i noticed later is, that i also got notifications randomly on the next day. They keep cropping up whenever i restart the emulator.

So, it seems that i can't stop the Alarm manager or Broadcast receiver. I've tried with providing pendingIntent to alarmmanager's cancel method to no avail. I also use toggle button to enable/disable notification, but it has no effect

Below is my code.

MainActivity.java

PendingIntent pendingIntent;
    AlarmManager alarmManager;
    Button set, cancel;
    MTPAdapter db;
    ArrayList<TourPlan> arrDates;

    ToggleButton toggle;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        toggle = (ToggleButton) findViewById(R.id.toggleButton1);

                // This function fetched data from DB
        fetchDates();



        set = (Button) findViewById(R.id.the_button);
        cancel = (Button) findViewById(R.id.the_button_cancel);
        cancel.setEnabled(false);



        toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    Toast.makeText(getApplicationContext(), "Set",
                            Toast.LENGTH_SHORT).show();
                    setEvent();
                } else {
                    alarmManager.cancel(pendingIntent);
                    MyReceiver.stopService(getApplicationContext());
                }
            }
        });
    } // end onCreate

    public void setEvent() {

        for (int i = 0; i < arrDates.size(); i++) {
            // int id = arrDates.get().getId();

            int year = Integer.parseInt(arrDates.get(i).getYear());

            int month = Integer.parseInt(arrDates.get(i).getMonth());

            int date = Integer.parseInt(arrDates.get(i).getDate());

            int hour = Integer
                    .parseInt(firstStringer(arrDates.get(i).getTime()));
            Log.v("Hour : ", "" + hour);

            int minute = Integer.parseInt(stringer(arrDates.get(i).getTime()));
            Log.v("minute : ", "" + minute);
            int second = 00;
            startAlarm(i, year, month, date, hour, minute, second);
        }
    }

    public void startAlarm(int id, int year, int month, int date, int hour,
            int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, date);

        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        // calendar.set(Calendar.AM_PM, Calendar.PM);

        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, id,
                myIntent, 0);

        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
                pendingIntent);
    }


    }

MyAlarmService.java

public class MyAlarmService extends Service {


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);



        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("ISFA")
                .setStyle(
                        new NotificationCompat.BigTextStyle()
                                .bigText("Tour Planned for the Day"))
                .setContentText("You have planned a tour for today")
                .setAutoCancel(true);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

MyReceiver.java (Broadcast receiver) :

  public class MyReceiver extends BroadcastReceiver {

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

                Intent service1 = new Intent(context, MyAlarmService.class);
                context.startService(service1);

            }

            public static void stopService(Context context) {
                Intent stopServiceIntent = new Intent(context, MyAlarmService.class);
                context.stopService(stopServiceIntent);
            }
        }

Manifest file:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

In Application tags,

 <service
           android:name="com.example.brodcastalarm.MyAlarmService"
           android:enabled="true" />
   <!-- THE BROADCAST RECIEVER WHICH MANAGES THE BROADCAST FOR NOTIFICATION -->
            <receiver android:name="com.example.brodcastalarm.MyReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>

This is the Database structure. I use subString methods to get hour and minutes.

This is Database Sample

È stato utile?

Soluzione

Alarm manager needs to be manually disabled, so for doing so use code below, it will disable the alarm.

AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);         
Intent intent = new Intent(getBaseContext(), YourScheduleClass.class);      
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent,              PendingIntent.FLAG_UPDATE_CURRENT);         
aManager.cancel(pIntent);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top