两次电源按钮preSS发送短信 [英] Twice Power button press sends sms
问题描述
我想创建一个能够应对当电源按钮pssed $ P $的应用程序。更具体地,这将响应它,当pressed 2倍,并发送SMS
I am trying to create an application that could respond when the power button is pressed. To be more specific, which would respond to it when pressed 2 times and sends the SMS.
我试图用的BroadcastReceiver
还可以,但没有奏效。
I tried using BroadcastReceiver
also, but it did not work.
下面是code我试着使用覆盖电源按钮:
Below is the code I tried using to override the power button :
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
return true;
}
return super.dispatchKeyEvent(event);
}
我想这也是:
I tried this also:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER)
{
// do what you want with the power button
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
和使用的BroadcastReceiver
我想这也是
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Check action just to be on the safe side.
if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) ||
(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) ) {
long delta = 0;
int count = 0;
if((System.currentTimeMillis() - delta) < DOUBLE_CLICK_INTERVAL)
{
count++;
Log.e("AAAAAAAA", "count= "+count);
}else{
count = 0;
}
if(count == 4){
Log.e("AAAAAAAA", "EVENT TRIGGERED!!!!!");
Intent alarmIntent = new Intent("com.helpme.EVENT_MyReceiver");
sendBroadcast(alarmIntent);
}
delta = System.currentTimeMillis();
}
}
};
但以上都不是为我工作了。
But none of the above worked for me.
推荐答案
我想你应该打电话给你的事件时系统(安卓)停机这样你就可以拦截按钮pressed样的事件。
I think you should call your event when the system(Android) shut downs that way you'll be able to intercept button pressed like event.
我建议你建立一个广播接收器捕获的ACTION_SHUTDOWN意图后,在的onReceive()方法,你可以做你喜欢什么都。
I recomend you build a broadcast receiver to catches the ACTION_SHUTDOWN intent and after that in the onReceive() method you can do what ever you like.
这篇关于两次电源按钮preSS发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!