I have created a repeating daily notification. Everything works fine but when my checkbox is unchecked it cancel the alarm for that day and fire the notification next day for the same time.What i want is to cancel alarm if my checkbox is unchecked until i will checked it again.
This is my Setting Activity class.Here,I have set the time on clicking the checkbox.And i have called cancelAlarm method when the checkbox is unchecked.
public class SettingActivity extends AppCompatActivity {
public static int hour;
public static int minute;
private boolean check = false;
private int hour_1,minute_1;
static final int TIME_DIALOG_ID = 1111;
final Calendar c = Calendar.getInstance();
DatabaseAdapter MyHelper;
SharedPreferenceManager sharedPreferenceManager;
TextView textView_clearAllData;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
textView_clearAllData = (TextView)
findViewById(R.id.textView_clearAllData);
MyHelper=new DatabaseAdapter(getApplicationContext());
context=this;
sharedPreferenceManager = new SharedPreferenceManager();
if(
Boolean.valueOf(sharedPreferenceManager.getPreferences
(SettingActivity.this,"check")))
{
CheckBox box = (CheckBox)
findViewById(R.id.checkBox_settingClassNotification);
box.setChecked(true);
}
hour_1 = c.get(Calendar.HOUR_OF_DAY);
minute_1 = c.get(Calendar.MINUTE);
}
public void onClick_dailyClassNotification(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.checkBox_settingClassNotification:
if (checked) {
// ((CheckBox) view).setChecked(false); //initially
checkbox wont be checked
showDialog(TIME_DIALOG_ID);
}
else
{
check = false;
sharedPreferenceManager.setPreferences
(SettingActivity.this,"check",String.valueOf(check));
Notification_service.cancelAlarm(context);
Toast.makeText(SettingActivity.this, "Notification
Cancelled" ,Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
TimePickerDialog myTime = new TimePickerDialog(this,
timePickerListener,
hour_1, minute_1, false);
myTime.setOnCancelListener(new
DialogInterface.OnCancelListener(){
CheckBox checkBox = (CheckBox)
findViewById(R.id.checkBox_settingClassNotification);
public void onCancel(DialogInterface dialog) {
//Log.d("IN HERE","HERE2");
checkBox.setChecked(false);
sharedPreferenceManager.setPreferences
(SettingActivity.this,"check",String.valueOf(check));
//checkBox.setText("Use reminders");
}
});
return myTime;
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int
minutes) {
hour = hourOfDay;
minute = minutes;
sharedPreferenceManager.setPreferences
(SettingActivity.this,"hour",Integer.toString(hour));
sharedPreferenceManager.setPreferences
(SettingActivity.this,"minute",Integer.toString(minute));
//Making checkbox checked and svaing in sharedPreference
CheckBox ch =
(CheckBox)findViewById(R.id.checkBox_settingClassNotification);
ch.setChecked(true);
check = true;
sharedPreferenceManager.setPreferences
(SettingActivity.this,"check",String.valueOf(check));
createNotify();
Toast.makeText(SettingActivity.this, "Notification Enabled
at " + hour + ":" + minute, Toast.LENGTH_SHORT).show();
}
};
public void createNotify() {
Intent in = new Intent(this,Notification_service.class);
startService(in);
}
//clear All Data
public void clearAllData() {
long id = MyHelper.clearAllData();
if(id<0)
Message.message(getApplicationContext(),"Error");
else
Message.message(getApplicationContext(),"All Data Cleared !");
}
public void createDeleteDialog(View view){
final AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(context);
dialogBuilder.setTitle("Delete All Data ?");
dialogBuilder.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
clearAllData();
}
});
dialogBuilder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
This is my Notification service class where i have set daily alarm and cancel it with the same request Id.
public class Notification_service extends Service {
SharedPreferenceManager sharedPreferenceManager;
private static final String TAG = "MyService";
int hour,minute;
/**
* The started service starts the AlarmManager.
*/
public static AlarmManager alarmManager;
public static PendingIntent pendingIntent;
@Override
public void onStart(Intent intent, int startid) {
sharedPreferenceManager = new SharedPreferenceManager();
hour = Integer.parseInt(sharedPreferenceManager.getPreferences(Notification_service.this,"hour"));
minute = Integer.parseInt(sharedPreferenceManager.getPreferences(Notification_service.this,"minute"));
//the hour and minute variables came from setting activity. Use wherever required.
long _alarm ;
Calendar now = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,hour);
c.set(Calendar.MINUTE,minute);
c.set(Calendar.SECOND,0);
if(c.getTimeInMillis() <= now.getTimeInMillis())
_alarm = c.getTimeInMillis() + (24*60*60*1000);
else
_alarm = c.getTimeInMillis();
Intent i = new Intent(this, Notification_Receiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/*PendingIntent*/ pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),100,i,PendingIntent.FLAG_UPDATE_CURRENT);
/*AlarmManager*/ alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,AlarmManager.INTERVAL_DAY,pendingIntent);
// Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
// Log.d(TAG, "onStart");
}
//cancel alarm
public static void cancelAlarm(Context context) {
/*if(alarmManager != null)
alarmManager.cancel(pendingIntent);*/
Intent i = new Intent(context, Notification_Receiver.class);
pendingIntent=PendingIntent.getBroadcast(context,100,i,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager=(AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
//Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
// Log.d(TAG, "onDestroy");
}
}
This is my Notification Receiver class where i have set the Notification.
public class Notification_Receiver extends BroadcastReceiver {
private Uri soundURI =
Uri.parse("android.resource://com.example.homedemo/"
+ R.raw.gets_in_the_way);
private long[] mVibratePattern = { 0, 200, 200, 300 };
/*String[] day = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};*/
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager= (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
/*Calendar calendar = Calendar.getInstance();
int d = calendar.get(Calendar.DAY_OF_WEEK);
Log.d("day = ",Integer.toString(d));
*/
Intent repeating_intent=new Intent(context,HomeActivity.class);
/*Intent repeating_intent = new Intent(context,RoutineOfTheDayActivity.class);
String day_str = day[d];
repeating_intent.putExtra("day",day_str);
*/
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.
getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Your today's classes").setContentText("Touch here to see schedule")
.setAutoCancel(true).setSmallIcon(android.R.drawable.arrow_up_float)
.setSound(soundURI)
.setVibrate(mVibratePattern);
notificationManager.notify(100,builder.build());
}
}
Aucun commentaire:
Enregistrer un commentaire