Intent使用归纳

Android中,Intent是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯。以下列出Intent常用的用法:

1、调用WEB浏览器

Uri uri = Uri.parse("http://www.baidu.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW ,uri); 
startActivity(intent); 

2、在地图上显示某个坐标

Uri uri = Uri.parse("geo:21.803233,-53.829831"); 
Intent intent = new Intent(Intent.Action_VIEW, uri); 
startActivity(intent); 

3、显示路径

Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent);

4、调用拨打电话界面

Uri uri = Uri.parse("tel:10086"); 
Intent intent = new Intent(Intent.ACTION_DIAL, uri); 
startActivity(intent); 

5、直接拨打电话

需要添加权限 

<uses-permission id="android.permission.CALL_PHONE" />

Uri uri = Uri.parse("tel.10086"); 
Intent intent =new Intent(Intent.ACTION_CALL, uri);

6发送短信或彩信

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.putExtra("sms_body", "The SMS text"); 
intent.setType("vnd.android-dir/mms-sms"); 
startActivity(intent);

7、发送短信

Uri uri = Uri.parse("smsto:10000"); 
Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 
intent.putExtra("sms_body", "haha"); 
startActivity(intent); 

8、发送彩信

Uri uri = Uri.parse("content://media/external/images/media/23"); 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra("sms_body", "some text"); 
intent.putExtra(Intent.EXTRA_STREAM, uri); 
intent.setType("image/png"); 
startActivity(intent); 

或者

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity"); 
intent.putExtra("subject", "彩信主题"); 
intent.putExtra("sms_body", "彩信内容"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/images/11.jpg")); 
intent.setType("image/jpeg"); 
startActivity(intent); 

9、调用发送邮件功能

Uri uri = Uri.parse("mailto:xxx@foxmail.com"); 
Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 
startActivity(intent);

10、发送邮件

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_EMAIL, xxx@foxmail.com); 
intent.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
intent.setType("text/plain"); 
startActivity(Intent.createChooser(intent, "Choose Email Client"));

或者

Intent intent = new Intent(Intent.ACTION_SEND); 
String[] tos = {"me@abc.com"}; 
String[] ccs = {"you@abc.com"}; 
intent.putExtra(Intent.EXTRA_EMAIL, tos); 
intent.putExtra(Intent.EXTRA_CC, ccs); 
intent.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
intent.setType("message/rfc822"); 
startActivity(Intent.createChooser(intent, "Choose Email Client")); 

11、播放媒体文件

Intent intent = new Intent(Intent.ACTION_VIEW); 
Uri uri = Uri.parse(file:///sdcard/musics/someonelikeyou.mp3); 
intent.setDataAndType(uri, "audio/mp3"); 
startActivity(intent); 
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent);

12、卸载APK

Uri uri = Uri.fromParts("package", strPackageName, null); 
Intent intent = new Intent(Intent.ACTION_DELETE, uri); 
startActivity(intent); 

或者

Uri uninstallUri = Uri.fromParts("package", "xxx", null); 
Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri); 

13、安装APK

Uri installUri = Uri.fromParts("package", "xxx", null); 
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

 14、播放音乐

Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3"); 
Intent intent = new Intent(Intent.ACTION_VIEW, playUri); 

15、发送附件

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/test.mp3"); 
sendIntent.setType("audio/mp3"); 
startActivity(Intent.createChooser(intent, "Choose Email Client")); 

16market上某个应用信,pkg_name就是应用的packageName

Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

17market上某个应用信息,app_id可以通过www网站看下

Uri uri = Uri.parse("market://details?id=app_id"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

18、调用搜索功能

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_WEB_SEARCH); 
intent.putExtra(SearchManager.QUERY, "android"); 
startActivity(intent);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值