Alarm Manager Example

前端 未结 10 1392
情话喂你
情话喂你 2020-11-21 05:12

I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.

Can anyone help me with a basic alar

相关标签:
10条回答
  • 2020-11-21 05:52

    Here's an example with Alarm Manager using Kotlin:

    class MainActivity : AppCompatActivity() {
    
        val editText: EditText by bindView(R.id.edit_text)
        val timePicker: TimePicker by bindView(R.id.time_picker)
        val buttonSet: Button by bindView(R.id.button_set)
        val buttonCancel: Button by bindView(R.id.button_cancel)
        val relativeLayout: RelativeLayout by bindView(R.id.activity_main)
        var notificationId = 0
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            timePicker.setIs24HourView(true)
    
            val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    
            buttonSet.setOnClickListener {
                if (editText.text.isBlank()) {
                    Toast.makeText(applicationContext, "Title is Required!!", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    Calendar.getInstance().apply {
                        set(Calendar.HOUR_OF_DAY, timePicker.hour)
                        set(Calendar.MINUTE, timePicker.minute)
                        set(Calendar.SECOND, 0)
                    }.timeInMillis,
                    PendingIntent.getBroadcast(
                        applicationContext,
                        0,
                        Intent(applicationContext, AlarmBroadcastReceiver::class.java).apply {
                            putExtra("notificationId", ++notificationId)
                            putExtra("reminder", editText.text)
                        },
                        PendingIntent.FLAG_CANCEL_CURRENT
                    )
                )
                Toast.makeText(applicationContext, "SET!! ${editText.text}", Toast.LENGTH_SHORT).show()
                reset()
            }
    
            buttonCancel.setOnClickListener {
                alarmManager.cancel(
                    PendingIntent.getBroadcast(
                        applicationContext, 0, Intent(applicationContext, AlarmBroadcastReceiver::class.java), 0))
                Toast.makeText(applicationContext, "CANCEL!!", Toast.LENGTH_SHORT).show()
            }
        }
    
        override fun onTouchEvent(event: MotionEvent?): Boolean {
            (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                .hideSoftInputFromWindow(relativeLayout.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            relativeLayout.requestFocus()
            return super.onTouchEvent(event)
        }
    
        override fun onResume() {
            super.onResume()
            reset()
        }
    
        private fun reset() {
            timePicker.apply {
                val now = Calendar.getInstance()
                hour = now.get(Calendar.HOUR_OF_DAY)
                minute = now.get(Calendar.MINUTE)
            }
            editText.setText("")
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:56

    AlarmManager in combination with IntentService

    I think the best pattern for using AlarmManager is its collaboration with an IntentService. The IntentService is triggered by the AlarmManager and it handles the required actions through the receiving intent. This structure has not performance impact like using BroadcastReceiver. I have developed a sample code for this idea in kotlin which is available here:

    MyAlarmManager.kt

    import android.app.AlarmManager
    import android.app.PendingIntent
    import android.content.Context
    import android.content.Intent
    
    object MyAlarmManager {
    
        private var pendingIntent: PendingIntent? = null
    
        fun setAlarm(context: Context, alarmTime: Long, message: String) {
            val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    
            val intent = Intent(context, MyIntentService::class.java)
            intent.action = MyIntentService.ACTION_SEND_TEST_MESSAGE
            intent.putExtra(MyIntentService.EXTRA_MESSAGE, message)
    
            pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent)
        }
    
        fun cancelAlarm(context: Context) {
            pendingIntent?.let {
                val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
                alarmManager.cancel(it)
            }
        }
    
    }
    

    MyIntentService.kt

    import android.app.IntentService
    import android.content.Intent
    
    class MyIntentService : IntentService("MyIntentService") {
    
        override fun onHandleIntent(intent: Intent?) {
            intent?.apply {
                when (intent.action) {
                    ACTION_SEND_TEST_MESSAGE -> {
                        val message = getStringExtra(EXTRA_MESSAGE)
                        println(message)
                    }
                }
            }
        }
    
        companion object {
            const val ACTION_SEND_TEST_MESSAGE = "ACTION_SEND_TEST_MESSAGE"
            const val EXTRA_MESSAGE = "EXTRA_MESSAGE"
        }
    
    }
    

    manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.aminography.alarm">
    
        <application
            ... >
    
        <service
            android:name="path.to.MyIntentService"
            android:enabled="true"
            android:stopWithTask="false" />
    
        </application>
    
    </manifest>
    

    Usage:

    val calendar = Calendar.getInstance()
    calendar.add(Calendar.SECOND, 10)
    MyAlarmManager.setAlarm(applicationContext, calendar.timeInMillis, "Test Message!")
    

    If you want to to cancel the scheduled alarm, try this:

    MyAlarmManager.cancelAlarm(applicationContext)
    
    0 讨论(0)
  • 2020-11-21 05:56

    Alarm Manager:

    Add To XML Layout (*init these view on create in main activity)

      <TimePicker
        android:id="@+id/timepicker"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"></TimePicker>
    
      <Button
        android:id="@+id/btn_start"
        android:text="start Alarm"
        android:onClick="start_alarm_event"
        android:layout_width="match_parent"
        android:layout_height="52dp" />
    

    Add To Manifest (Inside application tag && outside activity)

     <receiver android:name=".AlarmBroadcastManager"
            android:enabled="true"
            android:exported="true"/>
    

    Create AlarmBroadcastManager Class(inherit it from BroadcastReceiver)

     public class AlarmBroadcastManager extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
          MediaPlayer mediaPlayer=MediaPlayer.create(context,Settings.System.DEFAULT_RINGTONE_URI);
          mediaPlayer.start();
        }
     }
    

    In Main Activity (Add these Functions):

     @RequiresApi(api = Build.VERSION_CODES.M)
     public  void start_alarm_event(View view){
        Calendar calendar=Calendar.getInstance();
        calendar.set(
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH),
        timePicker.getHour(),
        timePicker.getMinute(),
        0
        );
        setAlarm(calendar.getTimeInMillis());
     }
    
     public void setAlarm(long timeInMillis){
        AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent=new Intent(this,AlarmBroadcastManager.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this,0,intent,0);
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent);
        Toast.makeText(getApplicationContext(),"Alarm is Set",Toast.LENGTH_SHORT).show();
    
    
     }
    
    0 讨论(0)
  • 2020-11-21 05:57

    I have made my own implementation to do this on the simplest way as possible.

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    
    import junit.framework.Assert;
    
    /**
     * Created by Daniel on 28/08/2016.
     */
    public abstract class AbstractSystemServiceTask {
    
        private final Context context;
        private final AlarmManager alarmManager;
        private final BroadcastReceiver broadcastReceiver;
        private final PendingIntent pendingIntent;
    
        public AbstractSystemServiceTask(final Context context, final String id, final long time, final AlarmType alarmType, final BackgroundTaskListener backgroundTaskListener) {
    
            Assert.assertNotNull("ApplicationContext can't be null", context);
            Assert.assertNotNull("ID can't be null", id);
    
            this.context = context;
    
            this.alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
    
            this.context.registerReceiver(
                    this.broadcastReceiver = this.getBroadcastReceiver(backgroundTaskListener),
                    new IntentFilter(id));
    
            this.configAlarmManager(
                    this.pendingIntent = PendingIntent.getBroadcast(this.context, 0, new Intent(id), 0),
                    time,
                    alarmType);
        }
    
        public void stop() {
    
            this.alarmManager.cancel(this.pendingIntent);
            this.context.unregisterReceiver(this.broadcastReceiver);
        }
    
        private BroadcastReceiver getBroadcastReceiver(final BackgroundTaskListener backgroundTaskListener) {
    
            Assert.assertNotNull("BackgroundTaskListener can't be null.", backgroundTaskListener);
    
            return new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context context, Intent intent) {
    
                    backgroundTaskListener.perform(context, intent);
                }
            };
        }
    
        private void configAlarmManager(final PendingIntent pendingIntent, final long time, final AlarmType alarmType) {
    
            long ensurePositiveTime = Math.max(time, 0L);
    
            switch (alarmType) {
                case REPEAT:
                    this.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ensurePositiveTime, pendingIntent);
                    break;
                case ONE_TIME:
                default:
                    this.alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ensurePositiveTime, pendingIntent);
            }
        }
    
        public interface BackgroundTaskListener {
    
            void perform(Context context, Intent intent);
    
        }
    
        public enum AlarmType {
    
            REPEAT, ONE_TIME;
    
        }
    
    }
    

    The only next step, implement it.

    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    import ...AbstractSystemServiceTask;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * Created by Daniel on 28/08/2016.
     */
    public class UpdateInfoSystemServiceTask extends AbstractSystemServiceTask {
    
        private final static String ID = "UPDATE_INFO_SYSTEM_SERVICE";
        private final static long REPEAT_TIME = TimeUnit.SECONDS.toMillis(10);
        private final static AlarmType ALARM_TYPE = AlarmType.REPEAT;
    
        public UpdateInfoSystemServiceTask(Context context) {
    
            super(context, ID, REPEAT_TIME, ALARM_TYPE, new BackgroundTaskListener() {
    
                @Override
                public void perform(Context context, Intent intent) {
    
                    Log.i("MyAppLog", "-----> UpdateInfoSystemServiceTask");
    
                    //DO HERE WHATEVER YOU WANT...
                }
            });
    
            Log.i("MyAppLog", "UpdateInfoSystemServiceTask started.");
        }
    
    }
    

    I like to work with this implementation, but another possible good way, it's don't make the AbstractSystemServiceTask class abstract, and build it through a Builder.

    I hope it help you.

    UPDATED Improved to allow several BackgroundTaskListener on the same BroadCastReceiver.

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    
    import junit.framework.Assert;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * Created by Daniel on 28/08/2016.
     */
    public abstract class AbstractSystemServiceTask {
    
        private final Context context;
        private final AlarmManager alarmManager;
        private final BroadcastReceiver broadcastReceiver;
        private final PendingIntent pendingIntent;
    
        private final Set<BackgroundTaskListener> backgroundTaskListenerSet;
    
        public AbstractSystemServiceTask(final Context context, final String id, final long time, final AlarmType alarmType) {
    
            Assert.assertNotNull("ApplicationContext can't be null", context);
            Assert.assertNotNull("ID can't be null", id);
    
            this.backgroundTaskListenerSet = new HashSet<>();
    
            this.context = context;
    
            this.alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
    
            this.context.registerReceiver(
                    this.broadcastReceiver = this.getBroadcastReceiver(),
                    new IntentFilter(id));
    
            this.configAlarmManager(
                    this.pendingIntent = PendingIntent.getBroadcast(this.context, 0, new Intent(id), 0),
                    time,
                    alarmType);
        }
    
        public synchronized void registerTask(final BackgroundTaskListener backgroundTaskListener) {
    
            Assert.assertNotNull("BackgroundTaskListener can't be null", backgroundTaskListener);
    
            this.backgroundTaskListenerSet.add(backgroundTaskListener);
        }
    
        public synchronized void removeTask(final BackgroundTaskListener backgroundTaskListener) {
    
            Assert.assertNotNull("BackgroundTaskListener can't be null", backgroundTaskListener);
    
            this.backgroundTaskListenerSet.remove(backgroundTaskListener);
        }
    
        public void stop() {
    
            this.backgroundTaskListenerSet.clear();
    
            this.alarmManager.cancel(this.pendingIntent);
            this.context.unregisterReceiver(this.broadcastReceiver);
        }
    
        private BroadcastReceiver getBroadcastReceiver() {
    
            return new BroadcastReceiver() {
    
                @Override
                public void onReceive(final Context context, final Intent intent) {
    
                    for (BackgroundTaskListener backgroundTaskListener : AbstractSystemServiceTask.this.backgroundTaskListenerSet) {
    
                        backgroundTaskListener.perform(context, intent);
                    }
                }
            };
        }
    
        private void configAlarmManager(final PendingIntent pendingIntent, final long time, final AlarmType alarmType) {
    
            long ensurePositiveTime = Math.max(time, 0L);
    
            switch (alarmType) {
                case REPEAT:
                    this.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ensurePositiveTime, pendingIntent);
                    break;
                case ONE_TIME:
                default:
                    this.alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ensurePositiveTime, pendingIntent);
            }
        }
    
        public interface BackgroundTaskListener {
    
            void perform(Context context, Intent intent);
    
        }
    
        public enum AlarmType {
    
            REPEAT, ONE_TIME;
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题