I'd like to switch on the screen from a service and immediately let the default system timeout take over (i.e. not hold a wakelock for a specific time, but just hand over control to the system). I've tried WakeLock.acquire() followed immediately by WakeLock.release() (with a SCREEN_BRIGHT_WAKE_LOCK), but that doesn't even switch on the screen at all. Is there any way to achieve this from a Service (short of launching a new WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON-dummy-activity)?
I think this is what you are looking for. You need to use
PowerManager.ACQUIRE_CAUSES_WAKEUP
Related
I develop a small app, this one have to run always over all when the device is sleep or deep sleep (press right button to turn off the screen) I read many posts about it, and all tell me that the way is use PowerManager, and my question is if I use fine this command, my structure is> myActivity.class, ReceiverBoot.class and ServiceBoot.class, I use the POwerManager class on myActivity.class like this:
PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
PowerManager wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP , "MyWakeLock");
on onCreateMethod after of this one put
wakeLock.acquire();
and after of this one I have a
super.onCreate(savedInstanceState);
this.mTimer.scheduleAtFixedRate(
new TimerTask(){
#Override
public void run(){doTask();}
} , 0, 1000);
wakeLock.release();
on Manifest XML code I have
<uses-permission android:name="android.permission.WAKE_LOCK" />
and on layout XML code have
android:keepScreenOn="true"
but after to 10seg the creen es OFF always but the app is running, jut with wifi.
the app work very fine with wifi conn, but when change to 3G conn, the app is gone, I use fine this command?? the problem is the kind of conn to Internet??? thanks a lot!
I'm not 100% clear on your issue. Whether its the data issue, or the screen issue. (Or if the screen issue is what you are doing to try and fix the data issue?).
For the screen
You are not using the right lock to keep the screen on. PARTIAL_WAKE_LOCK only requests that you can use the processor. To keep the screen on your app use one of SCREEN_DIM_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK or FULL_WAKE_LOCK depending on what you want. This lock should be held for as long as you need the lock. Currently you are releasing it in onCreate(). Keep in mind that if the user presses the power button though that your lock is released (with PARTIAL being the exception to this).
If your intent is just to keep the screen on when a view is active then it's better not to use the lock at all. The wake lock needs an extra permission. You can do it by adding this to your onCreate override:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
This is the same effect as using android:keepScreenOn="true", which you already seem to be doing. I can't however tell why that isn't working from your snippets. Make sure you are inflating the right layout.
For your data
The device will likely be switching off 3G data when the screen is not active (and no lock is present). Again, don't release your lock if you need it (Though don't keep it forever either, that's just going to suck up phone battery).
Is it feasible to make our Android application completely transparent (as if its not active at all) and work on the other apps?
My Requirement:
First last an app and after some few settings, make it transparent. Once its transparent, the user will not know that this app is active, however, our app should respond to only specific controls.
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast. So, currently I am using Power button which is not the requirement.
Please throw some light on this. I did some research but, couldnt find any. :(
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast.
I am not sure it this is right. If you read Android BroadCastReceiver for volume key up and down question, it seems that you can detect it in BroadCastRceiver. I've never tried but it might be worth a try. Do something like following:
In your BroadcastReceiver onReceive function, do something like following:
public void onReceive(Context arg0, Intent intent) {
if (intent!=null){
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
// Get the old volume from the SharedPOreferences
// volume variable contains the current volume
// Compare it to the old value saved. If it is greater than old value then user pressed the UP Volume button else DOWN volume.
}
}
Also I am not sure that you can make it transparent and still keep it running. You can have a background of an activity as transparent though. How do I create a transparent Activity on Android?. Hope it helps.
I want my Android background to go into sleep mode - but then wake up when the user starts moving.
However, if I use the accelerometer in NORMAL mode (the lowest sample rate ~ 5Hz) I fear it would still consume too much power.
The best way to do it so far is on USER_PRESENT - screen on and unlocked.
Not even screen on(possibly with keyguard present) works, because, as many of you may know, there are plenty of bad apps out there that will hold a wakelock and start the screen from time to time.
I am contemplating having the user push the volume up/down buttons..
Is there any better solution to this?
don't know if you're still looking for a way to do this, but i discovered (by accident) that you can start a shakeListener, and your app will get the events, even when in the background.
(and by "by accident", i mean that i did not want my app playing the sound that it is supposed to play when the app is in the background, but even when had another app in the background, and then even put the phone to sleep, when i would walk with the phone in my pocket, it was enough shaking to cause the app to perform the operation in the background.)
/**
* load and set up the listener for shake detection
*/
private void loadShaker() {
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
public void onShake() {
if (!mActivityPaused)
performMyOperationCausedByShake();
}
});
}
you could probably thus set this up so that performMyOperationCausedByShake() performs an intent that causes your desired Activity to happen.
(it might be the case that this is not quite sensitive enough for what you're looking for …)
I'm receiving an incoming C2DM notification while the screen is locked. I'd like to wake up the screen and display the notification message on top of the lock screen using an Activity. I'm launching the notification Activity from my C2DM BroadcastReceiver as follows:
Intent new_intent= new Intent().setClass( context, EIAlertDialog.class );
new_intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
new_intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity( new_intent );
and within the notification Activity's onCreate method, I wake up the screen as follows:
PowerManager powerManager= (PowerManager)getSystemService( Context.POWER_SERVICE );
if (!powerManager.isScreenOn()) {
mWakeLock= powerManager.newWakeLock(
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP,
"My Tag" )
mWakeLock.acquire();
}
The screen is woken up, but the notification Activity is not visible until I unlock the screen.
I realize that I can avoid the lock screen with the code below, but that is not desired. I want the user to unlock the phone, only if he/she is interested in reading/responding to the notification.
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
);
This is not possible as far as I know. It sounds like you are trying to replicate the iOS experiences. You should realize that android has its own conventions and violating them is not something you should do lightly.
Android is a little bit of a contradiction. It's very open and as a developer you have access to anything, and it's up to you to use those powers for good or evil. When I say evil I don't mean malware. I mean apps that try to get cute and use things in ways they weren't meant to be used, like putting up notifications asking you to use the app more. The contradiction is that you don't actually have access to everything, there are a few parts the developers decided were so important that app couldn't mess with them. The lock screen is one of those parts. You can replace your home app all you want, but you never have to worry about your replacement lock screen failing and preventing you from accessing your phone.
Even if this were possible you would have more problems to deal with. Every lock screen is different, manufacturers can and do customize it so you have no guarantees your activity won't get in the way of unlocking the phone.
Uses the flags is the right way to do it, using the power manager is wrong.
Your requests sound conflicting to me: you say you want the activity to appear on top of the lock screen (in fact we don't do that, we hide the lock screen so the activity can be seen), while at the same time you want the user to first have to unlock the device.
If you are thinking you want the user to see a notification before unlocking the device to see your activity... I really think you don't want that. The notification is very small (in the status bar at the top), and the next that is shown while posting it is very transient. This is not going to be a good experience for someone who heard their phone beep or buzz and is pulling it out to see what is going on.
You should use whatever combination of the window flags that make sense for your app. You can get pretty much any reasonable behavior between the various combinations of them. These are used for the alarm clock, incoming call UI, etc.
Try this in your activity
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenOn = pm.isScreenOn();//check id screen is on
if(!screenOn){//if not turn it on or wkaeup the screen
final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Screen On");
wl.acquire();
Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO: handle exception
}finally{
wl.release();// release wakelock important
}
}
};
timer.start();
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);//keep the activity running under lock screen..
Hope this helps
I want to take pictures from the Android device's camera periodically over a matter of hours, to create a time lapse video effect.
I set an Alarm Manager with an AlarmManager.RTC_WAKEUP flag set to start up a service every few minutes.
The service holds a partial wakelock, does some work, and then calls a Broadcast Receiver through the Alarm Manager which starts up an Activity.
The activity is created (or is resumed), turns on it's own wakelock, and sets up the camera preview surface. Once the surface is setup the SurfaceHolder listener's surfaceChanged() method is called, which finally takes a picture.
If the device is awake, everything works perfectly as expected. But if the device is asleep, once the Activity's onResume() method is finished the Activity is instantly paused. The camera's preview surface never finishes initializing, and no picture will ever be taken.
So the questions I have are:
Is there any way to wake up the phone programmatically? I even try using:
PowerManager powerManager =
(PowerManager)this.getSystemService(Context.POWER_SERVICE);
powerManager.userActivity(SystemClock.currentThreadTimeMillis(),false);
But that doesn't wake up the phone if it is asleep.
Is there any way to take a picture without using a preview surface view?
Is there a way to take a picture that doesn't rely on asynchronous callbacks? Can I put all the code in the Activities onResume() method to take a picture?
Is there any way to keep the Activity's onResume() method running long enough so that the camera's preview has enough time to initialize and call all the listeners?
I am using the wakelocks correctly, and I have all the permission's set properly in the manifest file. My activity isn't kept awake long enough for the asynchronous listeners to properly work.
And to compound the issue, I'm trying to keep everything Android 1.6 compatible, because that is the only test device I have access to.
This is frustrating stuff!
I have finally gotten somewhere now.
I have to create a wakelock using these two flags
PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
Then the device wakes up, and starts at the keyguard screen.
But the only way I can get past the keyguard screen and take a picture is to use these flags on the Activity's window:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
But this is only available with Android 2.0, and doesn't work in 1.6.
You can also disable the Keyguard screen with
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
km.newKeyguardLock(TAG).disableKeyguard();
provided you have the DISABLE_KEYGUARD permission.
That's available since API Level 1.
Are you doing something like this in your onResume method
.... onResume() {
....
WakeLock myWakeLock = .....;
...
}
If so, as soon as the method exits, the WakeLock is released, and the device is free to do whatever it feels like doing ( which is likely to go back to sleep )
and you will need to store the WakeLock in the class somewhere, not as a function local.