Reputation: 77
I need to programme alarm. To achieve this,i decided to use class AlarmManager. But when i build and run my app the first time alarm triggerafter about a minute,but it must trigger immediately. When i open this app again,it works well,so the alarm trigger immediately. Help me please,How i can programme,to alarm triggered even if i run my app the first time after booting the device. May be instead of using AlarmManager for alarm the best way to use another classes,such as timer,countDownTimer,etc? Help me please in it. I catch alarm,using receiver. I tryed to work with receiver in onCreate() method of activity,but it not help for me. It also not help,when i use setExact method instead of set method in AlarmManager. Repeating of alarm trigger inexact even when i run my app after running it the first time after booting the device. Here is my code of mainActivity,AlarmReceiver and manifest. Thanks everybody for help. MainActivity.java
package ru.AlexandrKozlovskiy.alarm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import ru.AlexandrKozlovskiy.alarm.AlarmReceiver;
import ru.AlexandrKozlovskiy.alarm.AlarmReceiver;
import ru.AlexandrKozlovskiy.alarm.R;
public class MainActivity extends AppCompatActivity {
public static AlarmReceiver alarm;
@Override
protected void onStart() {
super.onStart();
alarm = new AlarmReceiver();
if(alarm!=null) alarm.setAlarm(this.getApplicationContext(),System.currentTimeMillis(),5000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
// cancel alarm and free of resources
alarm.cancelAlarm(this.getApplicationContext());
alarm=null;
}
}
AlarmReceiver.java
package ru.AlexandrKozlovskiy.alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// keep screen on
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"alarm");
wl.acquire();
// show notification and generate tone during alarm.
Toast.makeText(context,"Testing of alarm",3).show();
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC,ToneGenerator.MAX_VOLUME);
toneGenerator.startTone(toneGenerator.TONE_CDMA_ALERT_CALL_GUARD,500);
toneGenerator.release();
wl.release();
}
public void setAlarm(Context context,long timeInMilis,long intervalInMilis)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(context, AlarmReceiver.class);
PendingIntent pi= PendingIntent.getBroadcast(context,0, intent,0);
// set usual or repeating alarm according the parameter intervalInMilis
if (intervalInMilis>0) am.setRepeating(AlarmManager.RTC_WAKEUP,timeInMilis,intervalInMilis,pi); else am.set(AlarmManager.RTC_WAKEUP,timeInMilis,pi);
}
public void cancelAlarm(Context context)
{
Intent intent=new Intent(context, AlarmReceiver.class);
PendingIntent sender= PendingIntent.getBroadcast(context,0, intent,0);
AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.AlexandrKozlovskiy.alarm">
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleInstance"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Upvotes: 0
Views: 115
Reputation: 211
As noted HERE:
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
Upvotes: 0