PDA

View Full Version : مشکل در alarmmanager و Calendar



arash ghafori
یک شنبه 03 خرداد 1394, 01:58 صبح
با سلام.
دوستان من در حال نوشتن یک task reminder هستم. طرز کارم به این صورته که میام موقعی که کاربر یه job رو ثبت میکنه همون موقع زمان و تاریخی که کاربر انتخاب کرده رو set میکنم به Calendar. بعد به آلارم منیجرم میگم که هر وقت به این زمان و تاریخ رسیدی یه notification بفرست.

مشکلم اینه که "هر تایمی" ست میکنم روی time picker به محض زدن دکمه ی ثبت کار notification رو میده !! در صورتی که من مثلا بهش یه زمان دیگه ای رو دادم !

MainActivity :

btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "Button clicked.");
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();


String time = timePicker.getCurrentHour().toString() +":"+ timePicker.getCurrentMinute();
PersianCalendar date = datePicker.getDisplayPersianDate();


work.setTitle(title);
work.setDescription(description);
work.setTime(time);
work.setDate(date.getPersianShortDate());
work.setFlag(0);
dbHandler.insertWork(work);


MiladiDate MDate = new MiladiDate();
String miladi = MDate.todayMiladi(date.getPersianYear(), date.getPersianMonth(), date.getPersianDay());
DateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
try {
Date date1 = format.parse(miladi);


calendar.set(Calendar.MONTH, date1.getMonth());
calendar.set(Calendar.YEAR, date1.getYear());
calendar.set(Calendar.DAY_OF_MONTH, date1.getDay());


calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);


myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1, myIntent, 0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);


} catch (ParseException e) {
e.printStackTrace();
}


editTextTitle.setText("");
editTextDescription.setText("");




}
});


MyReceiver :

public class MyReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);


}
}


MyAlarmService :

public class MyAlarmService extends Service {




private NotificationManager mManager;


@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);


mManager = (NotificationManager) this.getApplicationContext().getSystemService(this .getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.c lass);


Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);


PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationConte xt(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicatio nContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);


mManager.notify(0, notification);
}


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


ببخشید یکم طولانی شد!
ممنون میشم راهنمایی کنید.

#root#
یک شنبه 03 خرداد 1394, 07:06 صبح
تاریخ رو چک کردید؟
قبل از این خط :

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

این تاریخ calendar.getTimeInMillis رو با Log چاپ کنید و بررسی کنید، اگر تاریخ مربوط به قبل از زمان حال باشه AlarmManager همون موقع اجراش میکنه.

نکته دیگه، اگر کار سرویس تنها ایجاد Notif هست، استفاده از سرویس اضافه کاری محسوب میشه، تو همون Broadcast اعلان رو ایجاد کنید.
استفاده از سرویس برای کارهایی که زمان بر هستن مناسب هست.

arash ghafori
یک شنبه 03 خرداد 1394, 12:25 عصر
ممنون. مشکل حل شد.
مشکل جدید ب وجود اومد.

برنامه در حالت عادی کار میکنه ! اما وقتی رم رو بوست میکنم در
notification اختلال ایجاد میشه !!!
انگار با بوست کردن رم سرویس من بسته میشه و بعد از چند ثانیه دوباره باز میشه !!! و در کارش اختلال به وجود میاد !

راه حلی به نظرتون میرسه ؟؟
ممنون