启动第三方应用时弹出仿touch悬浮按钮以退出第三方应用,返回launcher界面

页面按钮,弹出第三方应用,同时弹出touch悬浮按钮
case R.id.ll_radio_left://音乐
    try {
        startService(new Intent(HomeAtyNew.this, AuxiliaryService.class));
        Intent intentUhome = new Intent();
        PackageManager packageManager = getPackageManager();
        intentUhome = packageManager.getLaunchIntentForPackage("com.spotify.music");
        intentUhome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentUhome);
    } catch (Exception ex) {
        ex.toString();
        Toast.makeText(this, getResources().getString(R.string.install_apk), Toast.LENGTH_LONG).show();
    }
 
AuxiliaryService.java文件:
package com.unilife.fridge.haierbase.tftifa.ui.activity.main;



import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.unilife.fridge.haierbase.tftifa.ui.widget.EasyTouchView;


public class AuxiliaryService extends Service implements EasyTouchView.ServiceListener {
    private Intent mIntent;

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

    public void onCreate() {
        super.onCreate();
        new EasyTouchView(this, this).initTouchViewEvent();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mIntent = intent;
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void OnCloseService(boolean isClose) {
        stopService(mIntent);
    }
}

弹出的EasyTouchView控件代码:代码中点击该控件可以弹出菜谱对话框,这里省略,点击时退出第三方应用,控件消失。
EasyTouchView.java
package com.unilife.fridge.haierbase.tftifa.ui.widget;

import android.app.ActivityManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.unilife.fridge.haierbase.tftifa.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class EasyTouchView extends View {
    private Context mContext;
    private WindowManager mWManager;
    private LayoutParams mWMParams;
    private View mTouchView;
    private ImageView mIconImageView = null;
    private PopupWindow mPopuWin;
    private ServiceListener mSerLisrener;
    private View mSettingTable;
    private int mTag = 0;
    private int midX;
    private int midY;
    private int mOldOffsetX;
    private int mOldOffsetY;

    private Toast mToast;
    private Timer mTimer = null;
    private TimerTask mTask = null;

    public EasyTouchView(Context context, ServiceListener listener) {
        super(context);
        mContext = context;
        mSerLisrener = listener;
    }

    public void initTouchViewEvent() {
        initEasyTouchViewEvent();

        initSettingTableView();
    }

    private void initEasyTouchViewEvent() {
        mWManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        midX = mWManager.getDefaultDisplay().getWidth() / 2 - 25;
        midY = mWManager.getDefaultDisplay().getHeight() / 2 - 44;
        mTouchView = LayoutInflater.from(mContext).inflate(R.layout.easy_touch_view, null);
        mIconImageView = (ImageView) mTouchView.findViewById(R.id.easy_touch_view_imageview);
        mTouchView.setBackgroundColor(Color.TRANSPARENT);

        mTouchView.setOnTouchListener(mTouchListener);
        WindowManager wm = mWManager;
        LayoutParams wmParams = new LayoutParams();
        mWMParams = wmParams;
        wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
        wmParams.flags = 40;
        wmParams.width = 100;
        wmParams.height = 100;
        wmParams.format = -3;
        wm.addView(mTouchView, wmParams);
    }

    private void initSettingTableView() {
        mSettingTable = LayoutInflater.from(mContext).inflate(R.layout.show_setting_table, null);
        Button commonUseButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_common_use_button);
        Button screenLockButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_screen_lock_button);
        Button notificationButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_notification_button);

        Button phoneButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_phone_button);
        Button pageButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_page_button);
        Button cameraButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_camera_button);

        Button backButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_back_button);
        Button homeButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_home_button);
        Button exitTouchButton = (Button) mSettingTable.findViewById(R.id.show_setting_table_item_exit_touch_button);

        commonUseButton.setOnClickListener(mClickListener);
        screenLockButton.setOnClickListener(mClickListener);
        notificationButton.setOnClickListener(mClickListener);

        phoneButton.setOnClickListener(mClickListener);
        pageButton.setOnClickListener(mClickListener);
        cameraButton.setOnClickListener(mClickListener);

        backButton.setOnClickListener(mClickListener);
        homeButton.setOnClickListener(mClickListener);
        exitTouchButton.setOnClickListener(mClickListener);
    }

    private OnClickListener mClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.show_setting_table_item_common_use_button:
                    hideSettingTable("常用");
                    break;
                case R.id.show_setting_table_item_screen_lock_button:
                    hideSettingTable("锁屏");
                    break;
                case R.id.show_setting_table_item_notification_button:
                    hideSettingTable("通知");
                    break;

                case R.id.show_setting_table_item_phone_button:
                    hideSettingTable("电话");
                    break;
                case R.id.show_setting_table_item_page_button:
                    hideSettingTable("1");
                    break;
                case R.id.show_setting_table_item_camera_button:
                    hideSettingTable("相机");
                    break;

                case R.id.show_setting_table_item_back_button:
                    if (isBackground(mContext)) {
//                        Log.e("touch","isBackground = true"+",android.os.Process.myPid()"+android.os.Process.myPid());
//                        PackageManager pm = mContext.getPackageManager();
//                        ResolveInfo homeInfo =
//                                pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
//                        ActivityInfo ai = homeInfo.activityInfo;
//                        Intent startIntent = new Intent(Intent.ACTION_MAIN);
//                        startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//                        Log.e("EasyTouch",""+ai.packageName+","+ai.name);
//                        startIntent.setComponent(new ComponentName("com.h.fileinput", "com.h.fileinput.TestActivity"));
//                        startActivitySafely(startIntent);
                        Toast.makeText(mContext, "isBackground(mContext)==ture", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.e("touch", "isBackground = false");
                        PackageManager pm = mContext.getPackageManager();
                        ResolveInfo homeInfo =
                                pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
                        ActivityInfo ai = homeInfo.activityInfo;
                        Intent startIntent = new Intent(Intent.ACTION_MAIN);
                        startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                        Log.e("EasyTouch", "" + ai.packageName + "," + ai.name);
                        startIntent.setComponent(new ComponentName("com.unilife.fridge.haierbase.tftifa", "com.unilife.fridge.haierbase.tftifa.ui.activity.main.HomeAtyNew"));
                        startActivitySafely(startIntent);
                    }
                    quitTouchView();
                    break;
                case R.id.show_setting_table_item_home_button:
                    hideSettingTable("主页");
                    break;
                case R.id.show_setting_table_item_exit_touch_button:
                    quitTouchView();
                    break;
            }

        }
    };

    private void startActivitySafely(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(mContext, "null",
                    Toast.LENGTH_SHORT).show();
        } catch (SecurityException e) {
            Toast.makeText(mContext, "null",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    Log.i("后台", appProcess.processName);
                    return true;
                } else {
                    Log.i("前台", appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }

    private void quitTouchView() {
        hideSettingTable("quit");

        mWManager.removeView(mTouchView);
        mSerLisrener.OnCloseService(true);

        clearTimerThead();
    }

    private OnTouchListener mTouchListener = new OnTouchListener() {
        float lastX, lastY;
        int paramX, paramY;

        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();

            float x = event.getRawX();
            float y = event.getRawY();

            if (mTag == 0) {
                mOldOffsetX = mWMParams.x;
                mOldOffsetY = mWMParams.y;
            }

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    motionActionDownEvent(x, y);
                    break;

                case MotionEvent.ACTION_MOVE:
                    motionActionMoveEvent(x, y);
                    break;

                case MotionEvent.ACTION_UP:
                    motionActionUpEvent(x, y);
                    break;

                default:
                    break;
            }

            return true;
        }

        private void motionActionDownEvent(float x, float y) {
            lastX = x;
            lastY = y;
            paramX = mWMParams.x;
            paramY = mWMParams.y;
        }

        private void motionActionMoveEvent(float x, float y) {
            int dx = (int) (x - lastX);
            int dy = (int) (y - lastY);
            mWMParams.x = paramX + dx;
            mWMParams.y = paramY + dy;
            mTag = 1;

            mWManager.updateViewLayout(mTouchView, mWMParams);
        }

        private void motionActionUpEvent(float x, float y) {
            int newOffsetX = mWMParams.x;
            int newOffsetY = mWMParams.y;
            if (Math.abs(mOldOffsetX - newOffsetX) > 50 || Math.abs(mOldOffsetY - newOffsetY) > 50) {
                mTag = 0;
            } else {
                PackageManager pm = mContext.getPackageManager();
                ResolveInfo homeInfo =
                        pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
                ActivityInfo ai = homeInfo.activityInfo;
                Intent startIntent = new Intent(Intent.ACTION_MAIN);
                startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                Log.e("EasyTouch", "" + ai.packageName + "," + ai.name);
                startIntent.setComponent(new ComponentName("com.unilife.fridge.haierbase.tftifa", "com.unilife.fridge.haierbase.tftifa.ui.activity.main.HomeAtyNew"));
                startIntent.putExtra("music", "music");
                startActivitySafely(startIntent);
                quitTouchView();
            }
        }
    };

    private void catchSettingTableDismiss() {
        mTimer = new Timer();
        mTask = new TimerTask() {

            @Override
            public void run() {
                if (mPopuWin == null || !mPopuWin.isShowing()) {
                    handler.sendEmptyMessage(0x0);
                }
            }
        };

        mTimer.schedule(mTask, 0, 100);
    }

    private void clearTimerThead() {
        if (mTask != null) {
            mTask.cancel();
            mTask = null;
        }

        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            mIconImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.touch_ic));
        }

        ;
    };

    public void showToast(Context context, String text) {
        if (mToast == null) {
            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    private void hideSettingTable(String content) {
        hideSettingTable();
        showToast(mContext, content);
    }

    private void hideSettingTable() {
        if (null != mPopuWin) {
            mPopuWin.dismiss();
        }
    }

    public interface ServiceListener {
        public void OnCloseService(boolean isClose);
    }
}

 
easy_touch_view.xml:点击页面按钮,弹出的悬浮图标:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/easy_touch_view_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/touch_ic" />

</LinearLayout>

show_setting_table.xml:点击悬浮图标弹出的菜单对话框,这里用不到。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="20dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/show_setting_table_item_common_use_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="常用" />

        <Button
            android:id="@+id/show_setting_table_item_screen_lock_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="锁屏" />

        <Button
            android:id="@+id/show_setting_table_item_notification_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通知" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/show_setting_table_item_phone_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话" />

        <Button
            android:id="@+id/show_setting_table_item_page_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:id="@+id/show_setting_table_item_camera_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="相机" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/show_setting_table_item_back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回" />

        <Button
            android:id="@+id/show_setting_table_item_home_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主页" />

        <Button
            android:id="@+id/show_setting_table_item_exit_touch_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出" />

    </LinearLayout>

</LinearLayout>
示例图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值