android 4.0以上屏蔽home键,返回键等键所在一栏两种方法。

来源:互联网 发布:网络兼职诈骗能追回吗 编辑:程序博客网 时间:2024/05/20 06:53

上一次由于项目的需要,需要在app级屏蔽home键,返回键等键所在一栏。百度各种无结果。于是就自己研究了一翻。发现了两种可行的方法:

方法一:用全屏悬浮窗去掉home键,返回键等键所在一栏

代码如下(直接做一个类全部复制进去即可,不用修改):

package com.xchy.utils;


import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;


public class ShowAdService extends Service {//悬浮窗屏蔽home键


public static final String LOCK_ACTION = "lock";
public static final String UNLOCK_ACTION = "unlock";
private Context mContext;
private WindowManager mWinMng;
private View view;
private static View AdView;
private static Activity activity;
    private static ShowAdService service;
// 按下Back键的次数
private int backCount = 0;
// 退出需要的Back键次数
private final int exitBackPressedTimes = 15;


public static void showAd(Activity activity, View adView) {
AdView = adView;
ShowAdService.activity = activity;
Intent intent = new Intent(activity, ShowAdService.class);
intent.setAction(ShowAdService.LOCK_ACTION);
activity.startService(intent);
}


public static ShowAdService getService(){
return service;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onCreate() {
super.onCreate();
System.out.println("ShowAdService Started.");
service=this;
mContext = getApplicationContext();
mWinMng = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Test",
Toast.LENGTH_SHORT).show();
}
});
new ToastMessageTask().execute("This is a TOAST message!");
}


@Override
public void onDestroy() {
super.onDestroy();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
service=this;
String action = intent.getAction();
if (TextUtils.equals(action, LOCK_ACTION))
addView();
else if (TextUtils.equals(action, UNLOCK_ACTION))
exit();
return super.onStartCommand(intent, flags, startId);
}


public void addView() {
service=this;
LayoutParams param = new LayoutParams();
param.type = LayoutParams.TYPE_SYSTEM_ALERT;
// param.format = PixelFormat.RGBA_8888;
param.width = LayoutParams.MATCH_PARENT;
param.height = LayoutParams.MATCH_PARENT;
view = AdView;
// 设置按键监听

view.setFocusableInTouchMode(true);
view.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
backCount++;
if (backCount >= exitBackPressedTimes)
exit();
return true;
}
return false;
}
});

mWinMng.addView(view, param);
}


public void exit() {
removeView();
activity.finish();
stopSelf();
}


public void removeView() {
if (view != null) {
mWinMng.removeView(view);
view = null;
}
}
private class ToastMessageTask extends AsyncTask<String, String, String> {
   String toastMessage;


   @Override
   protected String doInBackground(String... params) {
       toastMessage = params[0];
       return toastMessage;
   }


   // This is executed in the context of the main GUI thread
   protected void onPostExecute(String result){
          Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
          toast.show();
   }
}
}


将上面做成一个类后,最后一步,在mainActivity的onCreate()里加这么两句:


 View inflate = View.inflate(this, R.layout.ui_main, null);//悬浮窗去掉Home键功能 

ShowAdService.showAd(this, inflate);//悬浮窗去掉Home键功能



方法二:这是一个强力的方法:
在mainAcivity里onCreate()方法里直接复制如下代码即可;
try
// 彻底隐藏状态栏
{
String ProcID = "79"; // 彻底隐藏状态栏
if ( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH )
ProcID = "42"; // 彻底隐藏状态栏
// 需要root 权限
Process proc = Runtime.getRuntime().exec(new String[]
{ "su", "-c", "service call activity " + ProcID + " s16 com.android.systemui" }); // 彻底隐藏状态栏
proc.waitFor(); // 彻底隐藏状态栏
} catch (Exception ex) // 彻底隐藏状态栏
{
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show(); // 彻底隐藏状态栏
}


在其他方法里恢复:


try
// 恢复状态栏
{
Process proc = Runtime.getRuntime().exec(new String[]
{ "am", "startservice", "-n", "com.android.systemui/.SystemUIService" }); // 恢复状态栏
proc.waitFor();
} catch (Exception e) // 恢复状态栏
{
e.printStackTrace(); // 恢复状态栏
}

0 0