Skip to content Skip to sidebar Skip to footer

Android Long Running Service With Alarm Manager And Inner Broadcast Receiver

I have a Service that uses a custom Connection class (extends thread) to a hardware controller. When the User prefers, I wish to maintain this connection on a permanent basis. I al

Solution 1:

Does the phone slow down it's processor when it goes to sleep?

The phone shuts down its processor when it goes to sleep. That is the definition of "sleep".

I'm thinking it's the AlarmManger, but I'm having trouble getting it to work with an inner-class, within the Service. I tried using the API demos as a starting point, but I can't seem to figure out how to get the Broadcast receiver registered. I am trying to register the receiver programatically, with no changes to the manifest.

That is an unusual approach for AlarmManager. That being said, since you declined to describe "having trouble" in any detail, it is difficult to help you.

Get rid of getApplicationContext() (you don't need it and really don't want it in this case). I would register the receiver before touching AlarmManager. Before you go to production, please choose an action name that has your package name in it (e.g., com.something.myapp.KEEP_CONNECTION_ALIVE).

Beyond that, check LogCat for warnings.


UPDATE

In your LogCat, you should have a warning from AlarmManager complaining about not being able to talk to your BroadcastReceiver.

Replace:

Intent intent = newIntent(this, PingConnection.class);
intent.setAction("KEEP_CONNECTION_ALIVE");

with:

Intentintent=newIntent("KEEP_CONNECTION_ALIVE");

and you may have better luck.

Solution 2:

you can't register AlarmManager in a Service. All you can do is declare it as global in the Manifest.xml. You can start the alarm from service in this way, by declaring it in Manifest.xml

If you have a remote service and you close the launcher activity, the AlarmManager will still run, but don't forget to stop it on onDestroy() method of the service.

I've tried to register only in the Service the AlarmManager as I didn't used it for the main activity, but no success! It didn't work as registering as a normal BroadCastReceiver.

that's how the things are, you have to declare it in Manifest.xml as global

Solution 3:

I know it's late, but maybe it's useful for someone else.

You can register it, the problem is when the Intent tries to call it.

Instead of calling it like this:

Intent intent = newIntent(this, PingConnection.class);

Create an empty intent and add an action you are going to listen to:

Intentintent=newIntent();
intent.setAction("value you want to register");

Then create the pending intent and send the broadcast like you have it.

Create an attribute for the receiver so you can access it in the whole class and unregister if necessary (if the pendingIntent is also an attribute you can unregister any time):

privatePingConnectionpingConnection=newPingConnection();

Register it like this:

IntentFilterintentFilter=newIntentFilter();
intentFilter.addAction("the value you used before");
getApplicationContext().registerReceiver(pingConnection, filter);

Now you won't get any errors, and the class is not static, and it's an inner class.

Post a Comment for "Android Long Running Service With Alarm Manager And Inner Broadcast Receiver"