简体   繁体   English

即使活动在android中被破坏,如何使按钮类型的SOS起作用

[英]How to make the button kind of SOS functional even if the activity is destroyed in android

Hi i have a task of making a button (imageview, button, text, whatever) still be visible and functional even if the activity is destroyed. 嗨,我有一个任务,即使活动被销毁,按钮(图像视图,按钮,文本等)也仍然可见并起作用。 I tried with using service and run that service whenevr the activity is created. 我尝试使用服务,并在创建活动时运行该服务。 I created button dynamically and placing it in Window Manager's window. 我动态创建了按钮并将其放置在Window Manager的窗口中。 wrote function for that. 为此写了功能。 The codes as follows: 代码如下:

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;

    }

}

Could you add to Params yours floatview like WindowManager? 您可以像WindowManager一样将您的floatview添加到Params吗? You have to add params options floatview. 您必须添加params选项floatview。

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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使用户销毁了该如何在android中运行应用程序? - how to make an application in android run even if the user destroyed it? 即使活动和应用程序被破坏,如何继续继续上传任务? - How to keep continue uploading task even when activity and application destroyed? Android - 即使方法完成后侦听器对象也不会被销毁? - Android - How listener object is not destroyed even after the method is finished? Android Activity被销毁时如何销毁本机进程 - How to destroy Native process when Android Activity is destroyed Android:服务销毁(onDestroy)时如何调用活动? - Android: How do I call an activity when service is destroyed (onDestroy)? Android 处理程序在活动被销毁后仍会继续运行一段时间 - Android Handler keeps running for a while even after the activity has been destroyed 即使调用者 Activity 被破坏,短期运行的 Android 后台任务也可以安全地忽略,这对吗? - Is that right that a short running Android background task can safely be ignored even if the caller Activity is destroyed? 你如何在 android 中创建一个按钮来打开一个新的活动,这个活动不仅仅是一个默认活动,它是一个自定义活动 - How do you make a button in android that opens a new activity and this activity is not just a default activity its a custom one 即使不需要ScrollView,如何使Activity可滚动? - how to make an Activity scrollable even if no ScrollView is needed? Android Studio:使按钮打开下一个活动 - Android Studio: Make a Button open Next Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM