Android 唤起app的多种方式
16lz
2021-01-23
方式一(通过Intent唤起):
我们自己的app代码:
ComponentName componetName = new ComponentName( "com.lh.jimtrency.webviewdemo","com.lh.jimtrency.webviewdemo.MainActivity"); //(另外一个应用程序的包名,要启动的Activity ) Bundle bundle = new Bundle(); ArrayList 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):
那对方怎么接受呢?
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的配置
特别强调:
uri跳转时 ,action 配置 和 category 配置,一定不能缺。category 配置是固定的。如下:
方法三(简单粗暴:直接通过包名唤起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);
更多相关文章
- Android Native C 之 Helloworld的四种编译方式
- 使用Git下载Google Android源代码
- Android中用代码设置ImageView的src和background
- Android 退出提示框 代码
- Android输入框格式化Edittexthelp(有项目可下载)
- 我的Android进阶之旅------>android api的源代码下载地址
- 简单几段代码实现窗口抖动
- android camera2 image 中获得的yuv数据的格式转换
- android之按钮单击事件及监听器的实现方式