我如何知道活动关闭时是否按下了电源按钮,而不是屏幕开/关,因为我想确切知道电源按钮被按下了多少次。
我想我已经使用了广播或服务,但是找不到适合我的解决方案,我不知道该怎么办。这是我找到但无法使用的一些解决方案。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
Log.d("#tag", "ACTION_Down ");
}
return super.onKeyDown(keyCode, event);
}
Manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
或这样,但我不需要它,因为它仅在屏幕上打开/关闭,如果在屏幕关闭时快速双击电源按钮,则它不起作用。
public class PowerBroadcast extends BroadcastReceiver {
private boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
screenOff = true;
Toast.makeText(context, "ACTION_SCREEN_OFF ", Toast.LENGTH_SHORT).show();
Log.d("#tag", "ACTION_SCREEN_OFF ");
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
screenOff = false;
Toast.makeText(context, "ACTION_SCREEN_ON", Toast.LENGTH_LONG).show();
Log.d("#tag", "ACTION_SCREEN_On ");
}
}
}
最佳答案
看一下这个 :
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.i("", "Dispath event power");
//do watever u want.
return true;
}
return super.dispatchKeyEvent(event);
}
或尝试孔代码:
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);
//do something.
}
}
}
关于android - 如果 Activity 已关闭,则处理事件电源按钮的按下,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33013400/