描述:Android 从一个APP进入另一个APP,可传递数据跳转。比如MyApp软件跳转到YourApp的任意一个界面,可以携带一些参数,一起来看看简单的实现:
1.过于简单,无图可上,阅读描述,复制代码,即可实现。
首先要准备两个APP,先从MyApp开始了:
(进入YourAPP的首页和指定某个界面)
if(isApkInstalled(getAttachActivity(),"com.example.administrator.imagetest")){
Toast.makeText(getAttachActivity(), "应用已安装", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getAttachActivity(), "应用没有安装过", Toast.LENGTH_SHORT).show();
}
goMain();
ComponentName componentName = new ComponentName("com.example.administrator.imagetest", "com.example.administrator.imagetest.OneActivity");//这里是 包名 以及 页面类的全称
Intent intent = new Intent();
intent.setComponent(componentName);
intent.putExtra("type", "110");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// 1.在Activity上下文之外启动Activity需要给Intent设置FLAG_ACTIVITY_NEW_TASK标志,不然会报异常。
// 2.加了该标志,如果在同一个应用中进行Activity跳转,不会创建新的Task,只有在不同的应用中跳转才会创建新的Task
上面可以看到,加上了APP是否在手机上安装的判断条件,先跳转到YourAPP的首页,然后直接跳转到YourAPP的指定界面,有助于返回可以退到首页,进而进行YourAPP的其它操作。
/**
* 判断要打开的app是否安装
* @param context
* @param packageName
* @return
*/
public static boolean isApkInstalled(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) {
return false;
}else {
//已安装
}
try {
packageInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (PackageManager.NameNotFoundException e) {
// Toast.makeText(context, "应用没有安装过", Toast.LENGTH_SHORT).show();
packageInfo = null;
e.printStackTrace();
// return false;
}
if(packageInfo == null) {
// Toast.makeText(context, "应用没有安装过", Toast.LENGTH_SHORT).show();
return false;
} else {
// Toast.makeText(context, "应用已经安装", Toast.LENGTH_SHORT).show();
return true;//true为安装了,false为未安装
}
}
//或通过获取应用列表对比判断
private boolean checkAppInstalled( Context context, String pkgName) {
if (pkgName== null || pkgName.isEmpty()) {
return false
}
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> info = packageManager.getInstalledPackages(0);
if(info == null || info.isEmpty())
return false;
for ( int i = 0; i < info.size(); i++ ) {
if(pkgName.equals(info.get(i).packageName)) {
return true;
}
}
return false;
}
/**
* 跳转到另一个app的首页
*/
public void goMain(){
Intent intent = new Intent();
intent.setClassName("com.example.administrator.imagetest", "com.example.administrator.imagetest.MainActivity");
Bundle bundle = new Bundle();
bundle.putString("FIRST_APP_KEY", "你好 ,LoginActivity");
intent.putExtras(bundle);
startActivity(intent);
}
2.接下来是YourAPP的一些配置了:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.imagetest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OneActivity"
android:exported="true"
android:screenOrientation="portrait" />
</application>
</manifest>
注意:被指定的app中的类的清单文件中添加 android:exported="true",把要跳转的目标 APP 的指定 activity 暴露出来
在 AnroidManifest 文件中给 activity 节点设置 Android:exported="true",该属性表示当前 activity 能否被另外一个Application 的组件启动,true允许启动,false不允许。默认是false。(该属性是四大组件都拥有的)