欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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、测试运行效果

Android 在app里面打开另一个app

 

 

 

 

相关标签: Android