gpt4 book ai didi

单击后退按钮时,Android 关闭系统覆盖窗口(从服务启动)

转载 作者:太空狗 更新时间:2023-10-29 14:53:34 25 4
gpt4 key购买 nike

我在单击 float View (类似于 FB 聊天头)时打开了一个窗口(显示在其他 Activity 之上),如何在按下设备后退按钮时关闭窗口。下面是我的服务代码

public class ServiceFloating extends Service {

public static int ID_NOTIFICATION = 2018;
private WindowManager windowManager;
private ImageView chatHead;
private PopupWindow pwindo;
boolean mHasDoubleClicked = false;
long lastPressTime;
private Boolean _enable = true;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
super.onCreate();

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

chatHead = new ImageView(this);

chatHead.setImageResource(R.drawable.floating2);


final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;

windowManager.addView(chatHead, params);

try {
chatHead.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams paramsF = params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;

@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();


// If double click...
if (pressTime - lastPressTime <= 300) {
createNotification();
ServiceFloating.this.stopSelf();
mHasDoubleClicked = true;
}
else { // If not double click....
mHasDoubleClicked = false;
}
lastPressTime = pressTime;
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, paramsF);
break;
}
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}

chatHead.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
showOptions(chatHead);
_enable = false;
// Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// getApplicationContext().startActivity(intent);
}
});

}

public void showOptions(View anchor){
windowManager.removeView(anchor);
chatHead = null;
View myView = View.inflate(getApplicationContext(),R.layout.alert_layout,null);
ImageView img1,img2,img3;
img1 = (ImageView) myView.findViewById(R.id.id_img1);
img2 = (ImageView) myView.findViewById(R.id.id_img2);
img3 = (ImageView) myView.findViewById(R.id.id_img3);
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"img1 Clicked",Toast.LENGTH_LONG).show();

}
});
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.CENTER;
//params.x = 0;
//params.y = 100;

windowManager.addView(myView, params);

}



@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
// TODO Auto-generated method stub
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);

//Restart the service once it has been killed android


AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);

}

我无法在服务中添加 onBackPressed 或 onKeyEvent 方法。请提出解决方法。时间差

最佳答案

您应该只处理来自 Activity 的 View ,并使用服务绑定(bind)或广播等通信组件。您不应该从服务创建 View ,这不合适。

关于单击后退按钮时,Android 关闭系统覆盖窗口(从服务启动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33307817/

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