Android之App跳转其他软件

2023-10-07 16:36

本文主要是介绍Android之App跳转其他软件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、效果图
  • 二、实现步骤
    • 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"><LinearLayoutandroid: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"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="24dp"><TextViewandroid: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" /><ImageViewandroid: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><LinearLayoutandroid: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"><LinearLayoutandroid:id="@+id/cancle"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/telefram" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Telegram"android:textColor="#232323"android:textSize="16dp"></TextView></LinearLayout><LinearLayoutandroid:id="@+id/confirm"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/whatsapp" /><TextViewandroid: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() {@Overridepublic void onClick(View view) {mMenuListener.onGb();}});confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mMenuListener.onSelect();}});cancle.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {cancel();}});this.setContentView(mRootView);initAnim(context);setOnCancelListener(new OnCancelListener() {@Overridepublic 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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {dismissMe();}@Overridepublic void onAnimationRepeat(Animation animation) {}});}@Overridepublic void show() {super.show();mRootView.startAnimation(mShowAnim);}@Overridepublic 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;}@Overridepublic 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.utilsimport 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_PARENTlp.height = WindowManager.LayoutParams.WRAP_CONTENTwindow.attributes = lpappDialog.show()appDialog.setCanceledOnTouchOutside(false)appDialog.menuListener = object : APPDialog.MenuListener {//Telegramoverride 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))}}}//WhatsAppoverride 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.utilsimport 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 ClipboardManagercm!!.text = messsage // 复制}fun copyts(messsage: String?, context: Context) {var cm = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManagercm!!.text = messsage // 复制ToastUtils.showToast(context.getString(R.string.Copysuccessfully))}}
}

总结

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

这篇关于Android之App跳转其他软件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/159032

相关文章

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl