import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;
import java.util.List;
/**
* @author by benny
* @date 2020/4/11 10:08
* @description 跳转第三方App管理方法
*/
public class JumpManager {
/**
* 跳转方法1:包名(com.company.name),特定Activity名拉起
*
* B应用需要在manifest文件对应Activity添加 android:exported="true"
*/
public void jumpPackageActivity(Context context, String packageName) {
Intent intent =new Intent(Intent.ACTION_MAIN);
//知道要跳转应用的包命与目标Activity
ComponentName componentName =new ComponentName(packageName, packageName +".ui.user.LoginActivity");
intent.setComponent(componentName);
//这里Intent传值
intent.putExtra("", "");
context.startActivity(intent);
}
/**
* 跳转方法2:包名(com.company.name)拉起()(这里就是进去启动页)
*/
public void jumpPackageName(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent !=null) {
intent.putExtra("type", "110");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
/**
* 跳转方法3:url拉起
* url:csd://www.app.demo/test?type=110
* B应用manifest文件需配置(注意:在原有intent-filter下方另外添加,不是在原先里面,两个同时存在)
* android:host="www.app.demo"
* android:path="/test"
* android:scheme="test" />
* 优点:不暴露包命 host path schemeA应用和B应用得规定死
*/
public void jumpUrl(Context context, String url) {
Intent intent =new Intent();
intent.setData(Uri.parse(url));
//这里Intent当然也可传递参数,但是一般情况下都会放到上面的URL中进行传递
intent.putExtra("", "");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/**
* 检查包是否存在 判断B应用是否安装
*
* @param packageName 包名
* @return boolean
*/
private boolean checkPackInfo(Context context, String packageName) {
PackageInfo packageInfo =null;
try {
packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
}catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return packageInfo !=null;
}
/**
* 判断B应用是否在后台运行并直接打开
*/
public void openApp(Context context, String packageName) {
if (checkPackInfo(context, packageName)) {
openPackage(context, packageName);
}else {
Toast.makeText(context, "没有安装" +"", Toast.LENGTH_LONG).show();
}
}
public static boolean openPackage(Context context, String packageName) {
Context pkgContext =getPackageContext(context, packageName);
Intent intent =getAppOpenIntentByPackageName(context, packageName);
if (pkgContext !=null && intent !=null) {
pkgContext.startActivity(intent);
return true;
}
return false;
}
public static IntentgetAppOpenIntentByPackageName(Context context, String packageName) {
//Activity完整名
String mainAct =null;
//根据包名寻找
PackageManager pkgMag = context.getPackageManager();
Intent intent =new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
List list = pkgMag.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
for (int i =0; i < list.size(); i++) {
ResolveInfo info = list.get(i);
if (info.activityInfo.packageName.equals(packageName)) {
mainAct = info.activityInfo.name;
break;
}
}
if (TextUtils.isEmpty(mainAct)) {
return null;
}
intent.setComponent(new ComponentName(packageName, mainAct));
return intent;
}
public static ContextgetPackageContext(Context context, String packageName) {
Context pkgContext =null;
if (context.getPackageName().equals(packageName)) {
pkgContext = context;
}else {
// 创建第三方应用的上下文环境
try {
pkgContext = context.createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
}catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return pkgContext;
}
}