In my Main Activity i'm setting this flag onCreate :
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
But i want on to be able to turn it off on BroadcastReceiver
android.intent.action.ACTION_BATTERY_LOW"
How can i do this ? how will my main application know that the broadcast has been received and that it should turn off this flag ? .
You can register a BroadcastReceiver programmatically in your activity by calling registerReceiver() in onResume() and unregisterReceiver() in onPause():
// Placed at your activity level
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent);
if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction()) {
// Remove the flag
} else {
// Battery is okay, add the flag
}
}
public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_LOW);
// Probably also want to receive notification that the battery has gone back up
filter.addAction(Intent.ACTION_BATTERY_OKAY);
registerReceiver(mBroadcastReceiver, filter);
}
public void onPause() {
unregisterReceiver(mBroadcastReceiver);
}
Related
I am working on music player app and I have included the basic functionality like Play, Pause, next and Previous tracks using service class.
Now I want to update myrecyclerview UI according to the action that user clicks(eg: on the play, there should be an image of the selected element in recyclerview).
I thought to use Broadcast Receiver that service class will broadcast on different actions and then recyclerview can update according to the broadcast action. But how do I add such functionality?
You'd need an in-activity broadcast receiver. Such receivers are registered when your activity is shown and unregistered once it's hidden.
public class MainActivity extends Activity {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here update your RecyclerView
}
};
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(YourService.YOUR_CUSTOM_ACTION);
registerReceiver(mReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
And in your service, simply send broadcasts:
Intent intent = new Intent(YOUR_CUSTOM_ACTION);
// put extras in the intent as you wish
sendBroadcast(intent);
How can I close my application when the Android goes to stand by either by user pressing the button or screen timeout?
I've tried onResume(), onPause and onRestart() methods but I already use then in another context.
Regards
In your application class onCreate() method register receiver for Screen Off Event
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d(TAG, Intent.ACTION_SCREEN_OFF);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d(TAG, Intent.ACTION_SCREEN_ON);
}
}
}, intentFilter);
like this.
I have defined a BroadcastReceiver in my activity in the following way to get results from an IntentService running on another thread:
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
boolean isSyncCompleted = intent.getBooleanExtra("issynccompleted", false);
if (isSyncCompleted){
showAlert("Sync completed in service");
}
context.unregisterReceiver(receiver);
}
};
//register receiver for service
registerReceiver(receiver, new IntentFilter(ContentSyncService.SERVICE_INTENT_FILTER));
Note that I am unregistering my receiver in the onReceive code block and not in onPause. That is because I want to have the receiver listening even when the activity has been destroyed.
But I keep getting the error: "Activity ... has leaked IntentReceiver ... that was originally registered here. Are you missing a call to unregisterReceiver()?"
This solution works for me, I override the Activity methods, not a BrodcastReceiver:
#Override
protected void onPause() {
super.onPause();
// unregister receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
}
#Override
protected void onResume() {
super.onResume();
// register receiver
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("your.package.name"));
}
Only will unregister receiver if you receive action at least one time.
So you need unregister your receiver in OnPause (and in other place if you need), because there you are safe that always will unregister the receiver.
Intead of use context.unregisterReceiver(receiver) inside your receiver,
Try it:
activity.unregisterReceiver(this);
I essentially want to display a screen whenever the screen is unlocked regardless of the application already running.
Can someone just tell me how to display text as soon as the phone gets unlocked. I can take it from then on.
I have the following code up till now which I found on the net....
Suppose I want to display abc.xml as soon as the phone gets unlocked. How will i add it in the ScreenReceiver Class ?
Also I do not want to set any screen when the application runs.Do I need to run the code below as service ?
public class SampleActivity extends Activity {
//Declare the necessary variables
private BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.v("$$$$$$", "In Method: onDestroy()");
if (mReceiver != null)
{
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
}
where the Screen Reciever class is as follows
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
//onResume() will be called.
// Better check for whether the screen was already locked
//if locked, do not take any resuming action in onResume()
//Suggest you, not to take any resuming action here.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Log.v("$$$$$$", "In Method: ACTION_USER_PRESENT");
// Handle resuming events
}
}
}
For one, you don't display abc.xml, you display an activity, dialog, or other UI component. You can set up a broadcast receiver that listens for the ACTION_BOOT_COMPLETED intent. Once the device boot completes, you can start a sticky service to listen for the actions you have above. Presumably you'll want to display abc.xml in an activity, so you'll need to fire startActivity from one of the if() blocks above.
I am working on Android 3.0 and I need to know in my application when the device goes on sleep / turn off screen.
How can I register to this intent/event so I will be able to run some actions when this happens? Is there any action in BroadcastReceiver that notifies for this?
This page has a tutorial on exactly what you're looking for.
Code copied from that page (in order to turn this from a link-only answer to something directly useful):
1) Create a class in your application to receive the intent. For example, the following receiver stands alone and sets a static variable to be used in part 2:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
}
2) Modify your activity to receive screen on/off events. Your receiver will check the static variable in your broadcast receiver to know the reason for the intent you just received:
public class ExampleActivity extends Activity {
#Override
protected void onCreate() {
// initialize receiver
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// your code
}
#Override
protected void onPause() {
// when the screen is about to turn off
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
System.out.println("SCREEN TURNED OFF");
} else {
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}
#Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
System.out.println("SCREEN TURNED ON");
} else {
// this is when onResume() is called when the screen state has not changed
}
super.onResume();
}
}