Android打开第三方应用方法总结
程序员文章站
2022-05-25 12:48:16
...
在Android开发中,有很多地方都需要启动其他的程序,调起第三方应用(“QQ”、“微信”、“支付宝”、“高德地图”等),还有一些系统应用(“电话”、“短信”、“拍照”、“图库”等),下面一起来看看如何启动这些常用应用。
1. 第三方应用
1)通过包名启动
<span style="color:#444444"><code>PackageManager packageManager = this.getPackageManager();
Intent intent= packageManager.getLaunchIntentForPackage("com.tencent.mobileqq");
startActivity(intent);
</code></span>
2)通过包名和类名启动
<span style="color:#444444"><code>Intent intent = new Intent();
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.SplashActivity");
intent.setComponent(comp);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
</code></span>
2. 系统应用
1)从google搜索内容
<span style="color:#444444"><code>Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"搜索内容")
startActivity(intent);
</code></span>
2)浏览网页
<span style="color:#444444"><code>Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
</code></span>
3)显示地图
<span style="color:#444444"><code>Uri uri = Uri.parse("geo:36.899533,66.036476");
Intent intent = newIntent(Intent.Action_VIEW,uri);
startActivity(intent);
</code></span>
4)路径规划
<span style="color:#444444"><code>Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
Intent intent = newIntent(Intent.ACTION_VIEW,URI);
startActivity(intent);
</code></span>
5)拨打电话
<span style="color:#444444"><code>Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
startActivity(intent);
</code></span>
6)发短信
<span style="color:#444444"><code>Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "SMS Text");
startActivity(intent);
</code></span>
7)发送彩信
<span style="color:#444444"><code>Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hello");
Uri uri = Uri.parse("content://media/external/images/media/23");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
</code></span>
8)发送Email
<span style="color:#444444"><code>Uri uri = Uri.parse("mailto:[email protected]");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
</code></span>
9)播放多媒体
<span style="color:#444444"><code>Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
intent.setDataAndType(uri,"audio/mp3");
startActivity(intent);
</code></span>
10)设置界面
<span style="color:#444444"><code>Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(intent);
</code></span>
11)拍照
<span style="color:#444444"><code> // 打开拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
</code></span>
<span style="color:#444444"><code> // 取出照片数据
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
</code></span>
12)选择图片
<span style="color:#444444"><code>Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 2);
</code></span>
13)打开录音机
<span style="color:#444444"><code>Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(intent);
</code></span>
14)卸载软件
<span style="color:#444444"><code>Uri uri = Uri.fromParts("package", strPackageName, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
</code></span>
15)安装软件
<span style="color:#444444"><code>String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");
startActivity(intent);
</code></span>
3. 场景应用
检查版本更新,跳转到腾讯应用宝进行下载更新
<span style="color:#444444"><code>/** 跳转到腾讯应用宝下载软件 */
public static void goThirdApp() {
if (isAvilible(this, "com.tencent.android.qqdownloader")) {// 市场存在
startAppStore(getApplicationContext(), "项目包名", "com.tencent.android.qqdownloader");
} else {
Uri uri = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=项目包名");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
}
}
</code></span>
<span style="color:#444444"><code>/** 启动到app详情界面 */
public static void startAppStore(Context context, String appPkg, String marketPkg) {
try {
if (TextUtils.isEmpty(appPkg))return;
Uri uri = Uri.parse("market://details?id=" + appPkg);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (!TextUtils.isEmpty(marketPkg)) {
intent.setPackage(marketPkg);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
</code></span>
<span style="color:#444444"><code>/** 判断软件是否存在 */
public static boolean isAvilible(Context context, String packageName) {
try {
context.getPackageManager().getPackageInfo(packageName, 0);
return true;
} catch (NameNotFoundException e) {
return false;
}
}</code></span>
上一篇: 名将司马错:历经3任秦王,位高权重得善终