当屏幕关闭时,我使用此代码唤醒我的设备(Moto G 1 gen),但似乎它不起作用。它仅在屏幕打开时有效。
编辑:现在它可以工作,但CallScreen.class显示1秒然后结束。 LogCat没有提供相关信息。
代码:
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扩展了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();
答案 0 :(得分:1)
ELAPSED_REALTIME_WAKEUP
只会启动CPU,并且仅在onReceive()
的{{1}}返回之前启用。
所以:
在您的活动出现之前,CPU可能会再次入睡,因为BroadcastReceiver
是异步的,而startActivity()
会在onReceive()
真正开始处理之前返回
startActivity()
无法启用您似乎想要的屏幕
ELAPSED_REALTIME_WAKEUP
无法解锁您似乎想要的设备
答案 1 :(得分:1)
好的,我明白了。在我看来,FLAGS没有正常工作,所以牛肉是在setContentView:
之后PowerManager.WakeLock wl
...
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_call_screen);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();