1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android 监听home 锁屏 解屏简单实现

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

时间:2022-03-31 13:30:28

相关推荐

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<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();@Overrideprotected 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);}@Overrideprotected 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);}}@Overridepublic void updateList(int type) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date date = new Date(System.currentTimeMillis());HashMap<String, Object> map = new HashMap<String, Object>();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<String, Object> map = new HashMap<String, Object>();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;}@Overridepublic 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_DIALOGSString 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);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。