Android service后台检测全局触摸事件

自转载:http://kpbird.blogspot.com/2013/03/android-detect-global-touch-event.html

 

侧栏和glvebox应用程序一夜之间就变得流行起来,因为只有一种功能。它们提供全局菜单,可以从移动屏幕的左右边沿打开。作为一名开发人员,当我展示这些应用程序时,我首先想到的是“NDK”。然后,我开始寻找解决方案。从逻辑上讲,我想如果我能发现全球OnTouch事件“然后可以显示侧菜单,如侧栏或字条框。

我正在寻找Android系统中的全球OnTouch事件,最后我获得了成功。韩永斌。他介绍了一种通过siepanel应用程序检测全局触摸事件的技术。在本文中,我将解释在Android中检测全局事件的简化代码。

问题1:检测全球触觉事件
问题2:显示滑动菜单。

问题1的解决办法:
1.创建检测触摸事件的服务。
2.从系统服务中获取WindowManager对象
3.创建具有30 PX宽度和Fill_Parent高度的LinearLayout
4.将LinearLayout添加到WindowsManager
5.将LinearLayout设置在屏幕/窗口的左侧边缘。

 

Q1:我为什么要创造服务?
当活动处于背景或关闭时,我们无法获得活动。

问题2:如果我使LinearLayout宽度超过30 PX会怎样?
显示在后台的控件或活动将不会获得OnTouch事件。
 

青色是用来使线层可见。


让我们跳到密码。

步骤1:创建名为“全球触摸事件”的Android项目
步骤2:打开Activitymain.xml并添加按钮来启动服务。

<RelativeLayout 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"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Start Touch Detection"
        android:onClick="buttonClicked" />
</RelativeLayout>

步骤3:打开MainActivity.java并添加按钮Cliced事件来启动服务。

package com.kpbird.globaltouchevent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

 Intent globalService;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  globalService = new Intent(this,GlobalTouchService.class);
 }


 public void buttonClicked(View v){
  
  if(v.getTag() == null){
   startService(globalService);
   v.setTag("on");
   Toast.makeText(this, "Start Service", Toast.LENGTH_SHORT).show();
  }
  else{
   stopService(globalService);
   v.setTag(null);
   Toast.makeText(this, "Stop Service", Toast.LENGTH_SHORT).show();
  }
  
 }
}

步骤4:创建名为“GlobalTouchService”的新java类,它扩展了Service并实现了OnTouchListener。现在创建LinearLayout并将其添加到WindowManager中,当用户触摸屏幕的左边边缘时,LinearLayout将检测到事件。如果您想查看LinearLayout,只需在背景中设置任何颜色,或者在下面的源代码中取消设置背景颜色行。

package com.kpbird.globaltouchevent;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class GlobalTouchService extends Service implements OnTouchListener{

 private String TAG = this.getClass().getSimpleName();
 // window manager 
 private WindowManager mWindowManager;
 // linear layout will use to detect touch event
 private LinearLayout touchLayout;
 @Override
 public IBinder onBind(Intent arg0) {
  return null;
 }
 @Override
 public void onCreate() {
  super.onCreate();
  // create linear layout
  touchLayout = new LinearLayout(this);
  // set layout width 30 px and height is equal to full screen
  LayoutParams lp = new LayoutParams(30, LayoutParams.MATCH_PARENT);
  touchLayout.setLayoutParams(lp);
  // set color if you want layout visible on screen
//  touchLayout.setBackgroundColor(Color.CYAN); 
  // set on touch listener
  touchLayout.setOnTouchListener(this);

  // fetch window manager object 
   mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
   // set layout parameter of window manager
   WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
           30, // width of layout 30 px
           WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
                 WindowManager.LayoutParams.TYPE_PHONE, // Type Phone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus  
                 PixelFormat.TRANSLUCENT);      
         mParams.gravity = Gravity.LEFT | Gravity.TOP;   
   Log.i(TAG, "add View");

      mWindowManager.addView(touchLayout, mParams);
  
 }
 

 @Override
 public void onDestroy() {
   if(mWindowManager != null) {
             if(touchLayout != null) mWindowManager.removeView(touchLayout);
         }
  super.onDestroy();
 }
 
 @Override
 public boolean onTouch(View v, MotionEvent event) {
  if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP)
   Log.i(TAG, "Action :" + event.getAction() + "\t X :" + event.getRawX() + "\t Y :"+ event.getRawY());
  
  return true;
 }

}

步骤5:打开AndroidManifest.xml并注册“GlobalTouchService”,我们还需要system_警报_Window权限才能在WindowManager中添加视图。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kpbird.globaltouchevent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="GlobalTouchService" >
        </service>
    </application>
</manifest>


步骤6:准备运行这个应用程序。

从GitHub下载或使用Fork代码 
我将在下一篇文章中讨论问题2。

参考文献
1. http://developer.android.com/reference/android/view/WindowManager.html 
2.http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
3. https://github.com/sukso96100/SidePanel
4. http://stackoverflow.com/questions/6714020/how-can-a-service-listen-for-touch-gestures-events

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值