Android 在app里面打开另一个app
程序员文章站
2022-06-04 19:41:55
...
Android 在app里面打开另一个app
一、方式1:打开外部app,在新窗口打开。
//打开外部app,新窗口打开
private void doStartApplicationWithPackageName(String packagename) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
//BDToast.showToast(getText(R.string.app_not_found).toString());
Toast.makeText(getApplicationContext(), "app_not_found", Toast.LENGTH_SHORT).show();
return;
}
else
Toast.makeText(getApplicationContext(), "open successfully", Toast.LENGTH_SHORT).show();
Intent resolveIntent = getPackageManager().getLaunchIntentForPackage(packagename);// 这里的packname就是从上面得到的目标apk的包名
startActivity(resolveIntent);// 启动目标应用
}
二、方式2:打开外部app,嵌入旧窗口中。
//打开外部app,嵌入旧窗口中
private void doStartApplicationWithPackageNameE(String packagename) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
Toast.makeText(getApplicationContext(), "app_not_found", Toast.LENGTH_SHORT).show();
return;
} else
Toast.makeText(getApplicationContext(), "open successfully", Toast.LENGTH_SHORT).show();
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = 参数packname
String packageName = resolveinfo.activityInfo.packageName;
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 设置ComponentName参数1:packagename参数2:MainActivity路径
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
三、测试
1、java源码
package com.example.lancher_demo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
findViewById(R.id.btn_custom_property).setOnClickListener(this);
findViewById(R.id.btn_StartApplication).setOnClickListener(this);
}
//打开外部app,嵌入旧窗口中
private void doStartApplicationWithPackageNameE(String packagename) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
Toast.makeText(getApplicationContext(), "app_not_found", Toast.LENGTH_SHORT).show();
return;
} else
Toast.makeText(getApplicationContext(), "open successfully", Toast.LENGTH_SHORT).show();
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = 参数packname
String packageName = resolveinfo.activityInfo.packageName;
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 设置ComponentName参数1:packagename参数2:MainActivity路径
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
//打开外部app,新窗口打开
private void doStartApplicationWithPackageName(String packagename) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
//BDToast.showToast(getText(R.string.app_not_found).toString());
Toast.makeText(getApplicationContext(), "app_not_found", Toast.LENGTH_SHORT).show();
return;
}
else
Toast.makeText(getApplicationContext(), "open successfully", Toast.LENGTH_SHORT).show();
Intent resolveIntent = getPackageManager().getLaunchIntentForPackage(packagename);// 这里的packname就是从上面得到的目标apk的包名
startActivity(resolveIntent);// 启动目标应用
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_custom_property) {
//doStartApplicationWithPackageName("com.android.calculator2");
doStartApplicationWithPackageNameE("com.google.android.deskclock");
}else if (view.getId() == R.id.btn_StartApplication) {
doStartApplicationWithPackageName("com.android.calculator2");
}
}
}
2、测试运行效果
上一篇: Tomcat的性能监控及调优
推荐阅读
-
RN在Android打包发布App(详解)
-
ionic App 解决android端在真机上tab处于顶部的问题
-
RN在Android打包发布App(详解)
-
Android 启动另一个App/apk中的Activity实现代码
-
Android 再次打开APP进入按Home键退出时的界面(thisTaskRoot)
-
微信下载app需要点击右上角在浏览器中打开下载的问题
-
关于android studio打开后自动弹出:Error:Execution failed for task ':app:preDebugAndroidTestBuild'
-
苹果Mac os系统中在终端下打开APP应用程序方法介绍
-
weex添加模板成功后,在android studio工具上调试报错:Error:Executionfailedfortask':app:transformCl
-
Android开发之APP安装后在桌面上不显示应用图标的解决方法