I create an application for use flash LED android as torch or as strobe light and add wake lock and power manager for not sleep.But when the user go to menu setup my apk crashed.Without wake lock work fine!How i fix it?
private PowerManager.WakeLock wl;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Flash Strobe");
// .......................
case R.id.menu_setup:
stopTask();//stop timer repeat blink
if (camera != null) {
camera.release();
}
wl.release();
finish();
startActivity(new Intent(this, Setup.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
LogCat:
W/ServiceManager(148): Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=10151 pid=24623
f_button_handler button callback not registered
Related
I have an android application that needs to be locked (redirect to the login page) whenever the user presses the lock button. The user can lock the phone while on my application or while on other applications/home screen. In both of these scenarios, I need my application to be locked.
This is working fine in case the phone is locked from an application :
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
}
}
But I am not able to figure out the second scenario when the user has moved away from the application and then locks it. I do not want to start a service or any background thread for this purpose.
Try to add else and return at the end of the code so the activity will still awake in background process
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
} else{
return true;
}
I want to override the lock screen button somehow. The idea is that I want to keep my screen awake for a while, and I did so
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
}
Anyway, I have a flip cover on my phone, and when I put the flip on, my phone is get locked. How could I stop the locking screen event when I put the flip cover on? That's why I need to know if it's possible to stop the lock screen functionality for a while.
For the Flipcover, you have to check the SensorManager and for the Power/Lock Button you can try this -
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_SCREEN_OFF:
BaseActivity.unlockScreen();
break;
case Intent.ACTION_SCREEN_ON:
// and do whatever you need to do here
BaseActivity.clearScreen();
}
}
}
But I'm not sure it'll work
If my app is running and I press lock screen button, it will put the app in background.What is the method to check whether onPause() is called by screen lock?.Thanks in advance.
All you have to do is check if the screen is on or not.
#Override
protected void onPause() {
super.onPause();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = pm.isInteractive();
} else {
screenOn = pm.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
}
You Can Simply Know It By Using This Method
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
System.out.println("On Pause called");
}
For Keeping The Device Awake while lock screen. Documentation.
Ok in your case you would need Wake_Lock
To use a wake lock, the first step is to add the WAKE_LOCK permission to your application's manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
If your app includes a broadcast receiver that uses a service to do some work, you can manage your wake lock through a WakefulBroadcastReceiver, as described in Using a WakefulBroadcastReceiver. This is the preferred approach. If your app doesn't follow that pattern, here is how you set a wake lock directly:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
To release the wake lock, call wakelock.release(). This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.
DO this after setting powermanager.
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = powerManager.isInteractive();
} else {
screenOn = powerManager.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
You just want to know when onPause is called? You could override the super function and add logging to the function:
#Override
public void onPause() {
super.onPause();
System.out.println("On Pause called");
}
I write application with service which periodically takes data from server,analyze them and after it launches alarm ( if it necessary), I use partial wake lock( in service) for awake device and parse json data, then I use full wake lock for awake screen at device. but it is doesn't work, how to solve this problem?
code:
Autobus66WakeLock.partialLockOff(Autobus66Service.this);
Autobus66WakeLock.lockOn(Autobus66Service.this);
after that this code returns false
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
lockOn method:
public static void lockOn(Context context) {
LocalLog.appendLog("full lock off");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//Object flags;
if (wl == null)
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MATH_ALARM");
wl.acquire();
}
lock off:
public static void partialLockOff(Context context){
LocalLog.appendLog("partial lock off");
if(pWlock != null && isPartialWakeLock){
pWlock.release();
isPartialWakeLock=false;
}
}
My goal is make same thing as power button do.
I try PARTIAL_WAKE_LOCK and this is my code..
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
after that I open WAKE_LOCK permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
But when I launch my application not thing happen.
Do I miss something ?
Thanks
From your question I'm not totally sure whether you:
Try to turn off the device by pressing a button
Want to make sure the device will not go to sleep (as this is what a WakeLock is supposed to help you with). It can't prevent user interaction though (just tested on HTC Desire).
For 1) You can't lock the device or turn it's power off without being signed as a system app, as written here: http://groups.google.com/group/android-developers/browse_thread/thread/36399f15724ac3ae/98d93e53616cf495?show_docid=98d93e53616cf495
For 2) You can prevent the device from sleeping using WakeLock, sample code can read like this:
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
}
// Call me from a button
public void doLock(View view) {
Log.d(TAG, "Lock");
if (!wl.isHeld()) {
Log.d(TAG, "acquire");
wl.acquire();
} else {
Log.d(TAG, "release");
wl.release();
}
}