在Android中使用叠加效果模拟后退按钮

时间:2014-10-06 07:14:05

标签: android overlay softkeys

我有一个有根的Android设备,想要用自己的覆盖替换软键。 这适用于app" full!screen"但我想在我自己的应用程序中这样做。 我在这里搜索并发现了一些问题,但没有答案。

我想出了如何使用shell进行操作,但它非常慢,并且#34;已经完成!屏幕"有更好的表现。

我使用shell命令的服务如下所示:

public class MainService extends Service {
ButtonView mView;
@Override
public void onCreate() {
    super.onCreate();   
    final Bitmap BckBtn = BitmapFactory.decodeResource(getResources(),
            R.drawable.back);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            BckBtn.getWidth(), 
            BckBtn.getHeight(),
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
             PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.LEFT | Gravity.BOTTOM;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    mView = new ButtonView(this,BckBtn);
    mView.setOnTouchListener(new OnTouchListener() {


        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if(arg1.getX()<BckBtn.getWidth() & arg1.getY()>0)
            {
                Process process = null;
                try {
                    process = Runtime.getRuntime().exec("su");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                OutputStream outputStream = process.getOutputStream();
                String cmd = "input keyevent 4";
                try {
                    outputStream.write((cmd + "\n").getBytes("ASCII"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
         });

    wm.addView(mView, params);

    }



@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}



@SuppressLint("DrawAllocation")
class ButtonView extends ViewGroup {

Bitmap BckBtn;

public ButtonView(Context context,Bitmap BckBtn) {
    super(context);

    this.BckBtn=BckBtn;

}


protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(BckBtn,0 , 0, null); 
}


protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}

public boolean onTouchEvent(MotionEvent event) {
    return true;
}
}

0 个答案:

没有答案