极力推荐文章:欢迎收藏

Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本文主要是总结Intent 常用的方法,并封装成Utils类中

主要涉及以下内容

  1. 通过组件名启动
  2. 通过包名、类名启动
  3. 通过类启动
  4. 打电话
  5. 发短信
  6. 打开网页
  7. 播放音乐
  8. 打开图片
  9. 创建闹钟
  10. 创建定时器
  11. 添加日历事件
  12. 拍照
  13. 打开Camera
  14. 打开视频录像
  15. 选择联系人
  16. 查看联系人
  17. 编辑联系人
  18. 插入联系人
  19. 写邮件
  20. 打开地图指定点
  21. 检索特定类型图片

Intent 简介请看上篇文章

Intent 使用方法详解

1. 通过组件名启动 Activity

  • 使用方法
	/**
* 通过组件名启动Activity
* **/
public static void StartIntentFromComponent(Context context,
Class intentClass) {
Intent intent = new Intent();
// 1.使用ComponentName 启动Activity
ComponentName componentname = new ComponentName(context, intentClass);
intent.setComponent(componentname);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

2. 通过包名、类名启动 Activity

  • 使用方法
	/**
* 通过包名类名启动Activity
* **/
public static void StartIntentFromPackage(Context context,
String packageName, String className) {
Intent intent = new Intent();
// 1.使用ComponentName 启动Activity
ComponentName componentname = new ComponentName(packageName, className);
intent.setComponent(componentname);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

3. 通过类启动 Activity

  • 使用方法
	/**
* 通过Class启动Activity
* **/
public static void StartIntentFromClass(Context context, Class<?> classOpen) {
Intent intent = new Intent();
// 2.使用Setclass方法,类方法间接使用ComponentName
intent.setClass(context, classOpen);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

4. 打电话

  • 使用Intent 打电话 方法如下
	/**
* 打电话
* **/
public static void MakeCall(Context context, int number) { // 需要打电话权限
// <uses-permission android:name="android.permission.CALL_PHONE"/> Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
+ number));
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }
注意:

打电话需要申请权限

<uses-permission android:name="android.permission.CALL_PHONE"/>

5. 发短信

  • 使用方法

    1.基础发送短信
	/**
* 1.基础发送短信
* **/
public static void SendMms(Context context, String mmsString) { Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, mmsString);
sendIntent.setType("text/plain");
// sendIntent.setData(Uri.parse("smsto:"));
// This ensures only SMS apps respond
// 修改 Intnent 选择器Tittle
String title = context.getResources().getString(R.string.hello_world);
Intent chooser = Intent.createChooser(sendIntent, title); // 验证是否有Activity 接收
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(chooser);
}
}

2.自定义 发送短信

	/**
* 2.自定义 发送短信
* **/
public static void SendMmsCustom(Context context, String mmsString) { Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, mmsString);
sendIntent.setType("text/plain");
// sendIntent.setData(Uri.parse("smsto:"));
// This ensures only SMS apps respond
// 修改 Intnent 选择器Tittle String title = context.getResources().getString(R.string.hello_world); Intent chooser = Intent.createChooser(sendIntent, title); // 验证是否有Activity 接收
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(chooser);
}
}

6. 打开网页

  • 使用方法
	/**
* 打开网页
* **/
public static void OpenInternetUri(Context context, String uri) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }

7. 播放音乐

  • 使用方法
	/**
* 播放音乐
* **/
public static void PlayMusic(Context context, String path) { // String
// path=Environment.getExternalStorageDirectory().getAbsolutePath()+"test.mp3";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///" + path), "audio/*");
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }
  • 播放特定艺术家专辑
	/**
* 搜索特定艺术家专辑
* **/
public static void playSearchArtist(Context context, String artist) { Intent intent = new Intent(
MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS,
MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
intent.putExtra(SearchManager.QUERY, artist);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }

8. 打开图片

  • 使用方法
	/**
* 打开图片
* **/
public static void OpenImage(Context context, File file) {
// File file =new File("/mnt/sdcard/1.png");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*"); if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }

9. 创建闹钟

  • 使用方法
	/**
* 创建闹钟
* **/ public static void SetAlarmIntent(Context context, String message,
int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_HOUR, hour)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes); if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
  • 设置闹钟action 机权限
  <!-- 设置闹钟的权限 -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<activity android:name=".Intent.IntentMethod" >
<intent-filter>
<action android:name="android.intent.action.SET_ALARM" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
  • 显示所有闹钟

10. 创建定时器

  • 使用方法
	/**
* 创建定时器
* **/
public static void StartTimer(Context context, String message, int seconds) {
Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_LENGTH, seconds)
.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

添加设置SET_TIMER的Action

        <activity android:name=".Intent.IntentMethod" >
<intent-filter>
<action android:name="android.intent.action.SET_ALARM" />
<action android:name="android.intent.action.SET_TIMER" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

11. 添加日历事件

  • 使用方法
	/**
* 添加日历事件
* **/ public static void AddCalendarEvent(Context context, String title,
String location, Calendar begin, Calendar end) {
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI).putExtra(Events.TITLE, title)
.putExtra(Events.EVENT_LOCATION, location)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
  • 日历事件过滤

12. 拍照

  • 使用方法
	/**
* 拍照
* **/ public static void CapturePhoto(Context context, String targetFilename,
Uri mLocationForPhotos) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.withAppendedPath(mLocationForPhotos, targetFilename));
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} }
  • 拍照过滤

13. 打开Camera

  • 使用方法

/**
* 打开Camera
* **/ public static void OpenCamera(Context context) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
  • 打开Camera 过滤

14. 打开视频录像

  • 使用方法

/**
* 打开录像视频
* **/ public static void OpenCameraVideo(Context context) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
  • 打开录像功能过滤

15. 选择联系人

  • 使用方法
	/***
* 选择联系人
* **/
public static void SelectContact(Context context) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

16. 查看联系人

  • 使用方法
	/***
* 查看联系人
* **/
public static void ViewContact(Context context, Uri contactUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

17. 编辑联系人

  • 使用方法
	/***
* 编辑联系人
* **/
public static void EditContact(Context context, Uri contactUri, String email) {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(contactUri);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

18. 插入联系人

  • 使用方法
	/***
* 插入联系人
* **/
public static void InsertContact(Context context, String name, String email) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(Contacts.CONTENT_TYPE);
intent.putExtra(Intents.Insert.NAME, name);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

19. 写邮件

  • 使用方法

/***
* 写邮件
* **/
public static void composeEmail(Context context, String[] addresses,
String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
// intent.setData(Uri.parse("mailto:"));
// only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
  • 邮件过滤

20. 打开地图指定点

  • 使用方法
	/***
* 打开地图指定点
* **/
public static void callCar(Context context, Uri geoLocation) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(geoLocation);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

21 检索特定类型图片


/***
* 检索特定类型图片 获取照片
* **/
public static void selectImage(Context context) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

Intent 常用方法总结的更多相关文章

  1. Intent 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Intent 是一个消息传递对象,主要用于组建之间的通讯,例如:启动Activit ...

  2. Intent属性详解一 component属性

    先看效果图 概述 在介绍Component之前,我们首先来了解ComponentName这个类:ComponentName与Intent同位于android.content包下,我们从Android官 ...

  3. Android开发之Intent略解

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  4. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  5. Android应用开发中Intent的作用及使用方法

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  6. (Android数据传递)Intent消息传递机制 “Intent”“数据传递”

    Intent类的继承关系:   需要注意的是,该类实现了Parcelable(用于数据传递)和Cloneable接口. Intent是一种(系统级别的)消息传递机制,可以在应用程序内使用,也可以在应用 ...

  7. Android零基础入门第78节:四大组件的纽带——Intent

    前面学习Activity时己经多次使用了 Intent,当一个Activity需要启动另一个Activity时, 程序并没有直接告诉系统要启动哪个Activity,而是通过Intent来表达自己的意图 ...

  8. JUnit4---Hamcrest匹配器常用方法总结

    一.Hamcrest是什么? Hamcrest is a library of matchers, which can be combined in to create flexible expres ...

  9. 前端开发:Javascript中的数组,常用方法解析

    前端开发:Javascript中的数组,常用方法解析 前言 Array是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array ...

随机推荐

  1. Docker启动一个Centos镜像,在docker中安装ifconfig和ssh

    执行docker search centos 现在最流行的Linux嘛.查了下,排名第一的(STARS最多1882)官方版,就是你了 果断拿下, docker pull centos,看网速了静等拿下 ...

  2. 《收获,不止SQL优化》读书笔记

    整体性能分析 AWR.ASH.ADDM.AWRDD 整体分析调优工具 AWR:关注数据库的整体性能的报告: ASH:数据库中的等待事件与哪些SQL具体对应的报告: ADDM:oracle给出的一些建议 ...

  3. CSS3边框与圆角

    1. CSS3 圆角 border-radius 属性 一个最多可指定四个border -*- radius属性的复合属性,这个属性允许你为元素添加圆角边框!语法:border-radius: 1-4 ...

  4. MySQL之基础操作

    一.安装 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数 ...

  5. Codeforces 730I:Olympiad in Programming and Sports(最小费用流)

    http://codeforces.com/problemset/problem/730/I 题意:有n个人参加两种比赛,其中每个人有两个参加比赛的属性,如果参加了其中的一个比赛,那么不能参加另一个比 ...

  6. Dijkstra算法与堆(C++)

    Dijkstra算法用于解决单源最短路径问题,通过逐个收录顶点来确保得到以收录顶点的路径长度为最短.      图片来自陈越姥姥的数据结构课程:https://mooc.study.163.com/l ...

  7. Android使用WebView加载H5页面播放视频音频,退出后还在播放问题解决

    Android中经常会使用到WebView来加载H5的页面,如果H5页面中有音频或者视频的播放时,还没播放完就退出界面,这个时候会发现音频或者视频还在后台播放,这就有点一脸懵逼了,下面是解决方案: 方 ...

  8. C++学习书籍推荐《Effective C++ 第三版(英文)》下载

    百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing train ...

  9. 剑指offer第二版-3.数组中重复的数

    面试题3:数组中重复的数 题目要求: 在一个长度为n的数组中,所有数字的取值范围都在[0,n-1],但不知道有几个数字重复或重复几次,找出其中任意一个重复的数字. 解法比较: /** * Copyri ...

  10. android_activity_研究(一)

    android中活动的概念(activity)是一个很重要的东东.这里有很多东东值得好好研究.最好的研究来源当然是官网啦,所以本人这里写一点对官网文章的研究心得. 一.活动(activity)的概念 ...