Difficulties when implementing Alarm Manager in Android Studio

How to use AlarmManager in Android Studio I will introduce it based on the points that I stumbled upon when I was writing the code.

* This is Qiita's first post, there may be some strange or different points, but please try a lot ... </ font>

How to set AlarmManager

This time, we will create a code that will be processed in a few minutes (seconds, hours, etc ...). First, let's see how to set AlarmManager.

Important! </ font>

1: Add settings to Android Manifest.

AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"

<uses-permission android:name="android.permission.WAKE_LOCK" />

   <application

        <receiver
            android:name=".Class that receives AlarmManager."
            android:process=":remote" >
        </receiver>
   </application>
</manifest>

Android Manifest ʻuses-permission android: name =" android.permission.WAKE_LOCK " Andreceiver android: name =". Class that receives AlarmManager " AlarmManager works by writing android: process = ": remote" `.

I had a hard time forgetting to write here. The official website wasn't even at Alarm Manager, so ...

2: Create an Intent to send AlarmManager

Where you want to put AlarmManager.java


    ...
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context,Where to receive AlarmManager.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP,Set your favorite time in "milliseconds!", pendingIntent );

Use Intent to set how many minutes later the data will be sent. The context in the above three codes is oftengetApplicationContext (). The second argument on the last line is often in milliseconds, so it is recommended to write it in milliseconds.

3: Create a class that extends BroadcastReceiver

Where to receive AlarmManager.java



//Inherit BoardcastReceiver
where to receive the public class AlarmManager extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
     //Write the process you want to create
    }
}

After inheriting BroadcastReceiver, create an onReceive method and create a process there, and you're done.

Thank you for watching till the end. It's the first time, so I'm sorry if something is wrong.

I also do twitter, so I would appreciate it if you could follow me. https://twitter.com/tomfumy_dev

Recommended Posts