Android 唤起app的多种方式

方式一(通过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.uri.activity?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.uri.activity” 为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的配置

<activity
android: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);
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值