I have made an app that wakes up the screen when it receives a text message. I came up with the following code to wake the screen.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(!isScreenOn ){
final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My tag");
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();
}
}
};
timer.start();
}
Now the problem is that if I comment out or remove the statement Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show(); my screen wont wakeup. I am not sure what is the problem here... and I am using android 2.3.
Use PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP instead of just PowerManager.SCREEN_DIM_WAKE_LOCK, and see if that helps.
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;
}
How can I make SCREEN_ON event if my phone is in SCREEN_OFF.
Or in other words, how can I create some intent so that it invokes my phones screen from off to on.
Actually I have a service which creates a view over the top of lock screen.
In order to test it, I want to create a timer which invokes screen every 1 minute if screen is off. The service runs in background infinitely.
Try this:
private void screenOn() {
try {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean isScreenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
isScreenOn = pm.isInteractive();
}
else {
isScreenOn = pm.isScreenOn();
}
if(!isScreenOn){
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
// PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
// wl_cpu.acquire(10000);
}
}catch (Exception e){
e.printStackTrace();
}
}
My app -
reads a text file with scanner containing mobile nos and message for each.
Sends SMS to each mobile nos as per the text file.
Each time no of SMS varies from 20 to 140nos which takes about 8 to 10 minutes.
If I keep the CPU running by continuous interaction with touch, all SMS are sent properly. No problem.
If I do not touch and keep the device aside, only 15 - 20 SMS are sent.
I tried to use
1 Keep screen on - not working.
2 Use partial wakelock - not working
3 Used partial wakelock with a handler for releasing wakelock after 10 min - still only 15 - 20 SMS sent but wakelock is realeased after 10 minutes.
4 Used keep screen on with partial wakelock - still same as above and not working.
Here is my code.
public class Ardtxt extends Activity {
private Button buttonsendsms;
String gg = "my app";
File fileexists ;
String mdn, msg;
String tomdn = "9123456789";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_ardtxt);
//*********************
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Button buttonsendsms = (Button) findViewById(R.id.buttonsendsms);
buttonsendsms.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"ardtxtwakelock");
wakeLock.acquire();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "All SMS are sent.",Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("9123456789", null, "wake lock released", null, null);
wakeLock.release();
}
}, 600000);
Toast.makeText( getApplicationContext(), "start" , Toast.LENGTH_LONG).show();
Log.d("chk","app start");
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"kk");
directory.mkdirs();
Log.d("mytxt app App", Environment.getExternalStorageDirectory()+File.separator+"kk");
fileexists = new File(Environment.getExternalStorageDirectory()+File.separator+"kk"+File.separator, "Sample1.txt" );
if (fileexists.exists()) {
//Do action
Log.d("app exxxxx",String.valueOf(fileexists));
Toast.makeText( getApplicationContext(), "subject file exists" , Toast.LENGTH_LONG).show();
System.out.println("file exists so can be used by us");
Log.d("Ketan check", "Sample1.txt exists");
try {
Readtxtfile();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.d(gg,"Exception : file not found");
e.printStackTrace();
Toast.makeText( getApplicationContext(), "crpg file not found" , Toast.LENGTH_LONG).show();
}
}
}
});}
Pl let me know what I am missing or why partial wakelock is not keeping the CPU alive.
Thanks.
Yes, I have added wakelock permission.
However, I am not sure how to code a service with thread. Kindly provide a sample code if possible. Search didn't revealed any usefull code or I could not understand the same.
Thanks.
I'm trying to turn the screen on when the app sends a notification. Currently when I acquire a WakeLock it flashes on and off very quickly, almost un-noticeably. As far as I can tell I have my flags set correctly. AQUIRE_CAUSES_WAKEUP to turn the screen on and ON_AFTER_RELEASE to (poke the user activity timer) leave it on once I release it - otherwise, I understand it turning itself off immediately... Anyone got an insight?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TAG");
if(mTurnScreenOn) {
wl.acquire();
Log.d("TAG", "WakeLock Acquired");
if(pm.isScreenOn()) { Log.d("TAG", "Screen is on");
} else { Log.d("TAG", "Screen is off"); }
}
mNotificationManager.notify(1, notification);
if(mTurnScreenOn) { //release the wakelock if needed
wl.release();
Log.d("TAG", "WakeLock Released");
if(pm.isScreenOn()) { Log.d("TAG", "Screen is on");
} else { Log.d("TAG", "Screen is off"); }
}
where you are setting the value of mTurnScreenOn , Look at your code you are acquiring wake lock when mTurnScreenOn==true and on same condition your are releasing it also.
for more info see this
I m looking for the best practice to implement a service for logging gps- or other sensor-values periodically (every 10-60 sec). The service should deal with the standby mode, when the phone goes asleep.
Any help (pseudo-code or tutorials) is very much appreciated!
It looks like it is impossible to let the orientation sensors work constantly for hours even though the device may fall asleep (refer to http://code.google.com/p/android/issues/detail?id=3708#makechanges
As soon as the display goes off, the sensors will do alike... :(
I now implemented a wakelock (needs permission)
<uses-permission android:name="android.permission.WAKE_LOCK" />
in conjunction with a timer and a broadcastreciever that will turn the display back on again. This is of course crazy for battery life but I found no other way so far.
This is my onCreate method in the service:
#Override
public void onCreate() {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CONTEXT = this;
PowerManager pm = (PowerManager) CONTEXT.getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my tag");
mWakeLock.acquire();
Log.d(TAG, "Wakelock acquired");
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
this the onStart method of the service:
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStart");
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
Log.d(TAG, "Screen is on");
} else {
Log.d(TAG, "Screen is off");
Timer timer = new Timer("DigitalClock");
timer.schedule(new TimerTask() {
#Override
public void run() {
Log.d(TAG,"Waiting 1 sec for switching the screen back on again...");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
"my tag");
mWakeLock.acquire();
}
}, 1000);
mWakeLock.acquire();
}
}
and this is the BroadcastReceiver class:
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, MyService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
This workaround will also do with a device UN-plugged from the debugger via USB (I first had an issue with this).
Please let me know if you have a better solution, or if there is fix in 2.3. Thanx!
Here's a link to a tutorial on how to use Android Services. LINK
Here's a link to a tutorial on how to read data from sensors. LINK