我制作了一个监控手机加速度计的应用程序,当它倾斜时,它应该调用某个号码。
到目前为止,我让所有东西都运行起来,它甚至可以打电话,但只有在我用手解锁手机之后。
该应用程序旨在当有人试图从我的车上偷轮胎时给我打电话,所以手机必须在睡眠状态下给我打电话。
这是我的调用方法和相关变量:
PowerManager powerManager;
WakeLock fullWakeLock;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
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");
}
...
public void call(){
fullWakeLock.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + ((EditText)findViewById(R.id.nPhoneToCall)).getText().toString()));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
Log.e("", "Call failed", e);
}
}
编辑:哦,我也试过了:
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
就像我说的那样,他确实会打电话,但前提是屏幕已经解锁:(
edit2:必须在 Android 2.1(API 级别 7)上运行
请帮助:3