Starting Android Service on boot and PreferenceChange - android

As my code looks today, I'm periodically sending a alarm(?) using AlarmManager that is received by AlarmReceiver extends BroadcastReceiver which in turn starts a Service. The Service do some updating and ends with a stopSelf(). IMO this is the best way of periodically perfom a task without constantly having a Service running. Correct?
The issue with this code is however that the whole chain of events is initiated onSharedPreferenceChanged(). I (initially) thought this was a good idea since the whole updating thing is enabled by the user in SharedPreferences.
I've now come to the conclusion that this is in fact not very good and that I need to initiate the AlarmManager/AlarmReceiver/Service/whatever both onPreferenceChange but also on boot.
I've done some searching but everyone seems to want to start the Service on boot. As I see it, I just need to initiate the AlarmManager which will then start the Service (when needed and only periodically).
Please help me with, first of all, sorting this out and secondly coding it!
Thanks in advance!

Then, create and register a BroadcastReceiver where you will do the AlarmManager stuff:
public class YourBootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// do the AlarmManager here
}
}
Then, on your manifest:
<application>
... other stuff
<receiver android:name=".YourBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Related

Listen for user changed event (multi user device)

I've an app that starts itself if the phone is booted. A user told me his phone is used by two people, one of them is using my app and one not.
So I need some event to listen to when the user is switched, so that I can start my apps service if the correct user is using the phone. Anything I can use for that?
Edit
I'm listening to the boot event with a broadcast receiver registered in the manifest, so I know what this is. But I could not find anything suitable for switching users on a device
You need to look for something called BroadcastReciever in android. They are used to capture events such as camera click, phone booting up, screen unlocked etc... These events have a callback called onReceive where you can implement your login.
It's quite easy and you can Google it.
In your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your application element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}

How build Android Background Service proper way

I want to build a android background service for check data from MySQL data base.Normally I doing extend from Service class and when start the app,I run service using startService() method.But problem is if i remove the app from task manager,the service is also stopped.Another thing is I want to start this service when start the device,I mean beginning.How I implement this.Help me.
When you kill your app, the service is going to restart, not be removed. You can decrale the flag to define the break point when service restart, and write some 'if' 'else' to do thing after this break point.
And if you want to start serivce when start the device, just create the broadcastReceive call 'autoStart'.
In Manifest:
<receiver android:name=".autoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in autoStart class:
public class autoStart extends BroadcastReceiver
{
public void onReceive(Context ctx, Intent arg1)
{
Intent intent = new Intent(ctx,yourservice.class);
ctx.startService(intent);
Log.i("Autostart", "started");
}
}
When started device, system will detect on boot completed, and call this autoStart BroadcastReceive, and call your service from here

Broadcast receiver for Silent Mode detection? [duplicate]

I'm using a service. In that service my code should get executed when the user changes to silent mode, i.e. as soon as the user changes to silent mode, my code needs to get executed.
How can I do this?
You don't want use a service. Instead you want to use a BroadcastReciever that filters for the android.media.RINGER_MODE_CHANGED Intent.
You might want to take a look at this project as it deals with the phone being silenced. It probably has some source code that will be useful to you.
You can register to listen to the BroadcastAudioManager.RINGER_MODE_CHANGED_ACTION.
In your manifest file you can register the intent like this
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
And then recieve the intent at method
public void onReceive(Context context, Intent intent)
of class that extends BroadcastReceiver class

Execute code when user changes to silent mode in Android

I'm using a service. In that service my code should get executed when the user changes to silent mode, i.e. as soon as the user changes to silent mode, my code needs to get executed.
How can I do this?
You don't want use a service. Instead you want to use a BroadcastReciever that filters for the android.media.RINGER_MODE_CHANGED Intent.
You might want to take a look at this project as it deals with the phone being silenced. It probably has some source code that will be useful to you.
You can register to listen to the BroadcastAudioManager.RINGER_MODE_CHANGED_ACTION.
In your manifest file you can register the intent like this
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
And then recieve the intent at method
public void onReceive(Context context, Intent intent)
of class that extends BroadcastReceiver class

Android Broadcast Receivers: Can they execute simultaneously or do they always execute consecutively?

I have a little confusion with broadcast receivers. I have a broadcast receiver which is triggered upon TIME_SET and TIMEZONE_CHANGED actions (the code is given below). What I was wondering is, can OnDateTimeChanged (see the code below) be triggered simultaneously (and its execution overlaps) when both TIME_SET and TIMEZONE_CHANGED actions are triggered or is one always going to be triggered after the other? Based on some simple experiments I did, I got the impression that the two executions of OnDateTimeChanged are triggered consecutively with no time overlap but I cannot be 100% sure of this. If anyone has an idea I'll be very happy.
<!-- Excerpt from manifest -->
<receiver android:name=".OnDateTimeChanged">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
</intent-filter>
</receiver>
// Broadcast receiver class
public class OnDateTimeChanged extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Do some work here
}
}
BTW, both TIME_SET and TIMEZONE_CHANGED can be triggered when under Settings - Date&Time you switch to the Automatic mode and this changes both the time and the timezone.
-Ali
Logically, they would all execute simultaneously. Physically, only one can occupy a core at a time and might finish before another starts. Under identical conditions,the behavior might appear to be consistent. The documentation itself describes it as, "All receivers of the broadcast are run in an undefined order, often at the same time."
If you want to give other receivers a chance to run, you can call Thread.yield().

Categories

Resources