Android API Guides学习2——Common Intents

http://developer.android.com/guide/components/intents-common.html

       Intent允许你通过一个你想要执行的行为动作来启动一个其他app的Activity。这种启动方式叫做隐式意图以为你并没有指定一个组件去启动,而是指定要执行的具体动作并且提供一些数据。
       当你使用startActivity() 或 startActivityForResult()并传入一个隐式意图,系统会决定这个intent去往那个app,并开启对应的activity。如果有大于一个app可以处理,就会弹出一个对话框来个你选择需要哪个app来处理。如果没有app可以处理程序会崩溃,可以resolveActivity()来判断是否有app可以处理,返回的不是空则有app可以处理。
       接下来介绍一些常见的隐式intent。

  • Alarm Clock
    • 创建一个 alarm
      • Action
        ACTION_SET_ALARM
      • Data URI
        none
      • MIME Type
        none
      • Extras
        EXTRA_HOUR:闹钟的小时。
        EXTRA_MINUTES:闹钟的分钟。
        EXTRA_MESSAGE:闹钟的信息。
        EXTRA_DAYS:一个ArrayList包含该重复的星期,必须声明为一个来自Calendar类中的整数如MONDAY,如果为单词闹钟,不要这个Extra。
        EXTRA_RINGTONE:指定闹钟的铃声,为一个 content:的URI。不声明使用默认铃声。
        EXTRA_VIBRATE:是否震动,布尔值。
        EXTRA_SKIP_UI:是否跳过闹钟设置UI界面。布尔值。
    public void createAlarm(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(getPackageManager()) != null) {
        startActivity(intent);
    }
    }

    intent filter:

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SET_ALARM" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
    • 创建一个计时器
      • Action
        ACTION_SET_TIMER
      • Data URI
        none
      • MIME Type
        none
      • Extras
        EXTRA_LENGTH:倒计时的秒数。
        EXTRA_MESSAGE:一个自定义消息。
        EXTRA_SKIP_UI:是否跳过闹钟设置UI界面。如果是true值简单的启动计时器。
    public void startTimer(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(getPackageManager()) != null) {
        startActivity(intent);
    }
    }

    intent filter:

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SET_TIMER" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
    • 显示所有闹钟
      • Action
        ACTION_SHOW_ALARMS
      • Data URI
        none
      • MIME Type
        none

    intent filter:

    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SHOW_ALARMS" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
  • Calendar
    • 添加一个日历事件
      • Action
        ACTION_INSERT
      • Data URI
        Events.CONTENT_URI
      • MIME Type
        “vnd.android.cursor.dir/event”
      • Extras
        EXTRA_EVENT_ALL_DAY:是否是一个全天事件,布尔值。
        EXTRA_EVENT_BEGIN_TIME:事件开始时间,毫秒自从1970-01-01 00:00:00。
        EXTRA_EVENT_END_TIME:事件结束时间(milliseconds since epoch)。
        TITLE:事件标题。
        DESCRIPTION:事件描述。
        EVENT_LOCATION:事件坐标。
        EXTRA_EMAIL:以逗号间隔表示被邀请者的电子邮件。
    public void addEvent(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(getPackageManager()) != null) {
        startActivity(intent);
    }
    }

    intent filter:

    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.INSERT" />
        <data android:mimeType="vnd.android.cursor.dir/event" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
  • Camera
    • 得到一个图片或视频并返回
      • Action
        ACTION_IMAGE_CAPTURE
        ACTION_VIDEO_CAPTURE
      • Data URI
        None
      • MIME Type
        None
      • Extras
        EXTRA_OUTPUT:app要储存图片或视频的URI地址。传入Uri对象。
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final Uri mLocationForPhotos;
    public void capturePhoto(String targetFilename) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.withAppendedPath(mLocationForPhotos, targetFilename);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bitmap thumbnail = data.getParcelable("data");
        // 对被保存在mLocationForPhotos的原始图片做其他处理
        ...
    }
    }

    intent filter:

    <activity ...>
    <intent-filter>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
    • 使用静态图片模式、摄像模式开启相机app
      • Action
        INTENT_ACTION_STILL_IMAGE_CAMERA
        INTENT_ACTION_VIDEO_CAMERA
      • Data URI
        none
      • MIME Type
        none
      • Extras
      • none
    public void capturePhoto() {
    Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent);
    }
    }
    

    intent filter:

    <activity ...>
    <intent-filter>
        <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
  • 联系人 App
    • 选择一个联系人
    static final int REQUEST_SELECT_CONTACT = 1;
    public void selectContact() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_CONTACT);
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
        Uri contactUri = data.getData();
        // Do something with the selected contact at contactUri
        ...
    }
    }

    设置MIME Type

    • CommonDataKinds.Phone.CONTENT_TYPE
      显示号码的视图
    • CommonDataKinds.Email.CONTENT_TYPE
      显示电子邮件的视图
    • CommonDataKinds.StructuredPostal.CONTENT_TYPE
      显示地址的视图
    static final int REQUEST_SELECT_PHONE_NUMBER = 1;
    public void selectContact() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // 得到URI并在内容提供者中查询
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // 如果返回的游标有用,则得到电话号码
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
    }
    • 显示一个联系人
    public void viewContact(Uri contactUri) {
    Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
  • 编辑一个联系人

    public void editContact(Uri contactUri, String email) {
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setData(contactUri);
    intent.putExtra(Intents.Insert.EMAIL, email);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
  • 插入一个联系人

    public void insertContact(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(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
  • File Storage
    • 返回特定类型的文件
      • Action
        ACTION_GET_CONTENT
      • Data URI
        None
      • MIME Type
        MIME type为对应文件类型
      • Extras
        EXTRA_ALLOW_MULTIPLE:是否可以多选。
        EXTRA_LOCAL_ONLY:返回的只能为本地文件。
    static final int REQUEST_IMAGE_GET = 1;
    public void selectImage() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_GET);
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
        Bitmap thumbnail = data.getParcelable("data");
        Uri fullPhotoUri = data.getData();
        // Do work with photo saved at fullPhotoUri
        ...
    }
    }

    返回一张图片的intent filter

    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <data android:type="image/*" />
        <category android:name="android.intent.category.DEFAULT" />
        <!-- CATEGORY_OPENABLE为返回可读的文件可以被openFileDescriptor()表现为文件流 -->
        <category android:name="android.intent.category.OPENABLE" />
    </intent-filter>
    </activity>
  • Local Actions

  • Maps
  • Music or Video
    • 打开一个媒体文件
      • Action
        ACTION_VIEW
      • Data URI
        file:
        content:
        http:
      • MIME Type
        “audio/*”
        “application/ogg”
        “application/x-ogg”
        “application/itunes”
        或者其他需要的类型
      • Extras
    public void playMedia(Uri file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(file);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }

    intent filter:

    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:type="audio/*" />
        <data android:type="application/ogg" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>
  • Phone
    • 拨打电话
      • Action
        ACTION_DIAL:打开拨号界面。
        ACTION_CALL:拨打一个电话
      • Data URI
        tel:< phone-number>
        voicemail:< phone-number>
        http:
      • MIME Type
        None
    public void dialPhoneNumber(String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:" + phoneNumber));
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
    • 用特定的app查询
      • Action
        “com.google.android.gms.actions.SEARCH_ACTION”
        支持从google查找
      • Extras
        QUERY:查找需要的搜索查询字符串

    intent filter:

    <activity android:name=".SearchActivity">
    <intent-filter>
        <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    </activity>
    进行一个网络查询:
    
    public void searchWeb(String query) {
    Intent intent = new Intent(Intent.ACTION_SEARCH);
    intent.putExtra(SearchManager.QUERY, query);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
  • Settings
    • 打开特定设置界面
      • Action
        ACTION_SETTINGS
        ACTION_WIRELESS_SETTINGS
        ACTION_AIRPLANE_MODE_SETTINGS
        ACTION_WIFI_SETTINGS
        ACTION_APN_SETTINGS
        ACTION_BLUETOOTH_SETTINGS
        ACTION_DATE_SETTINGS
        ACTION_LOCALE_SETTINGS
        ACTION_INPUT_METHOD_SETTINGS
        ACTION_DISPLAY_SETTINGS
        ACTION_SECURITY_SETTINGS
        ACTION_LOCATION_SOURCE_SETTINGS
        ACTION_INTERNAL_STORAGE_SETTINGS
        ACTION_MEMORY_CARD_SETTINGS
    public void openWifiSettings() {
    Intent intent = new Intent(Intent.ACTION_WIFI_SETTINGS);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }
  • Text Messaging

  • Web Browser
    • 载入一个web界面
      • Action
        ACTION_VIEW
      • Data URI
        http:
        https:
      • MIME Type
        PLAIN_TEXT_TYPE (“text/plain”)
        “text/html”
        “application/xhtml+xml”
        “application/vnd.wap.xhtml+xml”
    public void openWebPage(String url) {
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    }

    intent filter

    <activity ...>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <!-- Include the host attribute if you want your app to respond
             only to URLs with your app's domain. -->
        <data android:scheme="http" android:host="www.example.com" />
        <category android:name="android.intent.category.DEFAULT" />
        <!-- The BROWSABLE category is required to get links from web pages. -->
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
    </activity>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值