BOOT_COMPLETED intents received even though no boot took place - android

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
}

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

How to Start an activity on receiving ACTION_SCREEN_ON message?

I am working on an application which will start an activity on receiving a ACTION_SCREEN_ON broadcast message.
How to implement this? Can anyone post the code for it?
I know that I have to use BroadcastReceiver. But I need a detailed explanation as I am a beginner.
Create in your application's AndroidManifest.xml a receiver with a intent-filter to catch android.intent.action.SCREEN_ON:
<receiver
android:name="your.app.packagename.Receiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
Then, create the receiver class:
package your.app.packagename;
// imports here
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
// Do whatever you need to be done when screen turns on
}
}
}
Realize that:
You need launch your app at least one time before it starts receiving the ACTION_SCREEN_ON broadcast.
The onReceive() method is expected to run fast. If you are going to do heavy work, launch an AsyncTask or a background thread for.

Need to shut off a service when a call is coming in and when user dials a number

So, I have a service that runs in the background (it's super secret so I cant tell you what it is:) ) but I need to be able to shut it off when the user initiates a call or a call is coming in. So far, I have checked out the Intent.ACTION_ANSWER, but for some reason, my Broadcast receiver never detects it. I have also tried to use the PhoneStateListener, but in my case statements, I am failing to understand how to do anything with my service. To what context is a PhoneStateListener?
Some example code for my BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_ANSWER)) {
phoneRinging = true;
}
Intent i = new Intent(context, MyService.class);
i.putExtra("phone", phoneRinging);
context.startService(i);
}
Here is a snippet for starting a service via the PhoneStateListener.
Example: startService(new Intent(---CONTEXT---, MySuperSecretService.class))
And the receiver is in the manifest:
<receiver android:name=".PhoneReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ANSWER"></action>
</intent-filter>
</receiver>
WTH goes into the CONTEXT portion in this statement if I am in a PhoneStateListener?
Please check ACTION_PHONE_STATE_CHANGED instead of ACTION_ANSWER. ACTION_ANSWER is not broadcast intent for incoming call. It's a standard activity action.

Does BroadcastReceiver can be killed from Android OS to free memory?

My app look at arriveed email in K9 client in this way...
1) AndroidManifest:
<receiver
android:name="com.myapp.K9MailReceiver">
<intent-filter>
<action
android:name="com.fsck.k9.intent.action.EMAIL_RECEIVED" />
<data
android:scheme="email" />
</intent-filter>
</receiver>
2) My Broadcast Receiver:
public class K9MailReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
final Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.putExtra(Prepare.MAIL, intent.getExtras());
context.startService(serviceIntent);
}
}
My question: is it possible that Android decides to kill my K9MailReceiver class to free memory? If yes, how to prevent this?
Any BroadcastReceiver doesn't appear constantly in memory. It get's instantiated when the intent gets received , and killed after onReceive() method is finished.
A receiver only runs when it is being called, so no. Be sure to keep run time under 5 sec though.
However your service may be killed at any time. You can make sure to keep it running by posting an ongoing notification but you probably should not. If it is killed in a low memory situation it will be restarted later.

Categories

Resources