Make app/service "run after screen turns off" ? -Android- - android

i a'm completly android programing noob
i'am working on developing "Androrat" (this project is not mine i had found it over the internet)
when the screen turns off the connection is lost
how can i make the processes/service always running ?
I had googled some and found that i have to include a partial_wake_timer
I want the code and where i have to enter it ?
I a'm completely noob please bair with me
http://dl.dropbox.com/u/102520949/Untitled.jpg
or maybe a kind person can help me over team-viewer ?

A partial WakeLock is what you want. It will hold the CPU open, even if the screen is off.
To acquire:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
To release:
wakeLock.release();
This answer was taken from this link:
How can I keep my Android service running when the screen is turned off?
Next time, please search more deeply.

Related

Android prevent screen timeout

I got the following problem,
I have an unity Game which I would like to run.
During running the Game, it always lowers screen lighting and switch of after about 2,5 minutes.
I already tried:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
Links:
Android disable screen timeout while app is running
Android Screen Timeout
Programmatically disable screen timeout
But after this, it still switches off as before.
Were there any changes done, in android or are there any other solutions to solve my problem.
Thanks.
Solution was very simple done using Unity adding the following Code into the Start() Function:
Screen.sleepTimeout = SleepTimeout.NeverSleep;
I doesn't know if it is switched of automatically by Unity or nor but I hope so.
Link to Unity Documentation

Wakelock works only when screen turned on

many things has been written about wakelocks in Android. I'm using wakelock this way:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial");
partialWakeLock.acquire();
// few hours work
partialWakeLock.release?
In most of devices, PARTIAL_WAKE_LOCK can keep device running whole night. But there are few models (Samsung Galaxy ACE 2 (i8190) - 4.1.2 etc.), which ignore this kind of wakelock. Wakelock is held, but phone goes to sleep if device's screen is turned off.
Is there some way how to determine, if application is running in device with this problem? I can let user turn devices's screen off for a few seconds and check if work has been done, but is there some automatic way?
Thanks in advance.

How can I prevent Galaxy S3 from stopping my app when turning idle?

I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.
I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.
Possible solutions are rooting the new phone and thus making advanced settings visible
or increasing the time-out (which i dont like so much as a solution).
Do you have any ideas how to solve the issue or general input that might enlighten me?
Thnx!
BTW: here is the app in question (no ad):
Google Play Link
I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)
First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.
But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();
...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.
Then, when I finish needing to keep the phone awake:
mWakeLock.release();
You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Android Wakelocks in 4.2 for flashing the screen on and off

In versions of android before 4.2 I used to have some code to flash the screen on when a notification came into the app (if users wanted it to flash). I used:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "example_tag"
);
wl.acquire();
//this would switch the screen on
//then after a little while I'd call
wl.release();
So the "aquire" method would switch the screen on (if it wasn't already) and the "release" would switch it straight back off.
In android 4.2 it seems that the switching off doesn't happen when you release the wakelock, but only when the screen timeout setting for the display is reached (default seems to be 2 minutes)
Is there any way to make it switch off when the wakelock is released. I've seen some bits in the API about specifically switching the screen on and off, but I'm a little unsure about using as I wouldn't want the app switching the screen off if somebody was in the middle of doing something, so the wakelock seemed to work well. Any suggestions?
The only way I've found it to set the screen timeout down to a short time but store off the default and restore it when finished. This will turn it down to around 5-7 seconds at the shortest and you have to be careful to restore the setting back correctly afterwards as it's a total hack really.

Unable to Keep Screen from Sleeping in Android 4.2

I have a simple battery test app I wrote for Android and have used for several versions now successfully. It works fine in Android 4.1 The app simply launches the chrome browser to a different web site, logs the time to a database, waits 60 seconds and does it all over again.
Now, in Android 4.2, the screen turns off and system goes to sleep despite the fact that I have a wakelock and have set screen timeout to never using settings. What's most annoying is it turns off at an indeterminate time, not a fixed time after starting the test. Any idea what I need to do?
Here's the wakelock code I call in the service that launches the browser over and over again.
super.onCreate();
// Wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
I also put this in the layout file
android:keepScreenOn="true"
However, the app's screen itself isn't visible throughout the test as Chrome is always the foreground window and the browser launcher is a service and the wakelock is in the service.
Any idea how to keep the screen from timing out? Nothing seems to work.
Just add following line to your XML file......
android:keepScreenOn="true"
:)
I would guess that Android is killing your service and causing your wakelock to be released. Try making your service run in the foreground (with startForeground()) and see if that helps.

Categories

Resources