gpt4 book ai didi

android - 如何使用后退键删除WindowManager添加的 View ?

转载 作者:行者123 更新时间:2023-11-30 02:27:49 24 4
gpt4 key购买 nike

伙计们,我正在开发一个android AppLock 应用程序,我使用WindowManager 添加一个手势锁定 View 。但我有一个问题。后退键不会删除 View 。而且我无法重写 View 来覆盖 dispatchKeyEvent() 函数,因为我使用 LayoutInflater 来膨胀我的 View 。就这样:

LayoutInflater layoutInflater = 
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
enterPassView = layoutInflater.inflate(R.layout.activity_enterpass, null);

请帮我解决这个问题!谢谢。


加入UTC+8 2014/12/31 18:20

你好,很高兴我用简单的方法解决了我的问题。我使用了两个标志变量,一个是viewFlag(当我调用addView()时,viewFlag = true;当我调用removeView()时,viewFlag = false),另一个是lockedFlag(当顶级应用程序被用户锁定时添加数据库,lockedFlag = true,未锁定时,lockedFlag = false)所以,if(lockedFlag == false && viewFlag == true && view != null) {//我向处理程序发送消息以调用 removeView() }


class EnterPassView extends View {

public EnterPassView(Context context) {
super(context);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Message message = viewHandler.obtainMessage(0);
viewHandler.sendMessage(message);
return super.dispatchKeyEvent(event);
} else if(event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
Message message = viewHandler.obtainMessage(0);
viewHandler.sendMessage(message);
return super.dispatchKeyEvent(event);
} else if(event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
Message message = viewHandler.obtainMessage(0);
viewHandler.sendMessage(message);
return super.dispatchKeyEvent(event);
}
return super.dispatchKeyEvent(event);
}

}

private void showGestureLockView() {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.RGBA_8888);
params.gravity = (Gravity.CENTER);

LayoutInflater layoutInflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
enterPassView = layoutInflater.inflate(R.layout.activity_enterpass, null);

iv_icon = (ImageView) enterPassView.findViewById(R.id.enterpass_iv_icon);
tv_text = (TextView) enterPassView.findViewById(R.id.enterpass_tv_text);

animation = new TranslateAnimation(-20, 20, 0, 0);
animation.setDuration(50);
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);

SharedPreferences sp = getSharedPreferences(AppConfig.APP_SP_NAME, MODE_PRIVATE);
String savePass = sp.getString(AppConfig.GESTURE_PASS, "");
gesturePassView = (GesturePassView) enterPassView.findViewById(R.id.enterpass_view_gpv);
gesturePassView.setSavedPass(savePass);
gesturePassView.setScene(0);
gesturePassView.setOnGestureCompleteListener(new OnGestureCompleteListener() {

@Override
public void onGestureComplete(int result, String enterPassword) {
if(result == 0) {
iv_icon.setAnimation(animation);
tv_text.setText(R.string.enterpass_lesspass);
} else if(result == 1){
Message message = viewHandler.obtainMessage(0);
viewHandler.sendMessage(message);
Log.e("sendMessage", "pass is true want to remove view");
} else if(result == -1){
iv_icon.setAnimation(animation);
tv_text.setText(R.string.enterpass_wrongpass);
}
}
});

if(windowsManager == null) {
windowsManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
}
if(windowsManager != null && enterPassView != null) {
Message message = viewHandler.obtainMessage(1);
viewHandler.sendMessage(message);
Log.e("sendMessage", "want to addView");
}

}

Handler viewHandler = new Handler() {

@Override
public void handleMessage(Message message) {
int flag = message.what;
if(flag == 1) {
windowsManager.addView(enterPassView, params);
Log.e("sendMessage", "addView");
} else if(flag == 0) {
windowsManager.removeView(enterPassView);
enterPassView = null;
Log.e("sendMessage", "removeView");
}

}

};

最佳答案

这是一个可以提供帮助的伪代码。

  1. 在您的 View 中创建一个调用此方法的公共(public)方法:

    windowsManager.removeView(enterPassView);

  2. 然后在您的 Activity 中覆盖 onBackPressed。 http://developer.android.com/reference/android/app/Activity.html#onBackPressed()

  3. 在覆盖的 onBackPressed 中调用您在自定义 View 类中创建的方法。做一个 try catch 语句:

    尝试{ YourView.removeWindowView()} catch (IllegalArgumentException e){ //如果它出现在这里意味着没有 View 可以从 windowsManager 中删除 - 在这种情况下调用 super.onBackPressed();

编辑。这是解决问题的代码:

public class MainActivity extends Activity {
private MyService mServer;
ServiceConnection mConnection = new ServiceConnection() {

public void onServiceDisconnected(ComponentName name) {

}

public void onServiceConnected(ComponentName name, IBinder service) {

LocalBinder mLocalBinder = (LocalBinder) service;
mServer = mLocalBinder.getServerInstance();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
bindService(intent, mConnection, BIND_AUTO_CREATE);

}
@Override
public void onBackPressed() {
try{
mServer.removeWindow();
}
catch(IllegalArgumentException e){
super.onBackPressed();
}


}

}

public class MyService extends Service {

Thread thread;
WindowManager windowsManager = null;
WindowManager.LayoutParams params;
View view;

IBinder mBinder = new LocalBinder();


@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

public class LocalBinder extends Binder {
public MyService getServerInstance() {
return MyService.this;
}
}

@Override
public void onCreate() {
super.onCreate();
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
showView();
}

}, 5000);
}

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

private void showView() {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.RGBA_8888);
params.gravity = (Gravity.CENTER);
LayoutInflater layoutInflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.view_service, null);
if(windowsManager == null) {
windowsManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
}
if(windowsManager != null && view != null) {
Message message = addViewHandler.obtainMessage(1);
addViewHandler.sendMessage(message);
}

}

Handler addViewHandler = new Handler() {

@Override
public void handleMessage(Message message) {
int flag = message.what;
if(flag == 1) {
windowsManager.addView(view, params);
} else if(flag == 0) {
windowsManager.removeView(view);
}
}
};
public void removeWindow(){
windowsManager.removeView(view);
}

}

关于android - 如何使用后退键删除WindowManager添加的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com