12

In my 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 don't see any onreceive function fired every 2 minutes.nthg is happening... any help? thank you

 <?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 as well. ................................................

user3278732
  • 1,576
  • 9
  • 26
  • 60

2 Answers2

16

In your setRepeating function, you should use SystemClock.elapsedRealTime() for ELAPSED_REALTIME_WAKEUP. Also, you need to change 2000 to 2*60*1000 to specify your interval time.

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

Hope this helps.

Reference: ELAPSED_REALTIME_WAKEUP

EDIT: In your manifest file, there is a typo in your receiver name. Change ".AlarmReciever" to ".AlarmReceiver".

<receiver
    android:name=".AlarmReceiver"
    android:exported="true" >
</receiver>
stevo.mit
  • 4,341
  • 4
  • 34
  • 46
sam
  • 2,702
  • 1
  • 15
  • 29
  • its not working, my project is here https://www.wetransfer.com/downloads/8529755c58f64f5f9f5767be664893ce20141026134802/1395db476afa6c786d656162a5c140f420141026134802/5beafe if u wanna check it – user3278732 Oct 26 '14 at 13:59
  • @user3278732 I checked out your project and found out a typo in your manifest file. I just updated my answer. – sam Oct 26 '14 at 14:31
  • thank you lot, that solved the situation. now am working on another issue. in gingerbread the alarm manager is firing quickly, in kitkat its firing after 32 seconds.. when i do this 10*60*1000 – user3278732 Oct 26 '14 at 14:46
  • @user3278732 I think it's due to SystemClock.elapsedRealtime(). It returns milliseconds since boot. Thus, in your case, I guess gingerbread boot time is faster than kitkat's. – sam Oct 26 '14 at 14:57
  • one more question, if i enter the app 200 times... the alarm manager gets overwritten with new one created right? i wouldn't have 100 alarm managers right? – user3278732 Oct 26 '14 at 15:35
  • Can u tell me how can I set it for 2 minutes only once – Madhav_nimavat Dec 06 '16 at 14:26
4

in your code you set the alarm this way

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

the interval time is wrong to run every two minutes you should write:

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

EDIT

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

PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
EC84B4
  • 7,436
  • 4
  • 21
  • 32
  • set repeating takes 4 params, not 3. – user3278732 Oct 26 '14 at 13:30
  • I put the code here https://www.wetransfer.com/downloads/8529755c58f64f5f9f5767be664893ce20141026134802/1395db476afa6c786d656162a5c140f420141026134802/5beafe its not working. simple app, just print out the alarm – user3278732 Oct 26 '14 at 13:50