Alarm scheduling every 2 minutes of android

In activity class

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis(),2000, pendingIntent); } 

And my onrecieve function in alarmreciever class

  @Override public void onReceive(Context context, Intent intent) { //get and send location information System.out.println("fired"); } 

I am using nexus 4, kitkat version. I do not see any triggering function working every 2 minutes. Ntg going on ... any help? thanks

  <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alarmexample" android:versionCode="1" android:versionName="1.0" > 
 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.AlarmExample" android:exported="false" > </receiver> </application> </manifest> 

I just put my manifest ............................................. ....

+6
source share
2 answers

In your setRepeating function, you must use SystemClock.elapsedRealTime () for ELAPSED_REALTIME_WAKEUP. In addition, you need to change 2000 to 2 * 60 * 1000 to indicate the time of your interval.

 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 2*60*1000, pendingIntent); 

Hope this helps.

Link: ELAPSED_REALTIME_WAKEUP

EDIT: There is a typo in the name of your receiver in your manifest file. Change ".AlarmReciever" to ".AlarmReceiver".

 <receiver android:name=".AlarmReceiver" android:exported="true" > </receiver> 
+8
source

in your code you set the alarm this way

 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 2000, pendingIntent); 

The time interval is incorrect to run every two minutes you should write:

 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 1000 * 60 * 2, pendingIntent); 

EDIT

for your flag set intent PendingIntent.FLAG_UPDATE_CURRENT and see if it changes anything.

 PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
+3
source

Source: https://habr.com/ru/post/977236/


All Articles