Android中在service服务中监听Back键

如何在服务中监听Back、音量键?

大体思路:在服务中创建一个悬浮窗,悬浮窗中的View添加一个自定义View;在自定义的View中重写dispatchKeyEvent、onKeyDown、onKeyUp都可,则实现了Back、音量键的监听

此种方法是从网上看到的原创网址找不到了,此处为转载

 

1.自定义View

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

public class MonitorView extends View {
    private static final String LOG_TAG = "MonitorView";
    public MonitorView(Context context) {
        super(context);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.d("suhuazhi", "keyCode " + event.getKeyCode());
        return super.dispatchKeyEvent(event);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                Log.i(LOG_TAG, "KeyEvent.KEYCODE_BACK");
                break;
            case KeyEvent.KEYCODE_MENU:
                Log.i(LOG_TAG, "KeyEvent.KEYCODE_MENU");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                Log.i(LOG_TAG, "音量减小");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                Log.i(LOG_TAG, "音量增大");
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        
        return super.onKeyUp(keyCode, event);
    }
}

2.服务中添加悬浮窗(悬浮窗中视图使用自定义的View)

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.WindowManager;

import com.lidroid.xutils.cache.LruDiskCache;

import java.util.ArrayList;

public class TestService extends Service {


    @Override
    public void onCreate() {
        super.onCreate();
        showWindow();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public static WindowManager mWindowManager;
    public static MonitorView mMonitorView;
    private void showWindow() {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            mMonitorView = new MonitorView(TestService.this);
        }
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                1, 1, //Must be at least 1x1
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                //Don't know if this is a safe default
                PixelFormat.TRANSLUCENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0+,不设置这个flag可能会报错
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        params.format= PixelFormat.RGBA_4444;//设置背景图片
        //Don't set the preview visibility to GONE or INVISIBLE
        mWindowManager.addView(mMonitorView, params);
    }
    public static void hideWindow() {
        if(null != mWindowManager) {
            mWindowManager.removeView(mMonitorView);
        }
    }
    public static void addWindow(){
        if(null != mWindowManager) {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    1, 1,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                    PixelFormat.TRANSLUCENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0+,不设置这个flag可能会报错
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            params.format= PixelFormat.RGBA_4444;//设置背景图片
            mWindowManager.addView(mMonitorView,params);
        }
    }
}

3.清单文件AndroidManifest.xml中添加

你可能感兴趣的:(Android,Studio)