开启系统应用
通过查看系统源码,使用隐式意图来开启系统自带应用
拨打电话
//须在清单文件中添加权限
//<uses-permission android:name="android.permission.CALL_PHONE" />
public void onClick(View view) {
String phonenumber = "133xxxx3333";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenumber));
//自检查权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent);
}
或者
public void onClick(View view) {
//温馨提示:请勿随便拨打“110”,后果自负
Uri uri = Uri.parse("tel:110");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}
打开网页
public void onClick(View view) {
Uri uri = Uri.parse("http://www.hao123.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
打开应用市场
使用场景:引导用户去给 app 评分,或者在别的应用中直接跳转到 app 市场进行下载某个应用
/**
* 跳转到应用市场
*
* @param appPkg
* 已上传到应用市场的app的包名,不一定是本项目的包名
* 若为null,则默认是获取本项目的包名
* 会直接搜索到对应的app供用户下载和评论
* @param marketPkg
* 应用市场的包名
* 若为null,则系统会弹出市场的列表让你进行选择
*/
private void goToAppMarket(String appPkg, String marketPkg){
if (appPkg == null){
appPkg = getPackageName();
}
Uri uri = Uri.parse("market://details?id=" + appPkg);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 如果没给市场的包名,则系统会弹出市场的列表让你进行选择。
if (marketPkg != null) {
intent.setPackage(marketPkg);
}
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//AppContext 源码 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/awt/AppContext.java#AppContext
Toast.makeText(this, "尚未安装应用市场,无法评分", Toast.LENGTH_SHORT).show();
}
}
/**
* 进行跳转
*/
private void goToAppMarket(){
goToAppMarket("这里换成需要跳转到的 app 的包名", null);
}
开启三方应用
打开微信
//首先要判断手机是否装了微信
public static boolean isWeixinInstalled(Context context) {
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals("com.tencent.mm")) {
return true;
}
}
}
return false;
}
//打开微信
private void openWeixin() {
Intent intent = new Intent();
ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivity(intent);
}
public void onClick(View view) {
//首先要判断手机是否装了微信
if (isWeixinInstalled(this)) {
//打开微信
openWeixin();
}
}