Alarm uygulaması üzerinde çalışıyorum. Alarm bildirimi uygulamaya çalıştım ve her gün için bildirimler aldım ancak 7 gün boyunca alarm vermem gerekiyor. Yedi gün boyunca yayın alıcımda iptal ettim, ama yine de yedi gün sonra bildirimleri alıyorum.7 gün boyunca aynı anda alarmı ayarlamak mümkün mü?
public class MyTest extends AppCompatActivity {
AlarmManager alarmManager;
SharedPreferences preferences;
SharedPreferences.Editor editor;
PendingIntent pendingIntent;
int RequestCode =777;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AlarmReciever alarm = new AlarmReciever();
// alarm.setAlarm(this);
int alarmId = 0;
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(MyTest.this, AlarmReciever.class); // AlarmReceiver1 = broadcast receiver
alarmIntent.putExtra("alarmId", alarmId);
pendingIntent = PendingIntent.getBroadcast(MyTest.this, RequestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// alarmIntent.setData((Uri.parse("custom://" + System.currentTimeMillis())));
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 00);
alarmStartTime.set(Calendar.MINUTE, 00);
alarmStartTime.set(Calendar.SECOND, 0);
Log.d("Alarm", now.toString());
/// int count= 0;
// last i added for comparision
if (now.after(alarmStartTime)) {
//second
Log.d("Alarm", "Added a day");
alarmStartTime.add(Calendar.DATE, 1);
// count++;
}
/* System.out.println(count);
if(count==7){
alarmManager.cancel(pendingIntent);
}*/
// for(int i=0;i<7;i ++) {
// alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis() + 1000 * 60 * 60 * 24 /*AlarmManager.INTERVAL_DAY*/, pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Alarm", "Alarms set for everyday 8 am.");
// }
}
}
Yayın Alıcısı
public class AlarmReciever extends BroadcastReceiver {
SharedPreferences preferences;
SharedPreferences.Editor editor;
int count=0;
AlarmManager alarmManager;
PendingIntent pendingIntent;
int RequestCode =777;
@Override
public void onReceive(Context context, Intent intent) {
// String rec = intent.getDataString();
// Log.d("Alarm",rec);
Intent service1 = new Intent(context, NotificationService.class);
// service1.setData((Uri.parse("custom://" + System.currentTimeMillis())));
context.startService(service1);
count++;
preferences = context.getSharedPreferences("MyCount", Context.MODE_PRIVATE);
editor=preferences.edit();
editor.putInt("COUNT", count);
editor.commit();
incrementSum(context);
}
public void incrementSum(Context context){
int cou = preferences.getInt("COUNT",0);
// System.out.println(cou);
// int c=0;
// int cou = preferences.getInt("COUNT",0);
int co = preferences.getInt("C",0);
if(cou==1){
cou=co+1;
editor = preferences.edit();
editor.putInt("C",cou);
editor.commit();
}
int c = preferences.getInt("C",0);
// System.out.println("count"+cou);
// int count = preferences.getInt("COUNT",0);
// System.out.println("last"+count);
System.out.println("sum" + c);
if(c>=7) {
Intent alarmIntent = new Intent(context, AlarmReciever.class);
pendingIntent = PendingIntent.getBroadcast(context, RequestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager!= null) {
alarmManager.cancel(pendingIntent);
}
// alarmManager.cancel(pendingIntent);
}
// AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
}
Bildirim hizmet kümesi Alarmı İçin
public class NotificationService extends IntentService {
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
public NotificationService() {
super("testing.amaze.com.mytest");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
//.setSmallIcon(R.drawable.ic_launcher)
//.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
// .setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif", "Notifications sent.");
}
Lütfen kodunuzu paylaşın. – bwegs
, alarmı tetiklemeyi denediğinde alarmı iptal edip "System.currentTimeInmillis + öğütme işleminde 7 gün içinde saklayacak olan Paylaşılan Tercihte uzun bir değişken değişkeni" kaydedebilirsiniz. – kishorepatel
alarmı http://stackoverflow.com/questions/14485368/delete-alarm-from-alarmmanager-using-cancel-android ve bu http://stackoverflow.com/questions/3330522/how-to-cancel iptal etmek için okuyun -bu yinelenen alarm – kishorepatel