Alright, so i am having some problems trying to get a broadcast receiver and service to work properly with screen off and screen on.
What i am trying to do is start something when the screen goes off or when the screen goes on. I got it to work from an activity for testing, but the activity must be currently running. I need it to start from the background pretty much.
Now, i know that using the intent filters in the manifest does not work for screen_off and on. How would i be able to do this? I guess this would work sort of like a lockscreen...
Screen off --> starts something (example activity or create a log message as a toast wouldn't work)
Add a receiver:
public class BroadcastReceiverScreenListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), Intent.ACTION_SCREEN_OFF)) {
** Do your stuff**
}
}
From the docs:
You cannot receive this through components declared in
manifests, only by explicitly registering for it with
registerReceiver(BroadcastReceiver, IntentFilter)
This is a protected intent that can only be sent
by the system.
Related
I have an app where users can call each other through an SDK. Everything is working fine. I just need to be able to make calls like fabebook and whatsapp even when the app is closed. It should be able to start a particular activity even when the app is completely closed. I've followed some stackoverflow questions and I tried using the notification receiver but Its not working.
NotificationReceiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intentNotification = new Intent();
intentNotification.setAction("com.start.app");
context.sendBroadcast(intentNotification);
}
}
Manifest:
<receiver
android:name=".Notifications.NotificationReceiver"
android:enabled="true"
android:exported="false"></receiver>
What exactly you looking for ..
This can be done by creating a broadcast receiver class and send a broadcast when message received and on your broadcast receivers onReceive method call the specific activity you wish to open.
Dont forget to add the reciever in your manifest
Not tested but i think it will work..
I'm using a BroadcastReceiver in my Android app which simply contains the following piece of code:
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
GcmIntentService.isHandled = true;
Toast.makeText(context, "broadcast receiver test", Toast.LENGTH_SHORT).show();
}
}
I'm using this receiver to determine if my activity is running and carry out some updates in a ListView without having any notifications produced by GcmIntentService.
With the code being simple so far, only creating a toast message, I'm unable to catch the boolean value from GcmIntentService.isHandled as soon as the sendBroadcast is invoked.
Is it possible in any way to determine if the code for my receiver has finished running. I understand that sendBroadcast is an asynchronous call, and I'm making use of Thread.sleep(1000) so far to wait for the isHandled value, but it would be nice if there is a more reliable method on achieving this.
Any thoughts?
Your question can be divided to two parts:
1.How to know that if there is a receiver actually received the broadcast.
2.How should the receiver notify the service that message is been handled.
It seems difficult to achieve the first goal through standard Intent api, instead I suggest you may try the "observer pattern".
You may create a global Observable object in your Application and make your Activity implements Observer, register itself in onCreate() and unRegister in onDestory().Inside the Service you can check if there is an Activity running through countObservers() and then simply notify it.
I'm developing a game, and it's important that the sudden battery warning doesn't obstruct you. The activity already pauses the game during onPause(), but this isn't called when the battery warning comes up because it's a pop-up on top of the activity, and the activity continues to run.
I don't mean to dismiss the messsage, I just want the activity to know that it should pause the game if it appears, so that you can take your time to read it and dismiss it and not lose because of that.
I have searched a lot and haven't found a question similar to this. Is it possible to detect such a thing?
You may observe ACTION_BATTERY_LOW broadcast.
http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_LOW
Something like:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// where arg1.action is ACTION_BATTERY_LOW
}
}
You should register for the event broadcast with the System and implement a Broadcast Receiver component.
In your manifest file, you register for receiving the system broadcast pertaining to low batter condition. The relevant intent filter is ACTION_BATTERY_LOW. For a list of other filters related to battery status please refer to Intents and Intent Filters page and search for word BATTERY. HTH.
I have a dynamically registered BroadcastReceiver on a Service. It gets AudioManager.RINGER_MODE_CHANGED_ACTION as IntentFilter. Every time I start the service I get the log message in onReceive() method. It works normally after that. I do not want it to receive once when service is started each time. Could you please tell me what I am missing here?
receiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Log.d("zil", "degisti");
}
};
IntentFilter filter=new IntentFilter(
AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);
The intent you are interested in, AudioManager.RINGER_MODE_CHANGED_ACTION, is "sticky". That means that the system always keeps the last broadcast sent and whenever a BroadcastReceiver is registered that is interested in that Intent, it receives it right away. This is a very useful feature but sometimes it isn't what you want ;-)
I assume that you are only interested in actual "change" events. In this case you need to ignore the "current" event and listen only for any events that happen in the future. Lucky for you, there is a solution:
In 'onReceive()' do the following:
if (isInitialStickyBroadcast()) {
// Ignore this one as we aren't interested in the current state
} else {
Log.d("zil", "degisti");
// Do whatever you want to do with the event here
}
unregisterReceiver(receiver);
this probably wont work because you created an Anonymous inner class implementation of BroadcastReciever. instead create a nested/private class that extends BroacastReceiver in the activity where you want your service started. Then dynamically register and unregister your receivers in the Activity lifecycle callbacks
I have an app in which the main Activity starts an AlarmReceiver that calls an IntentService that runs in the background and does stuff. I'm unclear on what the correct way is to check on the IntentService's actions and present the end-user with some feedback in the visible Activity that they're in, on the IntentService's current state. In my ideal world there can be an icon somewhere on the screen that I can set to notify the user of what's going on with the IntentService. I don't need the user to be able to *do anything, just have feedback.
All advice welcome.
Android has a notification API, which is even easy to use - Creating Status Bar Notifications.
If you want your activity to receive updates from the service, I would suggest using broadcasts and broadcast receivers.
How to send a broadcast intent:
Intent i = new Intent("your.action");
sendBroadcast(i);
To receive this broadcast within your activity, you have to implement a broadcast receiver:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//
}
};
which you have to register...
registerReceiver(myReceiver, new IntentFilter("your.action"));