美文网首页
android 调起第三方APK方式

android 调起第三方APK方式

作者: a532628831fb | 来源:发表于2018-11-05 11:40 被阅读0次

B Activity 调起A Activity

方法一、通过包名调起,只知道包名

B Activity代码:

<pre>PackageManager packageManager = getPackageManager();

Intent intent = packageManager.getLaunchIntentForPackage("com.example.admin.aactivity");

intent.putExtra("token","1234567890");

startActivity(intent);

</pre>

方法二、通过包名调起具体界面

B Activity代码:

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.setComponent(new ComponentName("com.example.admin.aactivity","com.example.admin.aactivity.MainActivity"));

intent.putExtra("token","1234567890");

startActivity(intent);

以上两个方法A Activity解析如下:

A Activity代码:

String token = getIntent().getStringExtra("token");

Toast.makeText(this,token,Toast.LENGTH_LONG).show();

方法三:通过action方式

B Activity代码:

Intent intent =new Intent("com.bread.test");//A Activity对于activity的action

intent.putExtra("token","12345678911");

startActivity(intent);

A 代码:

AndroidManifest.xml:

<intent-filter>

    <action android:name="com.bread.test"/>

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

</intent-filter>

方法四:通过URL Scheme

重点讲述这个方法。

URL Scheme是android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

协议如下:

Scheme链接格式样式:[scheme]://[host]:[port]/[path]?[query]

String urlStr="bread://com.bread.test:8080/bz?token=12345&name=bz";

//协议scheme=bread

//域名host=com.bread.test

//端口port=8080

//页面path= /bz

//参数query=token=12345&name=bz

B代码:

Uri data = Uri.parse("bread://com.bread.test:8080/bz?token=12345&name=bz");

Intent intent =new Intent(Intent.ACTION_VIEW,data);

//保证新启动的APP有单独的堆栈,如果希望新启动的APP和原有APP使用同一个堆栈则去掉该项

//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {

startActivityForResult(intent,RESULT_OK);

}catch (Exception e) {

e.printStackTrace();

Toast.makeText(MainActivity.this,"没有匹配的APP,请下载安装",Toast.LENGTH_SHORT).show();

}

A代码:

AndroidManifest.xml:

<intent-filter> <!--URL Scheme启动-->

    <!--必有项-->

    <action android:name="android.intent.action.VIEW"/>

    <!--如果希望该应用可以通过浏览器的连接启动,则添加该项-->

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

    <!--表示该页面可以被隐式调用,必须加上该项-->

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

    <!--协议部分-->

    <data android:scheme="bread"

        android:host="com.bread.test"

        android:port="8080"

        android:path="/bz" />

</intent-filter>

A Activity:

Intent i_getvalue = getIntent();

String action = i_getvalue.getAction();

if(Intent.ACTION_VIEW.equals(action)){

uri = i_getvalue.getData();

Log.d(TAG,"scheme data: scheme " +uri.getScheme());

Log.d(TAG,"scheme data: host " +uri.getHost());

Log.d(TAG,"scheme data: port " +uri.getPort());

Log.d(TAG,"scheme data: path " +uri.getPath());

Log.d(TAG,"scheme data: Query " +uri.getQuery());

Log.d(TAG,"scheme data: token " +uri.getQueryParameter("token"));

Log.d(TAG,"scheme data: name " +uri.getQueryParameter("name"));

}

还有其他启动方式 后面再补充

相关文章

网友评论

      本文标题:android 调起第三方APK方式

      本文链接:https://www.haomeiwen.com/subject/egpjxqtx.html