大佬教程收集整理的这篇文章主要介绍了如何从信使聊天头服务等服务中检测后门按钮/主页按键?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_181_19@messenger如何处理它的聊天服务上的背压按钮? (或者我完全错了,他们不是使用Windows管理器的服务?)@H_944_21@
public class BACkButtonAwareLinearLayout extends LinearLayout { public interface BACkButtonListener { void onBACkButtonPressed(); } @Nullable private BACkButtonListener mListener; public BACkButtonAwareLinearLayout(Context context) { super(context); } public BACkButtonAwareLinearLayout(Context context,AttributeSet attrs) { super(context,attrs); } public BACkButtonAwareLinearLayout(Context context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr); } public void setBACkButtonListener(@Nullable BACkButtonListener listener) { mListener = listener; } @Override public Boolean dispatchKeyEvent(KeyEvent event) { if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK && mListener != null) { mlistener.onBACkButtonPressed(); return true; } return super.dispatchKeyEvent(event); } }
基本上,重写dispatchKeyEvent对我们来说是一个诀窍.
然后在一些xml中使用它(我称之为chat_head_container.xml):@H_944_21@
<?xml version="1.0" encoding="utf-8"?> <com.pablo432.myapplication.BACkButtonAwareLinearLayout xmlns:android="http://scheR_390_11845@as.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="48sp" android:text="Hello,world!" android:textColor="#000" android:BACkground="#f5f5f5" android:gravity="center"/> </com.pablo432.myapplication.BACkButtonAwareLinearLayout>
接下来,创建一个将我们的视图添加到WindowManager的服务(虽然我想你知道如何去做,但为了完整起见我还是会发布它):@H_944_21@
public class ChatHeadservice extends service implements BACkButtonAwareLinearLayout.BACkButtonListener { private WindowManager mWindowManager; private BACkButtonAwareLinearLayout mRootContainer; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mWindowManager = (WindowManager) getSystemservice(Context.WINDOW_serviCE); LayoutInflater inflater = (LayoutInflater) getSystemservice(Context.LAYOUT_INFLATER_serviCE); mRootContainer = (BACkButtonAwareLinearLayout) inflater.inflate( R.layout.chat_head_container,null,falsE); mRootContainer.setBACkButtonListener(this); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.FLAG_FULLSCREEN,PixelFormat.TRANSPARENT); mWindowManager.addView(mRootContainer,layoutParams); } @Override public void onBACkButtonPressed() { mRootContainer.setBACkButtonListener(null); mWindowManager.removeView(mRootContainer); } @Override public void onDestroy() { super.onDestroy(); if (mRootContainer != null) mWindowManager.removeView(mRootContainer); } }
长话短说,BACkButtonAwareLinearLayout公开了一个监听器接口,我们的服务需要实现并订阅自己的布局.@H_944_21@
还要记住,这个地址处理后退按钮.要处理主页按钮,您可能需要查看https://stackoverflow.com/a/31340960和https://stackoverflow.com/a/33580971 – 基本上我的答案是来自这两个链接https://stackoverflow.com/a/15980900的一些摘要,但包含一些调整(例如,您不能在WindowManager中设置FLAG_NOT_FOCUSABLE.的LayoutParams).@H_944_21@
当然,您需要通过调用startservice在某处启动服务,在AndroidManifest.xml中声明此服务并添加SYstem_ALERT_WINDOW权限.@H_944_21@
以上是大佬教程为你收集整理的如何从信使聊天头服务等服务中检测后门按钮/主页按键?全部内容,希望文章能够帮你解决如何从信使聊天头服务等服务中检测后门按钮/主页按键?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。