我正在開發一個SOS安卓應用程序。如果手機處於模式睡眠/待機狀態,並且開始發送幫助請求(例如發送短信),我想要檢測手勢(例如屏幕上的幾次觸摸)。 如何檢測此手勢? 有人可以幫助我嗎? 謝謝檢測電話睡眠的手勢
---解決方案---- 我找到了解決辦法here,這是我的代碼:
1)中的主要活動
getApplicationContext().startService(new Intent(this, UpdateService.class));
2)我創建一個服務
public class UpdateService extends Service {
@Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new PowerHookReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
// your code
} else {
// your code
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}}
3)我創建一個PowerHookReceiver();
public class PowerHookReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, UpdateService.class);
context.startService(i);
//do what you want
}}
4)清單
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<receiver android:name=".PowerHookReceiver" >
</receiver>
<service android:name=".UpdateService" />
我希望它是有用=)
好的,那麼如何在待機模式下使用硬件按鈕(啓動一項活動)? – user3193714