Android 监听home、锁屏、解屏简单实现

activity 代码:

package com.demo;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class KeyListenActivity extends ListActivity implements Update {

    private SimpleAdapter listItemAdapter;
    ArrayList> listItems = new ArrayList>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent
                    .ACTION_MAIN)) {
                finish();
                return;
            }
        }
        setContentView(R.layout.activity_applist);
        TextView textView = findViewById(R.id.title);
        textView.setText("手机监听");
        initListView();
        this.setListAdapter(listItemAdapter);
        registerHomeKeyReceiver(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "onDestroy");
        unregisterHomeKeyReceiver(this);
    }

    //自定义的广播接收者
    private HomeWatcherReceiver mHomeKeyReceiver = null;
    String LOG_TAG = "TAG";

    //注册广播接收者,监听Home键
    private void registerHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "registerHomeKeyReceiver");
        mHomeKeyReceiver = new HomeWatcherReceiver(this);
        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        homeFilter.addAction(Intent.ACTION_SCREEN_OFF);
        homeFilter.addAction(Intent.ACTION_SCREEN_ON);
        homeFilter.addAction(Intent.ACTION_USER_PRESENT);
        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

    //取消监听广播接收者
    private void unregisterHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");
        if (null != mHomeKeyReceiver) {
            context.unregisterReceiver(mHomeKeyReceiver);
        }
    }

    @Override
    public void updateList(int type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        HashMap map = new HashMap();
        System.out.println("type==============="+type);
        if (type == 0) {
            map.put("ItemTitle", "监听到一次home按键");
            map.put("ItemPackage", simpleDateFormat.format(date));
        } else if (type == 1) {
            map.put("ItemTitle", "监听到一次锁屏");
            map.put("ItemPackage", simpleDateFormat.format(date));
        } else if (type == 2) {
            map.put("ItemTitle", "监听到一次解屏");
            map.put("ItemPackage", simpleDateFormat.format(date));
        }

        listItems.add(map);
        //重新设置适配器
        KeyListenActivity.this.setListAdapter(listItemAdapter);
    }

    private void initListView()   {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        HashMap map = new HashMap();
        map.put("ItemTitle", "开始监听");
        map.put("ItemPackage", simpleDateFormat.format(date));
        listItems.add(map);

        listItemAdapter = new SimpleAdapter(this, listItems,
                R.layout.list_item,
                new String[] {"ItemTitle", "ItemPackage"},
                new int[ ] {R.id.ItemTitle, R.id.ItemPackage}
        );
    }
}

 

监听类代码:

package com.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class HomeWatcherReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "HomeReceiver";
    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
    private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";

    private Update update;

    public HomeWatcherReceiver(Update update) {
        this.update = update;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        Log.i(LOG_TAG, "=======================1===========");
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            // android.intent.action.CLOSE_SYSTEM_DIALOGS
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            Log.i(LOG_TAG, "==================2================");

            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                // 短按Home键
                Log.i(LOG_TAG, "==================3================");

                update.updateList(0);
            }
            else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
                // 长按Home键 或者 activity切换键
                Log.i(LOG_TAG, "long press home key or activity switch");

            }
            else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
                // 锁屏
                Log.i(LOG_TAG, "lock");

            }
            else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
                // samsung 长按Home键
                Log.i(LOG_TAG, "assist");
            }

        } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i(LOG_TAG, "==================5===============");
            update.updateList(1);
        } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
            Log.i(LOG_TAG, "==================6===============");
            update.updateList(2);
        }
    }

}

 

这里有个地方要注意,就是监听锁屏和解屏必须动态注册广播。

    private void registerHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "registerHomeKeyReceiver");
        mHomeKeyReceiver = new HomeWatcherReceiver(this);
        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        homeFilter.addAction(Intent.ACTION_SCREEN_OFF);
        homeFilter.addAction(Intent.ACTION_SCREEN_ON);
        homeFilter.addAction(Intent.ACTION_USER_PRESENT);
        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

 

你可能感兴趣的:(android)