Reputation:
I have an apps which is working fine but the problem is when it goes automatically to screen lock mode, i need to complete a job. Unlock screen i can detect using intent receiver but cant find how to know when its going to screen lock mode via power button press or automatically.
Is there a way to find it? I have tried following but no luck.
<uses-permission android:name="android.permission.WAKE_LOCK" />
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");
@Override
protected void onPause() {
super.onPause();
wl.release();
Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}//End of onPause
@Override
protected void onResume() {
super.onResume();
wl.acquire();
Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}//End of onResume
EDIT: tried but no luck under onCreate
KeyguardManager myKM = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
} else {
//it is not locked
Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
EDIT: You cant do 3 things with mainfest because of system performence, to still do it one has to use following dynamic injection.
private BroadcastReceiver mPowerKeyReceiver = null;
private void registBroadcastReceiver() {
final IntentFilter theFilter = new IntentFilter();
/** System Defined Broadcast */
theFilter.addAction(Intent.ACTION_SCREEN_ON);
theFilter.addAction(Intent.ACTION_SCREEN_OFF);
mPowerKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String strAction = intent.getAction();
if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
// > Your playground~!
Log.d(TAG, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>onDestroy");
}
}
};
getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter);
}
private void unregisterReceiver() {
int apiLevel = Integer.parseInt(Build.VERSION.SDK);
if (apiLevel >= 7) {
try {
getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
}
catch (IllegalArgumentException e) {
mPowerKeyReceiver = null;
}
}
else {
getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
mPowerKeyReceiver = null;
}
}
Upvotes: 0
Views: 592
Reputation: 166
Maybe you can use a Broadcast Reciever for "android.intent.action.SCREEN_ON" and "android.intent.action.SCREEN_OFF".
Upvotes: 1