ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working? - android

I'm trying to turn WiFi off when the screen is OFF (locked), and turn it on again when screen is ON (unlocked).
I made a BroadcastReceiver; put in manifest this code:
<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
and this is the class MyIntentReceiver:
package org.androidpeople.boot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyIntentReceiver extends BroadcastReceiver {
// Called when boot completes
public static boolean startup;
#Override
public void onReceive(Context context, Intent intent) {
// Set what activity should launch after boot completes
System.out.println("Intent Action: " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
System.out.println("locked : ACTION_SCREEN_OFF");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("not locked : ACTION_SCREEN_ON ");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("User Unlocking it ");
}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// this to indicate that program is running
// automaticlly not manually by user
startup = true;
System.out.println("Automatic BOOT at StartUp");
Intent startupBootIntent = new Intent(context, LaunchActivity.class);
startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupBootIntent);
}
}
}
And the result is - both ACTION_SCREEN_ON
and ACTION_SCREEN_OFF never fired!
USER_PRESENT and BOOT_COMPLETED worked fine but the other did not. I'm using an emulator, not a real device - can this cause the problem?
Any help?
I need to catch the screen on and off in order to enable/disable WiFi
to save battery.
Thanks in advance

To capture the SCREEN_OFF and SCREEN_ON actions (and maybe others) you have to configure the BroadcastReceiver by code, not through the manifest.
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);
It's tested and works right.

You cannot catch those intents through XML (I forget why). However, you could use a Service that registers a BroadcastReceiver member in its onStartCommand() and unregisters it in its onDestroy(). This would require the service to be running in the background, constantly or as long as you need it to, so be sure to explore alternative routes.
You could define the BroadcastReceiver in your Service class like so:
private final class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//stuff
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//other stuff
}
}
}
For a slightly complicated example, but one that shows how the BroadcastReceiver and Service interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.

Related

Broadcast not triggered when process is created

I have a broadcast which monitors for unlock event for the phone. But when the app's process is killed and no longer in memory, Unlocking the phone does not trigger the Receiver, instead I can see in the Android studio, that new process is created for that app.
If lock and unlock it again, then as the process is already running, I can see the BroadcastReceiver is triggered.
<receiver
android:name=".UserPresentBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Broadcast Receiver:
public class UserPresentBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = UserPresentBroadcastReceiver.class.getSimpleName();
#Override
public void onReceive(Context arg0, Intent intent) {
Log.d(TAG, "onReceive: Unlock Boradcast received");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Toast.makeText(arg0, "You just unlocked the device", Toast.LENGTH_LONG).show();
}
}
}
I am unable to understand this behavior. Is this the default behavior?
You have to register and unregister this broadcast receiver in Activity(or Service for listening in background all the time).
Manifest entry won't work.

How to check if my BroadcastReceiver receives the BOOT?

I have a BroadcastReceiver who should receive the BOOT.
Is there a simple way to check if the BroadcastReceiver receives the BOOT correctly?
Thanks
Assuming BOOT means BOOT_COMPLETED then you could use the following:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//Do a Log or, less likely, a Toast or start your application here.
}
}
};
You would register this in the manifest like such:
<receiver android:name="com.example.yourAppName.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Stopping a service when the USB device is unplugged

I want to stop a running service when a usb is unplugged.
inside my activity onCreate I check the intent for its action
if (getIntent().getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Log.d(TAG, "************** USB unplugged stopping service **********");
Toast.makeText(getBaseContext(), "usb was disconneced", Toast.LENGTH_LONG).show();
stopService(new Intent(this, myService.class));
} else {
init();
}
And inside my manifest I have another intent filter
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
And this intent filter as well which is being called.
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
But the detach is not being called.
Hmmm.. ACTION_USB_DEVICE_DETACHED is fired when a USB device (not the cable) is detached from the phone/tablet. This is not something you want.
I do not know if there is a straight forward API for detecting USB cable connections, but you can use ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED to accomplish your goal.
Use following filters for your receiver:
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
And in your receiver, you can check the state and implement the logic you want:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
switch(intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
case 0:
// The device is running on battery
break;
case BatteryManager.BATTERY_PLUGGED_AC:
// Implement your logic
break;
case BatteryManager.BATTERY_PLUGGED_USB:
// Implement your logic
break;
case BATTERY_PLUGGED_WIRELESS:
// Implement your logic
break;
default:
// Unknown state
}
}
}
You need to register a BroadcastReceiver
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Log.d(TAG, "************** USB unplugged stopping service **********");
Toast.makeText(getBaseContext(), "usb was disconneced",
Toast.LENGTH_LONG).show();
stopService(new Intent(this, myService.class));
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(receiver, filter);

Screen_Off does not work in Application Level

Screen_Off does not work in application Level ,although it is working on Activity Level.
My Problem,when Screen goes off,it must go to Login screen,below code ,successfully go to Login screen but it cause me unregistered Receive Broadcast
what i have did
if l do in Menifesty level,Screen off broadcast does not fire,
Manifest Level
<receiver android:name="com.android.libray.ScreenReciver" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
if i do in activity level,then it will fire screenReciver
IntentFilter filter =new IntentFilter(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver=new ScreenReciver();
registerReceiver(mReceiver, filter);
public class ScreenReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
Intent myintent = new Intent(context,TimerClockActivity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
wasScreenOn = false;
Log.v("screenoff", "Logggoff");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
}
}
}
The SCREEN_OFF Broadcast is a protected broadcast, and you will only receive it if you register dynamically through Java code in a Service or Activity or another Receiver. Registering in the manifest doesn't work for this broadcast.

Activity handle when screen unlocked

So I have my onResume command restarting a stopped thread which runs my game loop.
This works perfectly for resuming the app when closed by home button or by focusing on another app.
However, when you turn the screen off then on again, the activities onResume command fires right away before screen is unlocked. I need my activity to know when the screen is unlocked so it can restart the thread at the appropriate time.
Has anyone had this happen before?
for detect screen on and screen off register a broadcast reciver like:
AndroidManifest.xml:
<receiver android:name="receiverScreen">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.ACTION_USER_PRESENT" />
</intent-filter>
</receiver>
In Activity or Service:
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new receiverScreen();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
receiver code where System inform you if Screen on/off happen:
public class receiverScreen extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
}
}

Categories

Resources