實戰Android:通過BroadcastReceiver監聽Home,電源Power,和音量變化Volume鍵
阿新 • • 發佈:2019-01-25
上一個例子是採用AccessibilityService來實現按鍵的監聽。這次我們採用BroadcastReceiver來完成按鍵的監聽。
缺點:我嘗試了一下,暫時還不知道如何停止按 鍵的預設行為,比如我確實監聽到了電源按鍵,但卻沒法阻止此刻螢幕變黑的行為。先在這記下。以後找到解決辦法再補充。
注意點:
1。監聽Home鍵的相關字串。
注意下邊的註釋,這幾個字串是Android系統已經內建好了的,必須是這樣,比如你寫成home_key,就不會反應。
final String SYSTEM_DIALOG_REASON_KEY = "reason";//按下Home鍵 final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; // 系統內建字串,表示home鍵 == 長按Home鍵 final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; // 系統內建字串,停止鍵
2。監聽Volume鍵的字串。
volume鍵的IntentFilter和Home,Power鍵不一樣,沒有相關ACTION,我只找到了這個,確實能監聽到音量按鍵,但假設已經達到最大音量或最小音量,再往極限調,音量不會變化,就監聽不到了。
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
好了,下面先看監聽輸出,我的手機大致如此,
原始碼如下,
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spacesoftwares.spacecapture"> <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=".MainActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".service.WhiteService" android:enabled="true" android:exported="false" /> </application> </manifest>
MainActivity.java
package com.spacesoftwares.spacecapture; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.spacesoftwares.spacecapture.service.WhiteService; public class MainActivity extends AppCompatActivity { private Button mBtnWhite, mBtnStopWhite, mBtnExit; private final static String TAG = MainActivity.class.getSimpleName(); private Intent whiteIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnWhite = findViewById(R.id.btn_white); mBtnStopWhite = findViewById(R.id.btn_stop_white); mBtnExit = findViewById(R.id.btn_exit); setListener(); } @Override protected void onDestroy() { super.onDestroy(); } class ExitReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MainActivity.this.finish(); } } private void setListener(){ OnClick onClick = new OnClick(); mBtnWhite.setOnClickListener(onClick); mBtnStopWhite.setOnClickListener(onClick); mBtnExit.setOnClickListener(onClick); } private class OnClick implements View.OnClickListener{ @Override public void onClick(View view) { int viewId = view.getId(); switch(viewId){ case R.id.btn_white: Log.i(TAG, "MAIN: btn_white"); if(null == whiteIntent) whiteIntent = new Intent(MainActivity.this, WhiteService.class); startService(whiteIntent); break; case R.id.btn_stop_white: Log.i(TAG, "MAIN: btn_stop_white"); if(null != whiteIntent) stopService(whiteIntent); break; case R.id.btn_exit: Log.i(TAG, "MAIN: btn_exit"); if(null != whiteIntent) stopService(whiteIntent); finish(); break; } } } }
WhiteService.java
package com.spacesoftwares.spacecapture.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.spacesoftwares.spacecapture.MainActivity;
import com.spacesoftwares.spacecapture.R;
import com.spacesoftwares.spacecapture.key.HomeKeyObserver;
import com.spacesoftwares.spacecapture.key.PowerKeyObserver;
import com.spacesoftwares.spacecapture.key.VolumeKeyObserver;
import java.io.IOException;
import java.nio.channels.Channel;
public class WhiteService extends Service {
static public boolean isPowerKeyPressed = false;
private final static String TAG = WhiteService.class.getSimpleName();
private final static int FOREGROUND_ID = 1000;
private Notification mNotification;
/**
* 參考:
* 1 https://blog.csdn.net/q445697127/article/details/8432513
* 2 https://blog.csdn.net/lfdfhl/article/details/9903693
*/
private HomeKeyObserver mHomeKeyObserver;
private PowerKeyObserver mPowerKeyObserver;
private VolumeKeyObserver mVolumeKeyObserver;
private void init() {
mHomeKeyObserver = new HomeKeyObserver(this);
mHomeKeyObserver.setHomeKeyListener(new HomeKeyObserver.OnHomeKeyListener() {
@Override
public void onHomeKeyPressed() {
Log.i(TAG,"----> 按下Home鍵");
System.out.println("----> 按下Home鍵");
}
@Override
public void onHomeKeyLongPressed() {
Log.i(TAG,"----> 長按Home鍵");
System.out.println("----> 長按Home鍵");
}
});
mHomeKeyObserver.startListen();
//////////////////////////////////////////
mPowerKeyObserver = new PowerKeyObserver(this);
mPowerKeyObserver.setPowerKeyListener(new PowerKeyObserver.OnPowerKeyListener() {
@Override
public void onPowerKeyPressed() {
Log.i(TAG,"----> 按下電源鍵");
System.out.println("----> 按下電源鍵");
isPowerKeyPressed = true;
}
});
mPowerKeyObserver.startListen();
//////////////////////////////////////////
mVolumeKeyObserver = new VolumeKeyObserver(this);
mVolumeKeyObserver.setVolumeKeyListener(new VolumeKeyObserver.OnVolumeKeyListener() {
@Override
public void onVolumeKeyPressed() {
Log.i(TAG,"----> 按下電源鍵");
System.out.println("----> 按下電源鍵");
}
});
mVolumeKeyObserver.startListen();
}
public WhiteService(){}
@Nullable
@Override
public IBinder onBind(Intent intent) {
//return null;
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
Log.i(TAG, "WhiteService->onCreate");
super.onCreate();
init();
}
@Override
public void onDestroy() {
Log.i(TAG, "WhiteService->onDestroy");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
//if(null !=mNotification)
{
stopForeground(Service.STOP_FOREGROUND_REMOVE);
}
mHomeKeyObserver.stopListen();
mPowerKeyObserver.stopListen();
mVolumeKeyObserver.stopListen();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(null != mNotification){
Log.i(TAG, "WhiteService->onStartCommand->Notification exists");
return super.onStartCommand(intent, flags, startId);
}
Log.i(TAG, "WhiteService->onStartCommand->Create Notification");
//NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Foreground");
builder.setContentText("Text shown on notification bar");
builder.setContentInfo("Content Info");
builder.setWhen(System.currentTimeMillis());
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
mNotification = builder.build();
startForeground(FOREGROUND_ID, mNotification);
return super.onStartCommand(intent, flags, startId);
}
}
HomeKeyOberver.java
package com.spacesoftwares.spacecapture.key;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.util.Objects;
public class HomeKeyObserver {
private Context mContext;
private IntentFilter mIntentFilter;
private OnHomeKeyListener mOnHomeKeyListener;
private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver;
public HomeKeyObserver(Context context) {
this.mContext = context;
}
//註冊廣播接收者
public void startListen(){
mIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mHomeKeyBroadcastReceiver=new HomeKeyBroadcastReceiver();
mContext.registerReceiver(mHomeKeyBroadcastReceiver, mIntentFilter);
System.out.println("HomeKey----> 開始監聽");
}
//取消廣播接收者
public void stopListen(){
if (mHomeKeyBroadcastReceiver!=null) {
mContext.unregisterReceiver(mHomeKeyBroadcastReceiver);
System.out.println("HomeKey----> 停止監聽");
}
}
// 對外暴露介面
public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) {
mOnHomeKeyListener = homeKeyListener;
}
// 回撥介面
public interface OnHomeKeyListener {
void onHomeKeyPressed();
void onHomeKeyLongPressed();
}
//廣播接收者
class HomeKeyBroadcastReceiver extends BroadcastReceiver{
final String SYSTEM_DIALOG_REASON_KEY = "reason";
//按下Home鍵
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; // 系統內建字串,表示home鍵
//長按Home鍵
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; // 系統內建字串,停止鍵
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Objects.equals(action, Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null && mOnHomeKeyListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
mOnHomeKeyListener.onHomeKeyPressed();
} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
mOnHomeKeyListener.onHomeKeyLongPressed();
}
}
}
}
}
}
PowerKeyObserver.java
package com.spacesoftwares.spacecapture.key;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import java.util.Objects;
public class PowerKeyObserver {
private String TAG_POWER = "PowerKey";
private Context mContext;
private IntentFilter mIntentFilter;
private OnPowerKeyListener mOnPowerKeyListener;
private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver;
public PowerKeyObserver(Context context) {
this.mContext = context;
}
//註冊廣播接收者
public void startListen(){
mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF);
mPowerKeyBroadcastReceiver=new PowerKeyBroadcastReceiver();
mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter);
Log.i(TAG_POWER, "PowerKey----> 開始監聽");
System.out.println("PowerKey----> 開始監聽");
}
//取消廣播接收者
public void stopListen(){
if (mPowerKeyBroadcastReceiver!=null) {
mContext.unregisterReceiver(mPowerKeyBroadcastReceiver);
Log.i(TAG_POWER, "PowerKey----> 停止監聽");
System.out.println("PowerKey----> 停止監聽");
}
}
// 對外暴露介面
public void setPowerKeyListener(OnPowerKeyListener powerKeyListener) {
mOnPowerKeyListener = powerKeyListener;
}
// 回撥介面
public interface OnPowerKeyListener {
void onPowerKeyPressed();
}
//廣播接收者
class PowerKeyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Objects.equals(action, Intent.ACTION_SCREEN_OFF)) {
mOnPowerKeyListener.onPowerKeyPressed();
//abortBroadcast();
}
}
}
}
VolumeKeyObserver.java
package com.spacesoftwares.spacecapture.key;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import java.util.Objects;
public class VolumeKeyObserver {
private String TAG_VOLUME = "VOLUME";
private Context mContext;
private IntentFilter mIntentFilter;
private VolumeKeyObserver.OnVolumeKeyListener mOnVolumeKeyListener;
private VolumeKeyObserver.VolumeKeyBroadcastReceiver mVolumeKeyBroadcastReceiver;
public VolumeKeyObserver(Context context) {
this.mContext = context;
}
//註冊廣播接收者
public void startListen(){
//mIntentFilter = new IntentFilter(Intent.ACTION_VOICE_COMMAND);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
mVolumeKeyBroadcastReceiver=new VolumeKeyObserver.VolumeKeyBroadcastReceiver();
mContext.registerReceiver(mVolumeKeyBroadcastReceiver, mIntentFilter);
System.out.println("VolumeKey----> 開始監聽");
}
//取消廣播接收者
public void stopListen(){
if (mVolumeKeyBroadcastReceiver!=null) {
mContext.unregisterReceiver(mVolumeKeyBroadcastReceiver);
System.out.println("VolumeKey----> 停止監聽");
}
}
// 對外暴露介面
public void setVolumeKeyListener(VolumeKeyObserver.OnVolumeKeyListener VolumeKeyListener) {
mOnVolumeKeyListener = VolumeKeyListener;
}
// 回撥介面
public interface OnVolumeKeyListener {
void onVolumeKeyPressed();
}
//廣播接收者
class VolumeKeyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Objects.equals(action, "android.media.VOLUME_CHANGED_ACTION")) {
Log.i(TAG_VOLUME, "VolumeKey----> 監聽到了音量調節");
System.out.println("VolumeKey----> 監聽到了音量調節");
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/service_white" />
<Button
android:id="@+id/btn_stop_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stop_white" />
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/exit" />
</LinearLayout>
另外我也上傳的完整的程式碼,可以直接下載執行,