How build Android Background Service proper way - android

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

Related

Start Service on boot but not entire Android app

I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.

Start a service from Thread.UncaughtExceptionHandler?

I'm trying to setup a global exception handling service as mentioned in this answer. This approach sounds logical because after a crash, the code in my custom Thread.UncaughtExceptionHandler may not execute successfully, either because the application may not be in a stable state or because there may not be enough time before the process is killed by the OS.
Unfortunately, this approach doesn't seem to work because the service launches in the same process so it, too, is killed. How can I start a service from the handler's uncaughtException()? Can I force the service to launch in a completely seperate process?
I'm setting my custom default (VM-level) exception handler in Application like this:
public class MyApp extends Application {
#Override
public void onCreate() {
Thread.setDefaultUncaughtExceptionHandler(
MyExceptionReporter.getInstance(this));
...
In my custom handler, I'm launching the service like this:
final Intent i = new Intent(MyExceptionService.INTENT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(MyExceptionService.EXTRA_STACK_TRACE,
Log.getStackTraceString(ex));
mContext.startService(i);
The service is declated in the AndroidMaifest.xml file and launches fine from elsewhere in the application.
Ah, I figured this out. The key was to add android:process to the manifest, which forces the service to launch in a seperate process. This prevents the service from being killed when the application is killed.
<service
android:name="your.package.blah.blah.MyExceptionService"
android:process=":exception" >
<intent-filter>
<action android:name="your.package.blah.blah.REPORT" />
</intent-filter>
</service>

how to show call screen while android app running on background

Im making a SIP application for android 2.3.3. I can call someone and my "incoming call screen" is shown when some calls me. But when my app is running on the background and someone calls me , the "call screen" isn't been launched. so how can I make it launch like a normal incoming call.
FIXED:
Manifest: add the following code in application tag
<receiver android:name=".ReceiverTest" android:enabled="true">
<intent-filter>
<action android:name="com.example.INCOMING_CALL" />
</intent-filter>
</receiver>
Receiver class: when I receive a call, it will open my Incomingcall page
public class ReceiverTest extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1) {
...
Intent nextPage= new Intent("com.example.IncomingPage");
nextPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(nextPage);
...
}
}
If you are using an Activity your app is not running the background. When a new activity is brought to the top of the stack your application is paused and placed in the background. To allow your application to receive any type of notification try running a service or create a broadcast receiver to pick up the intents you want to listen for. This allows your app to process while it is not on the top of the stack.
UPDATE
So you have registered a broadcast receiver... this is good... but... If you registered it in your activity and the activity is killed, so is the reference and vm of your application and the intent is not received. Try making your broadcast receiver independent of the activity (ie make it be invoked by the system using the manifest). Then your broadcast receiver can receive intents even when your application is dead, and launch what you need.
The alternative is make a service that registers your receiver - a service can run in the background but then you have to worry about making your service light enough to never be killed (more tricks to this). The best best is option 1 (way less overhead).

Starting Android Service on boot and PreferenceChange

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" />

BOOT_COMPLETED intents received even though no boot took place

Hey there,
has anyone else discovered that sometimes BOOT_COMPLETED intents arrive out of nowhere?
I have created an OnBootRecoverReceiver which starts a service after it received a BOOT_COMPLETED intent from android - works fine so far... but in some (yet not traceable) events i receive such an intent even though there was no reboot at all.
Anyone has a clue about that, or had the same problem before?
The Manifest entry for the receiver:
<receiver android:name=".trigger.OnBootRecoverReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>`
Receiver Code:
public class OnBootRecoverReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("de.android.stuff.trigger.OnBootRecoverService");
context.startService(serviceIntent);
}
}
If anyone has a idea, please feel free to help.
To clear things up here: there was no BOOT_COMPLETED intent created anywhere
For some reason the Service (which is startet by the Receiver) crashed on the particular device some time ago. Our good friend the android ActivityManager then decided to re-animate the said Service:
03-16 12:00:02.239: WARN/ActivityManager(2504): Scheduling restart of crashed service de.ukn.hci.android.diary/.trigger.OnBootRecoverService in 5000ms
Which of course led to me thinking the Recevier was fired again, but - as it turns out there was just the Service starting again. Without any REBOOT intent whatsoever.
Solution to stop this:
Add a boolean Extra to the intent created by the Receiver
serviceIntent.putExtra("isFromReceiver", true);
context.startService(serviceIntent);
Then check for this boolean Extra while in onStart(Intent) of the Service
boolean isFromReceiver = intent.getBooleanExtra("isFromReceiver", false);
if( !isFromReceiver ) {
return; //just stop starting the service
}

Categories

Resources