朋友们在开发的时候对Intent肯定并不陌生,下面总结了一下Intent除了用在activity之间的跳转,还可以 在哪些地方使用:
1.显示网页
Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it);
2.拨打电话:
调用拨号程序分两种情况,一种是跳转到拨号界面,另一种是直接拨打电话。根据不同的需求选择不同的方式。
1)这种写法是跳转到对应的拨号界面
Uri uri = Uri.parse("tel:xxxxxx"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it);
2)这种写法是直接拨打电话
Uri uri = Uri.parse("tel.xxxxxx"); Intent it =new Intent(Intent.ACTION_CALL,uri); startActivity(it);
要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
3.发送SMS/MMS 发送短信和 拨打电话那个类似。两种情况 一种是跳转到发短信界面,另一种是直接发送短信。
1)调用发送短信的程序界面
Intent it = new Intent(Intent.ACTION_VIEW); it.putExtra("sms_body", "The SMS text"); it.setType("vnd.android-dir/mms-sms"); startActivity(it);
2)直接发送短信
Uri uri = Uri.parse("smsto:0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "The SMS text"); startActivity(it);
3)发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23"); Intent it = new Intent(Intent.ACTION_SEND); it.putExtra("sms_body", "some text"); it.putExtra(Intent.EXTRA_STREAM, uri); it.setType("image/png"); startActivity(it);
4.发送Email
Uri uri = Uri.parse("mailto:xxx@abc.com"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); startActivity(it); Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com"); it.putExtra(Intent.EXTRA_TEXT, "The email body text"); it.setType("text/plain"); startActivity(Intent.createChooser(it, "Choose Email Client"));
5.添加附件
Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); sendIntent.setType("audio/mp3"); startActivity(Intent.createChooser(it, "Choose Email Client"));
6.播放多媒体
Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/song.mp3"); it.setDataAndType(uri, "audio/mp3"); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
7.分享功能
Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); String msg = "我正在使用星空琴行软件,不错哦 ,你也快来试试看吧!"; intent.putExtra(Intent.EXTRA_TEXT, msg); startActivity(Intent.createChooser(intent, "分享"));
8.跳转到应用市场 自己应用详情 评分页面
Uri uri = Uri.parse("market://details?id=" + getPackageName()); intent = new Intent(Intent.ACTION_VIEW, uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(intent); } catch (ActivityNotFoundException exception) { MobclickAgent.reportError(this, exception); SingleToast.show(this, R.string.msg_need_app_market); }