Android打开第三方应用方法总结
12018.09.30 09:52:58字数 245阅读 10,391
在Android开发中,有很多地方都需要启动其他的程序,调起第三方应用(“QQ”、“微信”、“支付宝”、“高德地图”等),还有一些系统应用(“电话”、“短信”、“拍照”、“图库”等),下面一起来看看如何启动这些常用应用。
1. 第三方应用
1)通过包名启动
PackageManager packageManager = this.getPackageManager();
Intent intent= packageManager.getLaunchIntentForPackage("com.tencent.mobileqq");
startActivity(intent);
2)通过包名和类名启动
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);
2. 系统应用
1)从google搜索内容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"搜索内容")
startActivity(intent);
2)浏览网页
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
3)显示地图
Uri uri = Uri.parse("geo:36.899533,66.036476");
Intent intent = newIntent(Intent.Action_VIEW,uri);
startActivity(intent);
4)路径规划
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);
5)拨打电话
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
startActivity(intent);
6)发短信
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "SMS Text");
startActivity(intent);
7)发送彩信
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);
8)发送Email
Uri uri = Uri.parse("mailto:123456@qq.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
9)播放多媒体
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
intent.setDataAndType(uri,"audio/mp3");
startActivity(intent);
10)设置界面
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(intent);
11)拍照
// 打开拍照程序
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
// 取出照片数据
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
12)选择图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 2);
13)打开录音机
Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(intent);
14)卸载软件
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
15)安装软件
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);
3. 场景应用
检查版本更新,跳转到腾讯应用宝进行下载更新
/** 跳转到腾讯应用宝下载软件 */
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);
}
}
/** 启动到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();
}
}
/** 判断软件是否存在 */
public static boolean isAvilible(Context context, String packageName) {
try {
context.getPackageManager().getPackageInfo(packageName, 0);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
4. 应用包名