How do you make WakeLock pop app to foreground - android

I want my app to run in the background or after the phone has been put to sleep and when you near a certain latitude and longitude my app will immediately pop to the foreground. this must work no matter if it was from sleep or another app was in foreground.
I am using a service and a WakeLock. I am pretty sure the service is working but I don't know how to check because the debugger returns nothing if it is not in the foregrond. If it is working then my WakeLock is not working as anticipated.
public abstract class WakeLocker {
private static PowerManager.WakeLock fullWakeLock;
private static PowerManager.WakeLock partialWakeLock;
private static PowerManager pm;
public static void acquire(Context context) {
if (fullWakeLock != null) fullWakeLock.release();
if (partialWakeLock != null) partialWakeLock.release();
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
fullWakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
partialWakeLock.acquire();
}
public static void release() {
if (fullWakeLock != null) {
fullWakeLock.release();
fullWakeLock = null;
}
if (partialWakeLock != null) {
partialWakeLock.release();
partialWakeLock = null;
}
}
public static void wakeDevice(Context context) {
fullWakeLock.acquire();
//KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
//keyguardLock.disableKeyguard();
}
public static boolean fullWakeLockActive() {
return fullWakeLock != null;
}
}
the fullWakeLock is being acquired here
if (makeUseOfNewLocation(location) < 20) {
if (WakeLocker.fullWakeLockActive())
WakeLocker.wakeDevice(allert.getMA());
if (callProgress.isOffHook()) {
new BlinkBack(allert.getMA());
}

The wake lock itself cannot wake the device from sleep or cause your so to come to the foreground. You would have to use location services or an alarm to wake the device up, then use your wake lock to keep the device awake and trigger a UI component of your app to the foreground.

Related

AlarmManager + Service on Idle (screen off)

I call my Service with alarm manager
like this:
alarmManage.setExact(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + getPoolingInterval(), pendingIntentPolling);
On my ServicePooling i reschedule it of the same way, and this ServicePooling call another service to send data on my service.
Intent serviceSocket = new Intent(this.context, SenderService.class);
this.context.startService(serviceSocket);
All works very well every minut i receive on my server a polling communication, but when my device are screen off and without USB plugged, this stop work.
This is a bad idea to use Service for AlarmManager nowadays. Use WakefulBroadcastReceiver instead. your device fall asleep then unplugged.
public class BRMine extends WakefulBroadcastReceiver {
public static final String INTENT_FILTER = "com.example.BRMine";
#Override
public void onReceive(Context ctx, Intent intent) {
// TODO Auto-generated method stub
OWakeLocker.acquire(ctx, _.indexNOTS);
ComponentName comp = new ComponentName(ctx.getPackageName(),
SMine.class.getName());
startWakefulService(ctx, intent.setComponent(comp));
}
}
where:
public class OWakeLocker {
private static PowerManager.WakeLock[] wakeLocks = new PowerManager.WakeLock[_.indexNOTS_MAX];//Services count
#SuppressWarnings("deprecation")
public static void acquire(Context ctx, int index) {
WakeLock wakeLock = wakeLocks[index];
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, _.APPNAME + Integer.toString(index));
if (wakeLock != null && wakeLock.isHeld()){
wakeLock.acquire();
}
}
public static void release(int index) {
WakeLock wakeLock = wakeLocks[index];
if (wakeLock != null)
wakeLock.release();
wakeLock = null;
}}
to start:
Intent intent = new Intent(BRMine.INTENT_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(ctx, myintentalarm, intent, PendingIntent.FLAG_CANCEL_CURRENT):
am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
I solve my problem using a answer of Vyacheslav but wihtout AlarmManager because setExact didint work for me on idle and my android is a api lower then 23 (and don't have setExactAndAllowWhileIdle) i use a timertask on startapplication in my case works werry well, i just need this when my application are runnning.

Switch between wakelocks

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;
}
}

Android GCM not send notification message to device

I have facing problem in getting notification message from GCM Server.Device will get notification correctly when it not idle or in running state but when device goes idle for 10-15 minutes at that time device not able to get notification and also all registered devices are not gets notification from GCM server.How to resolve this problem?
Normally, your app need to wake when it sleeps.
Put this into your manifest file to wake your device when the message is received
<uses-permission android:name="android.permission.WAKE_LOCK" />
Add java class name WakeLocker.java
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context context) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "WakeLock");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
Call the above code in 'private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()' that might be in your MainActivity.java
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
Thank This source
Hope this helps

Unlock device, display a text, then lock again

For the need of my application, I need to display a message on the screen even if the lockscreen is enabled, then wait 3 seconds, than I have to lock again the phone as I don't want it to make unwanted phone calls in your pockets.
First part is easy:
if (PreferenceManager.getDefaultSharedPreferences(
getBaseContext()).getBoolean("wake", false)) {
KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
WakeLocker.acquire(ProtoBenService.this);
Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (isKeyguardUp) {
ProtoBenService.this.startActivity(myIntent);
} else
Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();
WakeLocker.release();
}
With this class:
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "CobeIm");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
And the Activity:
public class LockActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
TextView tv = new TextView(this);
tv.setText("This is working!");
tv.setTextSize(45);
setContentView(tv);
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
LockActivity.this.finish();
}
};
mHandler.postDelayed(mRunnable, 3 * 1000);
}
}
So, this is nice, the phone can display my text!
The only problem comes when I want to lock again the phone, it seems that locking the phone is protected by the system...
Programmatically turning off the screen and locking the phone
how to lock the android programatically
I think that my users won't understand the Device Admin and won't be able to activate it. Is there any workaround to lock the screen without the Device Admin stuff?
I used the following method for locking and unlocking phone.
initializing
KeyguardLock keyguard;
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguard = km.newKeyguardLock("MyApp");
to unlock phone
keyguard.disableKeyguard();
to lock phone again
keyguard.reenableKeyguard();
to turn screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and dont forget to use the following permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
I am pretty sure that you have to use the Device Admin Features to lock the screen.
protected static void initiateDeviceLock(Context context) {
ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean active = dpm.isAdminActive(componentName);
Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active));
if (active) {
dpm.lockNow();
}
}
To help the user's setup the Device Admin features you can take them to the settings page:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivityForResult(intent, CODE);

Why no push notification or some notification for incoming messages in the screen when the phone is in locked state? on Galaxy note

i am using Samsung Galaxy Note. When my phone is in locked state, and when i click the power button i see the pattern unlocking screen. When new messages flow in, i just see a mail icon in the top status bar, but no other notifications in the screen. what needs to be done for that?
You have to wakeup the device when push message came.
onMesaage method of GCM service you have to call below code
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
See the below link
android AlarmManager not waking phone up

Categories

Resources