I used this code to wake up my device (Moto G 1 gen) when screen is off, but it seems that it doesn't work. It works only when screen is on.
edit: Now it works, but CallScreen.class shows for 1 seconds and then finishes. LogCat provides no information for that.
Code:
Intent intent = new Intent(MainActivity.this, NiceBroadcastReceiver.class);
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("name",name);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + timeToCall, pendingIntent);
NiceBroadcastReceiver extends BroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getExtras().getString("name");
Intent i = new Intent(context, CallScreen.class);
i.setClassName("(package)", "(class)");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("name2", name);
context.startActivity(i);
//Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}
CallScreen.class:
PowerManager.WakeLock fullWakeLock;
PowerManager.WakeLock partialWakeLock;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
if(fullWakeLock.isHeld()){
fullWakeLock.release();
}
if(partialWakeLock.isHeld()){
partialWakeLock.release();
}
fullWakeLock.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relLay);
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Bundle extras = getIntent().getExtras();
if (extras != null) {
name = extras.getString("name2");
}
incomingCall = (TextView) findViewById(R.id.textIncomingView);
caller = (TextView) findViewById(R.id.callerId);
caller.setText(name);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
BroadcastReceiver
class, and the idea is that you're supposed to make a subclass of that. – GentBroadcastReceiver
is starting a service, unless you are usingWakefulBroadcastReceiver
or myWakefulIntentService
, the device may fall back asleep before the work gets done. – Gent