How to set alarm for every 10 minutes in Android Application?

好久不见. 提交于 2020-03-06 02:35:05

问题


Hai am trying to set alarm for every 10 minutes.But its running first time only any Body kindly help me

Intent intent = new Intent(this, ConnectionReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1 * 1000),   pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();//every 10 minutes i want to print the toast

回答1:


use alarmManager.setRepeating method

follow this link on developer site

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html

http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)




回答2:


    Calendar calCurrent = Calendar.getInstance();
    long tenmin = 10 * 60 * 1000;
    int mynotifyidis = Integer.parseInt(mydelid);
    Intent mIntent = new Intent(this, NotificationService1.class);
    mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mIntent.putExtra("id", mynotifyidis);

    AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent snoozependingintent = PendingIntent.getService(this,
            -mynotifyidis, mIntent, PendingIntent.FLAG_ONE_SHOT);

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            calCurrent.getTimeInMillis() + fivemin, fivemin,
            snoozependingintent);


来源:https://stackoverflow.com/questions/8986964/how-to-set-alarm-for-every-10-minutes-in-android-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!