android AlertDialog响应返回键两次 20
在Service里建立了一个AlertDialog,其他一切正常,就是在设置响应返回键的时候,每按一次返回键,会响应两次,各位大神帮忙解读一下!publicclassFl...
在Service里建立了一个AlertDialog,其他一切正常,就是在设置响应返回键的时候,每按一次返回键,会响应两次,各位大神帮忙解读一下!
public class FloatService extends Service{
WindowManager.LayoutParams wmParams;
AlertDialog dialog;
Button button;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
System.out.println(" on create");
AlertDialog.Builder b = new AlertDialog.Builder(getApplicationContext());
//b.setView(mFloatLayout);
dialog = b.create();
//Dialog不依赖于Activity存在就必须设置windowtype为SystemDialg或者KeyguardDialog,
//否则会出现BadTokenException的问题,但是TYPE_SYSTEM_ALERT这样就会覆盖掉所有的Activity和window
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//show过后再设置布局的各种参数,才能起作用!!!!!
dialog.show();
dialog.setContentView(R.layout.floattest);
wmParams = dialog.getWindow().getAttributes();
//调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
//横屏
wmParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
//全屏,包括状态栏
wmParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
//透明
//wmParams.format = PixelFormat.TRANSLUCENT;
//以屏幕左上角为原点,设置x y 初始值,相对于gravity
wmParams.x = 0;
wmParams.y = 0;
//设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
//mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
dialog.getWindow().setAttributes(wmParams);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
System.out.println("get key code");
}
return false;//false还会响应源代码,true只响应自己的代码
}
});
button = (Button)dialog.getWindow().findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("button click!!");
}
});
}
}
找到答案了:Android一次单击操作定义了两个事件,ACTION_DOWN和ACTION_UP,即按下和松手两个动作。响应时改成
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP){
System.out.println("get back key");
}
就可以了 展开
public class FloatService extends Service{
WindowManager.LayoutParams wmParams;
AlertDialog dialog;
Button button;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
System.out.println(" on create");
AlertDialog.Builder b = new AlertDialog.Builder(getApplicationContext());
//b.setView(mFloatLayout);
dialog = b.create();
//Dialog不依赖于Activity存在就必须设置windowtype为SystemDialg或者KeyguardDialog,
//否则会出现BadTokenException的问题,但是TYPE_SYSTEM_ALERT这样就会覆盖掉所有的Activity和window
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//show过后再设置布局的各种参数,才能起作用!!!!!
dialog.show();
dialog.setContentView(R.layout.floattest);
wmParams = dialog.getWindow().getAttributes();
//调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
//横屏
wmParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
//全屏,包括状态栏
wmParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
//透明
//wmParams.format = PixelFormat.TRANSLUCENT;
//以屏幕左上角为原点,设置x y 初始值,相对于gravity
wmParams.x = 0;
wmParams.y = 0;
//设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
//mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
dialog.getWindow().setAttributes(wmParams);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
System.out.println("get key code");
}
return false;//false还会响应源代码,true只响应自己的代码
}
});
button = (Button)dialog.getWindow().findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("button click!!");
}
});
}
}
找到答案了:Android一次单击操作定义了两个事件,ACTION_DOWN和ACTION_UP,即按下和松手两个动作。响应时改成
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP){
System.out.println("get back key");
}
就可以了 展开
2个回答
展开全部
service里创建AlertDialog?这不科学啊。UI部分请放到主线程,service里不要做UI操作。
追问
是这样,我的A.apk会调用到另一个B.apk,要求是A.apk和B.apk同时运行,并且A.apk 在最上层、有控件能操作、透明、能够观察到B.apk的运行,那如果A.apk是activity的话应该是不行吧,B.apk是不是会onPause,所以我就把A.apk做成一个Service,并创建一个AlertDialog代替activity,这样的思想对吗?有木有其他方法可以代替?多多指教
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询