Android 唤起app的多种方式

来源:互联网 发布:淘宝评论怎么写100字 编辑:程序博客网 时间:2024/06/05 07:02

方式一(通过Intent唤起):

我们自己的app代码:

ComponentName componetName = new ComponentName(                               "com.lh.jimtrency.webviewdemo","com.lh.jimtrency.webviewdemo.MainActivity"); //(另外一个应用程序的包名,要启动的Activity )  Bundle bundle = new Bundle();  ArrayList<String> strings=new ArrayList<>();  strings.add("18883250894");  strings.add("浮夸的小白菜");  bundle.putStringArrayList("userInfo", strings);  Intent intent = new Intent();  intent.putExtras(bundle);  intent.setComponent(componetName);  startActivity(intent);

PS:com.lh.jimtrency.webviewdemo 为对方的包名
com.lh.jimtrency.webviewdemo.MainActivity 为对方的MainActivity类

上面的代码就是唤起了对方App的MainActivity类,那对方还需要怎么配置呢?其实,只需要在AndroidManifest.xml中,对的MainActivity配置时,加上这个属性(exported):

  <activity        android:name=".MainActivity"        android:exported="true"/>

对方怎么接受呢?

Bundle bundle=getIntent().getExtras();if (bundle!=null){       Toast.makeText(this,    bundle.getStringArrayList("userInfo").toString()   ,Toast.LENGTH_SHORT).show();}

方式二(通过Uri唤起app):

其实也很简单,就是换了一种方式而已。但,读者,一定要对uri格式有一定连接才行(uri格式详解:http://blog.csdn.net/harvic880925/article/details/44679239)。

下面看看,我们端app的代码怎么写:

Uri uri = Uri.parse("jimtrency://user.info.detail?password=1");Intent intent = new Intent("android.jimtrency.schemeurl.activity");intent.setData(uri);startActivity(intent);

PS: “android.jimtrency.schemeurl.activity” 为跳转时的action

“jimtrency” 为Uri的scheme

“user.info.detail” 为authority

对方 app怎么接受呢?

    Intent intent = getIntent();    if (null != intent) {        Uri uri = intent.getData();        if (uri == null) {            return;        }        String acionData = uri.getQueryParameter("password");        Toast.makeText(this,acionData,Toast.LENGTH_SHORT).show();    }

对方app的AndroidManifest.xml的配置

<activityandroid:name=".MainActivity"><intent-filter>    <action android:name="android.jimtrency.schemeurl.activity" />    <category android:name="android.intent.category.DEFAULT" />    <data        android:scheme="jimtrency"        android:host="user.uri.activity" /></intent-filter></activity>

特别强调:

uri跳转时 ,action 配置 和 category 配置,一定不能缺。category 配置是固定的。如下:

   <category android:name="android.intent.category.DEFAULT" />

方法三(简单粗暴:直接通过包名唤起app):

PackageManager packageManager = getPackageManager();Intent intent=new Intent();intent = packageManager.getLaunchIntentForPackage("com.cmcc.jzfpb");startActivity(intent);

那要是没有对应的app怎么办?其实,你可以打卡对应的下载页面就行.

Intent view = new Intent ("android.intent.action.VIEW",Uri.parse(""));startActivity(viewIntent);