How to Start an activity on receiving ACTION_SCREEN_ON message? - android

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.

Related

I want to use broadcast receiver even if the app is closed

I am trying to show a toast message when receiving an incoming call/outgoing call.
The receiver is not working if the app is closed.
I do not want to use Service. Please help me out.
'I am using the below receiver code'
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
if (isConnected(context)) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Call in progress", Toast.LENGTH_LONG).show();
}
}
}
'This is receiver registered in manifest'
<receiver android:name="com.example.android.testapplication.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.new_outgoing_call"></action>
</intent-filter>
</receiver>
Try adding the phone state permission to your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
By default when you register a BroadCastReceiver with AndroidOS that means Receiver always work as the Service part even your application is not working, since you do not have to worry about this problem.
I think the problem is the way you register you Receiver was not correct.
With in/out coming call you should use PhoneStateListener which has overrided method onCallStateChanged. You can use 3 states over there.
Maybe this example will be helpful.

Different broadcastreceiver depending on activity life

Some time ago, I was able to have 2 broadcastreceivers, one declared in my manifest, and the other one, declared in my activity.
Like here : What is the best way to handle callback from IntentService
But, since I have changed for LocalBroadcastReceiver, and changed sendOrderedBroadcast with sendBroadcast method, only the one registered in activity is only receiving the broadcast. I have read that localbroadcastreceiver can not be registered in manifest.
So how to wake up a broadcastreceiver which is not registered in an activity ?
I found my own answer, so I give the response for those who can be interested in.
I have two BroadcastReceiver class, one used when application is wake up, with :
ActivityBroadcastReceiver myReceiver = new ActivityBroadcastReceiver();
#Override
protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("com.mybroadcast"));
super.onResume();
}
and
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
super.onPause();
}
and in my manifest file :
<receiver
android:name="com.broadcastreceiver.NotificationBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.mybroadcast" />
</intent-filter>
</receiver>
To send my broadcast :
if(!LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast))
context.sendBroadcast(broadcast);
I agree that with this, context.sendBroadcast() , everyone can received my broadcast, but in my code, sendBroadcast send only non-sensible data. That is the best solution I got for now.

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.

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
}

onBoot Complete as user preference

Is there a way that trough the application, I can subscribe and unsubscribe for the "ACTION_BOOT_COMPLETED" message?
If so, how can I do this?
Any kind of pointer will help me.
Thanks in advance,
Regards,
Vinay
Check out my answer to this question. And these links.
http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
Trying to start a service on boot on Android
Android: how to start a service at boot based on user-settings?
The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)
So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.
As a matter of fact, there is:
Your boot receiver:
public class BootstrapReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_BOOT_COMPLETED.equals(action)) {
// do your thing
}
}
}
Add a receiver and a permission for receiving boot events:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
....
<receiver android:name=".BootstrapReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Via the application (Use this if you want to programatically add/remove boot receiver and not via AndroidManifest.xml. But remember, you will still have to declare the boot permission in your AndroidManifest.xml)
Context ctx = getContext();
BootstrapReceiver booter = new BootstrapReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
ctx.registerReceiver(booter, filter);
Unregister:
ctx.unregisterReceiver(booter);

Categories

Resources