Ginso
Ginso

Reputation: 569

AlarmManager isn't working

I know there are a lot of questions like this, but none of the solutions helped me. I try to schedule a BroadcastReciever using the AlarmManager. For now, all it should do is make a Toast and write something to a file. Here is my code, i get neither the toast "alarm" nor is the logfile created. The Toast of the Mainactivity is shown. Any Ideas?

Here is the Manifest,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.alarmtest" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReciever"/>
        <service android:enabled="true" android:name=".MyService" />
    </application>

</manifest>

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        long time = new GregorianCalendar(2015,12,4,9,44).getTimeInMillis();

        Intent intentAlarm = new Intent(this, AlarmReciever.class);
        intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
    }

}

and the Reciever

public class AlarmReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"alarm",Toast.LENGTH_LONG);
        log("alarm");
    }

    public void log(String s) {
        Log.e("STATE", s);
        File f = new File(Environment.getExternalStorageDirectory(),"log.txt");
        try {
            if (!f.exists()) f.createNewFile();
            FileWriter fileWriter = new FileWriter(f, true);
            fileWriter.write(s+"\n");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Views: 128

Answers (2)

Hiren Patel
Hiren Patel

Reputation: 52800

Can you please try this one ?

int trigger_time= System.currentTimeMillis() + 30 * 1000;   

if(Build.VERSION.SDK_INT >= 19) {
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, trigger_time, pendingIntent);
 } else {
     alarmManager.set(AlarmManager.RTC_WAKEUP, trigger_time, pendingIntent);
 }

Edit:

Difference between setRepeating and setInexactRepeating:

Decide how precise your alarm needs to be

Choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be.

For most apps, setInexactRepeating() is the right choice. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. This reduces the drain on the battery.

For the rare app that has rigid time requirements as example, the alarm needs to fire precisely at 12:00 p.m. everyday then use setRepeating().

Hope this will help you.

Upvotes: 1

Junaid
Junaid

Reputation: 7860

From the Docs : Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.

So what Hiren Patel has stated in his answer, will work for you.

Please tell us more about your test environment.

Upvotes: 1

Related Questions