如何以编程方式在android中检测设备电源按钮按下两次
Posted
技术标签:
【中文标题】如何以编程方式在android中检测设备电源按钮按下两次【英文标题】:How to detect device power button press twice in android programmatically 【发布时间】:2015-07-13 19:54:56 【问题描述】:我正在制作一个 android 应用程序,它需要检测设备电源按钮按下两次/三次并在后台发送 SMS 的事件。监听器应该在后台运行(即即使我的应用程序没有打开,它也应该检测到按键事件并采取相应的行动)。
以下是我尝试过的代码,但不起作用...
我的代码:
public class MyBroadCastReciever extends BroadcastReceiver
int Count=0;
@Override
public void onReceive(Context context, Intent intent)
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
Count++;
if(Count==2)
//Send SMS code..
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
//This is for screen ON option.
清单文件:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MyBroadCastReciever" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
</application>
【问题讨论】:
我猜你永远不会关闭或打开 2 个直屏,它会一直交替显示。 @PedroLobito 没关系。但它甚至没有检测到任何人(开/关) Detect on/off Key Press Android的可能重复 我投票结束这个问题,因为 OP 说的是code not working
,而不是提供完整的代码部分并试图帮助自己/志愿者。
@sanjay 您如何使用以下代码计算按下电源按钮的次数?
【参考方案1】:
这是我用来检测用户是否存在、屏幕开/关的代码。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.userpresent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.userpresent.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.userpresent.LockService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>
</application>
</manifest>
MainActivity.java
package com.example.userpresent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), LockService.class));
LockService.java
package com.example.userpresent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
public class LockService extends Service
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public void onCreate()
super.onCreate();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
public class LocalBinder extends Binder
LockService getService()
return LockService.this;
ScreenReceiver.java
package com.example.userpresent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
public class ScreenReceiver extends BroadcastReceiver
public static boolean wasScreenOn = true;
@Override
public void onReceive(final Context context, final Intent intent)
Log.e("LOB","onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
// do whatever you need to do here
wasScreenOn = false;
Log.e("LOB","wasScreenOn"+wasScreenOn);
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
// and do whatever you need to do here
wasScreenOn = true;
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
Log.e("LOB","userpresent");
Log.e("LOB","wasScreenOn"+wasScreenOn);
String url = "http://www.***.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
context.startActivity(i);
【讨论】:
谢谢伙计。它工作正常。但同样明智的是,我如何从上面的代码中跟踪电源按钮的长按。 不客气,看看这个答案***.com/a/10365166/797495 那个链接很好。但我不知道在哪里可以使用该代码(onKeyLongPress ..)?它应该出现在 LockService.java 类的 onStartCommand 方法上吗? 要使用该代码,您不需要我回答中的任何内容,只需将其放在班级最后一个
之前。
如果我在我的 mainActivity 上使用最后一个 的 onLongPress 方法,它仅在应用程序处于活动状态时才有效。如果我离开了我的应用程序,它将无法正常工作。就是这样,我问我如何在服务上使用该代码?【参考方案2】:
这是我做过的,
short description
:你只需要检测屏幕turn off
和turns on
计算它们之间的时间差,如果小于4秒(in my case
)发送消息否则不要。
P.S-你可以更改
pressing of power buttons
的间隔。
在您的BroadcastReceiver
中使用它:
@Override
public void onReceive(final Context context, final Intent intent)
cntx = context;
vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Log.v("onReceive", "Power button is pressed.");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
a = System.currentTimeMillis();
seconds_screenoff = a;
OLD_TIME = seconds_screenoff;
OFF_SCREEN = true;
new CountDownTimer(5000, 200)
public void onTick(long millisUntilFinished)
if (ON_SCREEN)
if (seconds_screenon != 0 && seconds_screenoff != 0)
actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
if (actual_diff <= 4000)
sent_msg = true;
if (sent_msg)
Toast.makeText(cntx, "POWER BUTTON CLICKED 2 TIMES", Toast.LENGTH_LONG).show();
vibe.vibrate(100);
seconds_screenon = 0 L;
seconds_screenoff = 0 L;
sent_msg = false;
else
seconds_screenon = 0 L;
seconds_screenoff = 0 L;
public void onFinish()
seconds_screenoff = 0 L;
.start();
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
a = System.currentTimeMillis();
seconds_screenon = a;
OLD_TIME = seconds_screenoff;
new CountDownTimer(5000, 200)
public void onTick(long millisUntilFinished)
if (OFF_SCREEN)
if (seconds_screenon != 0 && seconds_screenoff != 0)
actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
if (actual_diff <= 4000)
sent_msg = true;
if (sent_msg)
Toast.makeText(cntx, "POWER BUTTON CLICKED 2 TIMES", Toast.LENGTH_LONG).show();
vibe.vibrate(100);
seconds_screenon = 0 L;
seconds_screenoff = 0 L;
sent_msg = false;
else
seconds_screenon = 0 L;
seconds_screenoff = 0 L;
public void onFinish()
seconds_screenon = 0 L;
.start();
private long cal_diff(long seconds_screenon2, long seconds_screenoff2)
if (seconds_screenon2 >= seconds_screenoff2)
diffrence = (seconds_screenon2) - (seconds_screenoff2);
seconds_screenon2 = 0;
seconds_screenoff2 = 0;
else
diffrence = (seconds_screenoff2) - (seconds_screenon2);
seconds_screenon2 = 0;
seconds_screenoff2 = 0;
return diffrence;
manifest.xml
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" >
</action>
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:exported="false" />
粘贴到application
标签中
它对我来说很好用 也在后台
【讨论】:
什么是 .MyReceiver 和 .MyService?据我所知,这些是活动名称.... @AtiarTalukdarMyReceiver
是 BroadcastReceiver & MyService
是 Service class
这在我的代码中不起作用。你能给我一个完整的代码吗?我迫切需要它。我的意思是需要在锁屏或其他屏幕中实现电源按钮或音量按钮双击操作。
这里出现错误... seconds_screenon = 0 L;【参考方案3】:
检查这个:Power Button
还有这个:Press Power Button
static int i=0;
public boolean onKeyDown(int keyCode, KeyEvent event)
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER)
i++;
if(i==2)
//do something
//at the end again i=0;
return super.onKeyDown(keyCode, event);
【讨论】:
我应该在哪里提供此代码,因为我在此应用程序中没有任何活动。仅广播 Receiver 类 它只有在我的应用程序处于活动状态时才有效。但即使我不在我的应用程序中,我也希望它能够完成 您是否在清单中添加了接收方?像这样:试试这个...
public boolean onKeyDown(int code, KeyEvent keyEvent)
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_POWER)
// Your Logic Is Here
return true;
return super.onKeyDown(code, keyEvent);
【讨论】:
以上是关于如何以编程方式在android中检测设备电源按钮按下两次的主要内容,如果未能解决你的问题,请参考以下文章