Java 如何使按钮类SOS功能化,即使活动在android中被破坏
嗨,我有一个任务,使一个按钮(图像视图,按钮,文本,无论什么)仍然可见和功能,即使活动被破坏。我尝试使用服务,并在创建活动时运行该服务。我动态创建了按钮并将其放置在窗口管理器的窗口中。为此编写了函数。 代码如下:Java 如何使按钮类SOS功能化,即使活动在android中被破坏,java,android,Java,Android,嗨,我有一个任务,使一个按钮(图像视图,按钮,文本,无论什么)仍然可见和功能,即使活动被破坏。我尝试使用服务,并在创建活动时运行该服务。我动态创建了按钮并将其放置在窗口管理器的窗口中。为此编写了函数。 代码如下: public class FloatingBubbleService extends Service { WindowManager windowManager; ImageView floatingFaceBubble; public void onCreate() {
public class FloatingBubbleService extends Service {
WindowManager windowManager;
ImageView floatingFaceBubble;
public void onCreate() {
Intent i = new Intent(FloatingBubbleService.this,
PreHomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
super.onCreate();
floatingFaceBubble = new ImageView(this);
//a face floating bubble as imageView
floatingFaceBubble.setImageResource(R.drawable.sos_icon);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//here is all the science of params
final WindowManager.LayoutParams myParams = new
WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x = 0;
myParams.y = 100;
// add a floatingfacebubble icon in window
windowManager.addView(floatingFaceBubble, myParams);
try {
//for moving the picture on touch and slide
floatingFaceBubble.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams paramsT = myParams;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
private long touchStartTime = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
//remove face bubble on long press
if (System.currentTimeMillis() - touchStartTime >
ViewConfiguration.getLongPressTimeout() && initialTouchX ==
event.getX()) {
windowManager.removeView(floatingFaceBubble);
stopSelf();
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStartTime = System.currentTimeMillis();
initialX = myParams.x;
initialY = myParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() -
initialTouchX);
myParams.y = initialY + (int) (event.getRawY() -
initialTouchY);
windowManager.updateViewLayout(v, myParams);
break;
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
floatingFaceBubble.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent not = new Intent(FloatingBubbleService.this,
CreateNotificationActivity.class);
not.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(not);
}
});
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.hasExtra("isToHide") &&
intent.getBooleanExtra("isToHide",Boolean.FALSE)){
floatingFaceBubble.setVisibility(View.GONE);
}
if(intent.hasExtra("isToShow") &&
intent.getBooleanExtra("isToShow",Boolean.TRUE)){
if(floatingFaceBubble!=null)
floatingFaceBubble.setVisibility(View.VISIBLE);
}
return Service.START_STICKY;
}
}
您可以像WindowManager一样添加到浮动视图的参数吗? 您必须在视图中添加参数选项
params = new WindowManager.LayoutParams(
70,
70,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(testView, params);
inflateView = View.inflate(this, R.layout.floatwindow, null);
floatWindowParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
现在,即使活动停止,它也能工作。但我希望它即使在活动被破坏的情况下也能正常运行..我决定。。。。因为这是一项服务,所以我将其设置为Start Sticky,并创建了一个forground流程。。就像这个startForeground(1,notifySOS.build());