• Android之App跳转其他软件



    前言

    最近遇到一个需求,就是App内大面积需要长按复制并跳转指定App,没办法,只能埋头苦干呐,废话不多说,直接干!


    一、效果图

    在这里插入图片描述

    二、实现步骤

    1.弹框xml(自己替换图标)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
        <LinearLayout
            android:id="@+id/ll_share"
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/bzhs_bff_8"
            android:gravity="center_horizontal"
            android:orientation="vertical">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="@string/Pleaseselectanapp"
                    android:textColor="#232323"
                    android:textSize="16dp"
                    android:textStyle="bold" />
    
                <ImageView
                    android:id="@+id/imag_gb"
                    android:layout_width="39dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="16dp"
                    android:scaleType="center"
                    android:src="@mipmap/ico_gban" />
            </RelativeLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="41dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="45dp"
                android:orientation="horizontal">
    
                <LinearLayout
                    android:id="@+id/cancle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/telefram" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="Telegram"
                        android:textColor="#232323"
                        android:textSize="16dp"></TextView>
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/confirm"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/whatsapp" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="WhatsApp"
                        android:textColor="#232323"
                        android:textSize="16dp"></TextView>
                </LinearLayout>
    
            </LinearLayout>
    
        </LinearLayout>
    
    
    </RelativeLayout>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    2.弹框utils

    package com.example.merchant.utils;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.view.Gravity;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.NumberPicker;
    import android.widget.TextView;
    
    import com.example.merchant.R;
    
    import java.util.Calendar;
    
    
    /**
     * Created by :caoliulang
     * ❤
     * Creation time :2023/9/01
     * ❤
     * Function :APP选择弹框
     */
    public class APPDialog extends Dialog {
        Context context;
        MenuListener mMenuListener;
        View mRootView;
        private Animation mShowAnim;
        private Animation mDismissAnim;
        private boolean isDismissing;
        LinearLayout cancle;//取消
        LinearLayout confirm;//确定
        ImageView imag_gb;//关闭
    
        public APPDialog(Context context) {
            super(context, R.style.ActionSheetDialog);
            this.context = context;
            getWindow().setGravity(Gravity.BOTTOM);
            initView(context);
        }
    
        private void initView(final Context context) {
            mRootView = View.inflate(context, R.layout.app_dialog, null);
            cancle = mRootView.findViewById(R.id.cancle);
            imag_gb=mRootView.findViewById(R.id.imag_gb);
            confirm = mRootView.findViewById(R.id.confirm);
            imag_gb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mMenuListener.onGb();
                }
            });
            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMenuListener.onSelect();
                }
            });
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
    
            this.setContentView(mRootView);
            initAnim(context);
            setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    if (mMenuListener != null) {
                        mMenuListener.onCancel();
                    }
                }
            });
    
        }
    
        private void initAnim(Context context) {
            mShowAnim = AnimationUtils.loadAnimation(context, R.anim.translate_up);
            mDismissAnim = AnimationUtils.loadAnimation(context, R.anim.translate_down);
            mDismissAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    dismissMe();
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
        }
    
    
        @Override
        public void show() {
            super.show();
            mRootView.startAnimation(mShowAnim);
        }
    
        @Override
        public void dismiss() {
            if (isDismissing) {
                return;
            }
            isDismissing = true;
            mRootView.startAnimation(mDismissAnim);
        }
    
        private void dismissMe() {
            super.dismiss();
            isDismissing = false;
        }
    
    
        public MenuListener getMenuListener() {
            return mMenuListener;
        }
    
        public void setMenuListener(MenuListener menuListener) {
            mMenuListener = menuListener;
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU) {
                dismiss();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
        public interface MenuListener {
            void onCancel();
    
            void onSelect();
            void onGb();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149

    3.两个弹框动画

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromYDelta="100%"
               android:toYDelta="0"
               android:duration="250">
    </translate>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromYDelta="0%"
               android:toYDelta="100%"
               android:duration="250">
    </translate>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.封装方便调用

    package com.example.merchant.utils
    
    import android.content.Context
    import android.view.Window
    import android.view.WindowManager
    import com.example.merchant.R
    
    /**
     * @Author : CaoLiulang
     * @Time : 2023/9/27 14:42
     * @Description :
     */
    class AppTk {
        companion object {
            private lateinit var appDialog: APPDialog
    
            /**
             * app选择弹框
             */
            fun showTimeDailog(message: String, context: Context) {
                appDialog = APPDialog(context)
                CopyUtils.copy(message, context)
                val window: Window = appDialog.window!!
                val lp = window.attributes
                //这句就是设置dialog横向满屏了。
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = lp
                appDialog.show()
                appDialog.setCanceledOnTouchOutside(false)
                appDialog.menuListener = object : APPDialog.MenuListener {
                    //Telegram
                    override fun onCancel() {
                        if (appDialog != null) {
                            appDialog.dismiss()
                            //数据线连接设备命令输入adb shell pm list packages查看所有应用包名
                            // 通过包名获取要跳转的app,创建intent对象
                            val intent =
                                context.packageManager.getLaunchIntentForPackage("org.telegram.messenger.web") // 这里如果intent为空,就说名没有安装要跳转的应用嘛
                            if (intent != null) {
                                // 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样
                                context.startActivity(intent)
                            } else {
                                // 没有安装要跳转的app应用,提醒一下
                                ToastUtils.showToast(context.resources.getString(R.string.Youhavenotinstalledthissoftwareyet))
                            }
                        }
                    }
    
                    //WhatsApp
                    override fun onSelect() {
                        if (appDialog != null) {
                            appDialog.dismiss()
                            //数据线连接设备命令输入adb shell pm list packages查看所有应用包名
                            // 通过包名获取要跳转的app,创建intent对象
                            val intent =
                                context.packageManager.getLaunchIntentForPackage("com.whatsapp") // 这里如果intent为空,就说名没有安装要跳转的应用嘛
                            if (intent != null) {
                                // 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样
                                context.startActivity(intent)
                            } else {
                                // 没有安装要跳转的app应用,提醒一下
                                ToastUtils.showToast(context.resources.getString(R.string.Youhavenotinstalledthissoftwareyet))
                            }
                        }
                    }
    
                    override fun onGb() {
                        appDialog.dismiss()
                    }
    
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    5.调用

    AppTk.showTimeDailog(
                    text.text.toString(),
                    this
                )
    
    • 1
    • 2
    • 3
    • 4

    6.长按事件方法

     //长按事件
        fun setCAListener(text: TextView) {
            text.setOnLongClickListener(View.OnLongClickListener {
                AppTk.showTimeDailog(
                    text.text.toString(),
                    this
                )
                true
            })
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    7.跳转步骤

    1:数据线连接设备,AS命令输入adb shell pm list packages查看所有应用包名

    adb shell pm list packages
    
    • 1

    2:通过报名获取要跳转的app

     // 通过包名获取要跳转的app,创建intent对象
                            val intent =
                                context.packageManager.getLaunchIntentForPackage("org.telegram.messenger.web") // 这里如果intent为空,就说名没有安装要跳转的应用嘛
                            if (intent != null) {
                                // 这里跟Activity传递参数一样的嘛,不要担心怎么传递参数,还有接收参数也是跟Activity和Activity传参数一样
                                context.startActivity(intent)
                            } else {
                                // 没有安装要跳转的app应用,提醒一下
                                ToastUtils.showToast(context.resources.getString(R.string.Youhavenotinstalledthissoftwareyet))
                            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    8.复制utils

    package com.example.merchant.utils
    
    import android.content.ClipboardManager
    import android.content.Context
    import android.content.Context.CLIPBOARD_SERVICE
    import com.example.merchant.R
    
    /**
     * @Author : CaoLiulang
     * @Time : 2023/9/27 14:11
     * @Description :复制工具类
     */
    class CopyUtils {
    
        companion object {
            fun copy(messsage: String?, context: Context) {
                var cm = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                cm!!.text = messsage // 复制
            }
            fun copyts(messsage: String?, context: Context) {
                var cm = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                cm!!.text = messsage // 复制
                ToastUtils.showToast(context.getString(R.string.Copysuccessfully))
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    总结

    实现很简单,就两步几行代码完美收工,喜欢点个赞,不喜欢点个关注谢谢!

  • 相关阅读:
    瓦斯抽采VR应急救援模拟仿真系统筑牢企业安全生产防线
    运维工程师面试题及答案(网络运维工程师面试题)
    【MySQL从0到1】第六篇:内置函数
    DDD诊所——异步事件综合征
    怎么把大视频发到微信上?关键时刻很实用!
    FAT12文件系统详解
    23ccpc(最长上升子序列题解)
    “拍视频”成为小程序的基础能力
    Python实战小项目分享
    运动蓝牙耳机排行榜,目前最好的运动耳机推荐
  • 原文地址:https://blog.csdn.net/Android_Cll/article/details/133641381