我正在开发一个应用程序,可以在3-5秒内连续3次点击 POWER_BUTTON 。
我已经在所有StackOverflow中搜索了答案,但是没有一个对我有用。
Lars D中适用于该活动的答案尽管已被接受,但也行不通。
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, ActivitySetupMenu.class);
startActivity(i);
return true;
}
return super.dispatchKeyEvent(event);
}
有一个app打算这样做,我安装了它,但仍然无法正常工作,也许他们使用的API已过时,甚至已被删除。
这些解决方案不起作用的原因:
也许我们可以找到一种在活动中执行此操作的方法,但是我想在应用被杀死/在后台/屏幕锁定/时,使用服务/广播接收器收听操作屏幕关闭时。
好吧,这个问题在StackOverflow上肯定重复了很多次,但是没有给出完整或可行的答案。
答案 0 :(得分:1)
由于没有人试图解决这个问题,或者甚至根本无法理解这个问题,因此幸运的是,在搜索了多个小时后,我发现了这个很棒的website可以解决我的问题,也想在这里发布。
现在解决的问题:
我只是写下步骤,以防将来链接无法正常工作或将其删除:
1。首先,我们将创建一个广播接收器,它可以按如下所示监听和处理Android屏幕的开/关广播事件。
ScreenOnOffReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ScreenOnOffReceiver extends BroadcastReceiver {
private final static String SCREEN_TOGGLE_TAG = "SCREEN_TOGGLE_TAG";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_OFF.equals(action))
{
Log.d(SCREEN_TOGGLE_TAG, "Screen is turn off.");
}else if(Intent.ACTION_SCREEN_ON.equals(action))
{
Log.d(SCREEN_TOGGLE_TAG, "Screen is turn on.");
}}
}
2。在活动中注册和注销ScreenOnOffReceiver。
现在,我们将创建一个活动并在其onCreate()方法中注册ScreenOnOffReceiver,并在其onDestroy()方法中注销该接收者,如下所示。
ScreenOnOffActivity.java
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.dev2qa.example.R;
import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
public class ScreenOnOffActivity extends AppCompatActivity {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_on_off);
setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
// Create an IntentFilter instance.
IntentFilter intentFilter = new IntentFilter();
// Add network connectivity change action.
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
// Set broadcast receiver priority.
intentFilter.setPriority(100);
// Create a network change broadcast receiver.
screenOnOffReceiver = new ScreenOnOffReceiver();
// Register the broadcast receiver with the intent filter object.
registerReceiver(screenOnOffReceiver, intentFilter);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onCreate: screenOnOffReceiver is registered.");
}
@Override
protected void onDestroy() {
super.onDestroy();
// Unregister screenOnOffReceiver when destroy.
if(screenOnOffReceiver!=null)
{
unregisterReceiver(screenOnOffReceiver);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onDestroy: screenOnOffReceiver is unregistered.");
}
}
}
在下面的步骤中进行以上活动。
3。在Android后台服务中注册和注销广播接收器
在活动中注册广播接收器时,活动退出后它将停止。
要解决此问题,我们将创建一个android服务对象,并在该服务对象中注册和注销广播接收器。
因为活动退出后android服务对象仍将在后台运行,所以广播接收器也将在android应用退出后仍运行。
3.1创建Android服务类。
3.1.1创建一个扩展android.app.Service的Java类。
ScreenOnOffBackgroundService.java
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import ScreenOnOffReceiver;
public class ScreenOnOffBackgroundService extends Service {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
// Create an IntentFilter instance.
IntentFilter intentFilter = new IntentFilter();
// Add network connectivity change action.
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
// Set broadcast receiver priority.
intentFilter.setPriority(100);
// Create a network change broadcast receiver.
screenOnOffReceiver = new ScreenOnOffReceiver();
// Register the broadcast receiver with the intent filter object.
registerReceiver(screenOnOffReceiver, intentFilter);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onCreate: screenOnOffReceiver is registered.");
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregister screenOnOffReceiver when destroy.
if(screenOnOffReceiver!=null)
{
unregisterReceiver(screenOnOffReceiver);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onDestroy: screenOnOffReceiver is unregistered.");
}
}
}
3.1.2在AndroidManifest.xml文件中添加服务Xml标签。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="put your own package">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".broadcast.activity.ScreenOnOffActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".broadcast.service.ScreenOnOffBackgroundService" />
</application>
</manifest>
3.1.3将活动Java代码更改为以下。
请注意启动服务对象的Java代码。
Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
startService(backgroundService);
ScreenOnOffActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.dev2qa.example.R;
import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
import com.dev2qa.example.broadcast.service.ScreenOnOffBackgroundService;
public class ScreenOnOffActivity extends AppCompatActivity {
private ScreenOnOffReceiver screenOnOffReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_on_off);
setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
startService(backgroundService);
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onCreate");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onDestroy");
}
}
再次运行示例,您可以看到下图。从logcat输出中,我们可以看到android应用退出后,广播接收器仍在运行。