Search code examples
androidalarmmanager

using AlarmManager.setRepeating to run a service every 5 seconds


After scourging stackoverflow and the web I am still frustrated and running into issues. I am attempting to make a service that will poll sensors periodically and send that data somewhere. I have started at the ground level, just trying to get the service to display a toast every x seconds so I know that it is indeed working.

I started by making an alarm receiver, where I initialized the service intent that I want to run periodically. That code can be found below.

package com.example.cnash.raf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
         Context oAppContext = context.getApplicationContext();

         if (oAppContext == null) {
             oAppContext = context;
         }

         Intent serviceIntent = new Intent(oAppContext, MyService.class);
         oAppContext.startService(serviceIntent);
    }
}

After that, I quickly made my service and just put a toast in the onStartCommand, just to test to see if the service was being started every x seconds.

package com.example.cnash.raf;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return Service.START_STICKY;
    }
}

After that, I made a setupAlarm method in my main activity and followed some steps to set that up. I use an Alarm Manager to do a .setRepeating, and then after that I finish the activity. But by my understanding the alarm should still be set to repeat every x seconds.

package com.example.cnash.raf;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import java.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupAlarm(5);
    }

    private void setupAlarm(int seconds) {
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent i = new Intent(getBaseContext(), AlarmManager.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar t = Calendar.getInstance();
        t.setTimeInMillis(System.currentTimeMillis());

        int interval = seconds*1000;
        am.setRepeating(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), interval, pendingIntent);
        MainActivity.this.finish();
    }
}

When I run this app, I can see a white screen quickly come up and disappear. This I am certain is the activity_main booting up on onCreate and then disappearing when I call finish(); But after the activity_main pops up and disappears, nothing happens. I am expecting to see a Toast pop up every 5000 ms, but it doesn't.

Also, I am going to include my AndroidManifest file because I am still iffy on whether or not I set that up right.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cnash.raf">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"></service>
    </application>
</manifest>

Any help is greatly appreciated, thank you

Small side question not related: a quick think-ahead, I was going to put my sensor polling data into MyService, where I would use SensorManagers and what-not. Is this the right place to put something of that sort, or does it belong in another class that I would call through MyService via Intent?


Solution

  • Two things that I noticed here is:

    1.Name of your broadcast receiver is AlarmReceiver. But in code you are using AlarmManager.class

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent i = new Intent(getBaseContext(), **AlarmManager.class**);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    
    
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent i = new Intent(getBaseContext(), **AlarmReceiver.class**);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    
    1. I cannot find the declaration of your broadcast Receiver (AlarmReceiver) in AndroidManifest.xml.

    Fix these two and it should work. Enjoy :)