Android之App跳转其他软件

news2024/10/6 21:50:27

文章目录

  • 前言
  • 一、效果图
  • 二、实现步骤
    • 1.弹框xml(自己替换图标)
    • 2.弹框utils
    • 3.两个弹框动画
    • 4.封装方便调用
    • 5.调用
    • 6.长按事件方法
    • 7.跳转步骤
    • 8.复制utils
  • 总结


前言

最近遇到一个需求,就是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>

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

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>
<?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>

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()
                }

            }
        }
    }
}

5.调用

AppTk.showTimeDailog(
                text.text.toString(),
                this
            )

6.长按事件方法

 //长按事件
    fun setCAListener(text: TextView) {
        text.setOnLongClickListener(View.OnLongClickListener {
            AppTk.showTimeDailog(
                text.text.toString(),
                this
            )
            true
        })
    }

7.跳转步骤

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

adb shell pm list packages

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

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

总结

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1066087.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

微信CRM系统:微商不可或缺的客户管理利器!

在这个竞争激烈的时代&#xff0c;微信客户已成为微商生存和发展的关键。如何更好地了解客户需求&#xff0c;提高客户满意度和忠诚度&#xff0c;已成为微商们亟待解决的问题。而解决这些问题&#xff0c;关键在于个微是否有一套完善的客户关系管理&#xff08;CRM&#xff09…

9.变换之平移

愿你出走半生,归来仍是少年 通过顶点着色器实现图形的整体平移。 1.知识点 1.1.齐次坐标 在GLSL 中Vec4进行坐标表达时&#xff0c;作为齐次坐标(x,y,z,w).当w1时&#xff0c;这个vec4可以表达一个点的三维坐标。在进行平移时&#xff0c;应保证偏移量的W为0。 1.2矢量相加运…

【Vue面试题三】、Vue中的v-show 和 v-if 怎么理解 ?

文章底部有个人公众号&#xff1a;热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享&#xff1f; 踩过的坑没必要让别人在再踩&#xff0c;自己复盘也能加深记忆。利己利人、所谓双赢。 面试官&#xff1a;v-show和v-if有什么区别…

Linux防火墙之--SNAT和DNAT

1.SNAT是什么 SNAT又称源地址转换。源地址转换是内网地址向外访问时&#xff0c;发起访问的内网ip地址转换为指定的ip地址&#xff08;可指定具体的服务以及相应的端口或端口范围&#xff09;&#xff0c;这可以使内网中使用保留ip地址的主机访问外部网络&#xff0c;即内网的多…

VR模拟鸡胚培养接种实验,打造沉浸式的学习环境

在医学教育领域&#xff0c;传统的鸡胚接种实验一直是教学的重要组成部分。然而&#xff0c;这种实验方法存在一定的局限性&#xff0c;如操作难度大、成本高、安全隐患等。为了解决这些问题&#xff0c;越来越多的教育机构开始尝试引入虚拟现实(VR)技术&#xff0c;以模拟鸡胚…

Safran与是德科技合作推出基于GNSS技术的5G LBS方案

概述 虹科Safran与是德科技&#xff08;Keysight&#xff09;联手推进基于全球导航卫星系统&#xff08;GNSS&#xff09;技术的5G定位服务&#xff08;LBS&#xff09;&#xff0c;利用虹科Safran先进的全球导航卫星系统&#xff08;GNSS&#xff09;仿真功能扩展是德科技的5G…

成为黄金代理,必须考虑到这一点

目前很多投资者都会选择黄金代理进行现货黄金投资账户的开立。一方面是市场中各种各样的现货黄金代理&#xff0c;越来越专业&#xff0c;提供的交易服务越来越好&#xff0c;另一方面是黄金代理和黄金平台进行合作&#xff0c;如果平台选得好&#xff0c;投资者在平台开户还是…

深入探究 C++ 编程中的资源泄漏问题

目录 1、GDI对象泄漏 1.1、何为GDI资源泄漏&#xff1f; 1.2、使用GDIView工具排查GDI对象泄漏 1.3、有时可能需要结合其他方法去排查 1.4、如何保证没有GDI对象泄漏&#xff1f; 2、进程句柄泄漏 2.1、何为进程句柄泄漏&#xff1f; 2.2、创建线程时的线程句柄泄漏 …

Android Studio 是如何和我们的手机共享剪贴板的

背景 近期完成了target33的项目适配升级,随着AGP和gradle的版本升级,万年老版本Android Studio(后文简称AS)也顺便升级到了最新版Android Studio Giraffe | 2022.3.1,除了新UI外,最让我好奇的是这次的Running Devices功能(官方也称为Device mirroring)可以控制真机了. 按照操…

静态住宅代理的优缺点以及使用方法

住宅代理ip分为静态住宅代理和动态住宅代理&#xff0c;住宅代理就是代理ip是真人网络中出发&#xff0c;以代理地址来对目标网站进行访问&#xff0c;具有高匿名度和安全性&#xff0c;在跨境业务中经常使用。而静态住宅代理的特征就是ip地址长时间固定不变&#xff0c;具有唯…

DevEco Studio下载/安装与配置开发环境

一、下载与安装DevEco Studio 在HarmonyOS应用开发学习之前&#xff0c;需要进行一些准备工作&#xff0c;首先需要完成开发工具DevEco Studio的下载与安装以及环境配置。 1.进入DevEco Studio下载官网 单击“立即下载”进入下载页面。 DevEco Studio提供了Windows版本和Mac…

html2Canvas截图中特殊字体出不来原因

项目中有正常字体和特殊字体,但是导出成图片时特殊字体无法正常显示 下图为导出的截图: 原因: 特殊字体的名称定义 因为之前特殊字体的font-family,是以数字开头,放在html中被转义了,所以一直出不来,前面改成英文后可以正常显示

让丢失成为过去,尽在我们的智能防丢器

我们都知道&#xff0c;生活中总会遇到一些小烦恼&#xff0c;比如钥匙不见了&#xff0c;钱包忘在哪里&#xff0c;甚至手机掉在了不知名的地方&#xff0c;这些看似小事&#xff0c;却足以打乱我们的日程。那么&#xff0c;有没有一种方法&#xff0c;可以让这些烦恼一扫而空…

Ubuntu 2204 搭建 nextcloud 个人网盘

Nextcloud是一套用于创建网络硬盘/云盘以存放文件的客户端-服务器软件&#xff0c;Nextcloud 完全开源并且免费。 一、搭建 ubuntu apache2 mysql php &#xff08;lamp&#xff09;环境 因为 nextcloud 服务是使用 php 语言和 mysql 数据库的web服务&#xff0c;因此需要…

【一周安全资讯1007】多项信息安全国家标准10月1日起实施;GitLab发布紧急安全补丁修复高危漏洞

要闻速览 1.以下信息安全国家标准10月1日起实施 2.GitLab发布紧急安全补丁修复高危漏洞 3.主流显卡全中招&#xff01;GPU.zip侧信道攻击可泄漏敏感数据 4.MOVEit漏洞导致美国900所院校学生信息发生大规模泄露 5.法国太空和国防供应商Exail遭黑客攻击&#xff0c;泄露大量敏感…

Safran与是德科技合作为蔚来提供电动汽车中的5G和C-V2X连接测试

概述 虹科Safran GNSS模拟器助力是德科技&#xff08;Keysight&#xff09;为中国顶级电动汽车制造商之一——蔚来汽车&#xff08;NIO&#xff09;提供了业界领先的UXM 5G测试解决方案以验证5G和C-V2X的连接性&#xff0c;能够根据3GPP和其他标准组织定义的最新5G新无线电&am…

理解ES的refresh、flush、merge

一、refresh 对于任何数据库的写入来讲fsync刷盘虽然保证的数据的安全但是如果每次操作都必须fsync一次&#xff0c;那fsync操作将是一个巨大的操作代价&#xff0c;在衡量对数据安全与操作代价下&#xff0c;ES引入了一个较轻量的操作refresh操作来避免频繁的fsync操作。 1.1…

如何优雅构建自定义 Spring Boot 验证器,让你的代码更加丝滑!

作为一名开发人员&#xff0c;你应该知道确保应用程序中流动的数据的准确性和完整性是多么重要。Spring Boot提供了强大的验证功能&#xff0c;但有时我们需要额外的验证&#xff0c;创建适合特定需求的自定义验证器。 接下来&#xff0c;我们来介绍下如何完整的创建一个自定义…

九、分枝切割算法

文章目录 1、Gomory切割的算法原理2、分枝切割算法THE END 1、Gomory切割的算法原理 \qquad 考虑有一个等式的形式如下所示&#xff1a; I L F f ILFf ILFf \qquad 其中各项满足以下性质&#xff1a; I L IL IL是一个整数值的表达式 F F F是一个严格正分数的和 f < 1 f&…

SpringBoot项目默认使用HikariDataSource数据库连接池修改使用Druid连接池

1.启动项目&#xff0c;查看正在使用的链接池。 2.在pom.xml文件中引入驱动 <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency> 3.在ap…