gpt4 book ai didi

android - 如果 Activity 已关闭,则处理事件电源按钮按下

转载 作者:行者123 更新时间:2023-11-29 20:22:06 28 4
gpt4 key购买 nike

我如何知道在关闭 Activity 时是否按下了电源按钮,而不是打开/关闭屏幕,因为我想知道按下电源按钮的确切次数。

我想我已经使用了 Broadcast 或 Service,但我找不到任何适合我的解决方案,我不知道该怎么办。以下是我找到但无效的一些解决方案。

@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>

主 Activity .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));


}
}

锁服务.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;
}
}
}

屏幕接收者.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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com