首页 > 代码库 > implicit intent 隐式意图

implicit intent 隐式意图

Google 文档中:

    An intent allows you to start an activity in another app by describing a simple action you‘d like to perform (such as "view a map" or "take a picture") in an Intent object. This type of intent is called an implicitintent because it does not specify the app component to start, but instead specifies an action and provides some data with which to perform the action.

    When you call startActivity() or startActivityForResult() and pass it an implicit intent, the system resolves the intent to an app that can handle the intent and starts its corresponding Activity. If there‘s more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use.

我的理解为:

       Intent 可以实现在一个APP中开始一个Activity;对于只是在Intent 对象中只做了 简单的动作描述,这种被称为 Implicit Intent (隐式意图)。其中并没有指定由哪个应用去执行,只是指定了要做的事情和需要携带的数据。

       当我们调用startActivity()或 startActivityForResult()传入该Intent的时候,系统会完成Action和对应应用的配对,如果是多个应用的话,系统会向用户显示一个选择列表,由用户来选择需要运行的应用。如下图:

 技术分享

简单的例子:

首先需要在manifest文件中做一下配置

1、添加相应的许可;

2、添加相应的 intent-filter,在Activity下

如:

技术分享
 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest > 3   ........................... 4     <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 5          6     <application 7         android:allowBackup="true" 8         android:icon="@drawable/ic_launcher" 9         android:label="@string/app_name"10         android:theme="@style/AppTheme" >11         <activity12             android:name=".MainActivity"13             android:label="@string/app_name" >14             <intent-filter>15                 <action android:name="android.intent.action.MAIN" />16                 <category android:name="android.intent.category.LAUNCHER" />17             </intent-filter>18             19             <intent-filter>20                 <action android:name="android.intent.action.SET_ALARM" />21                 <category android:name="android.intent.category.DEFAULT" />22             </intent-filter>23             24             <intent-filter>25                 <action android:name="android.intent.action.SET_TIMER" />26                 <category android:name="android.intent.category.DEFAULT"/>27             </intent-filter>28             29             <intent-filter>30                 <action android:name="android.intent.action.SHOW_ALARMS" />31                 <category android:name="android.intent.category.DEFAULT"/>32             </intent-filter>33             34         </activity>35     </application>36 37 </manifest>
manifest

 

 1 private final void createAlarm(String message, int hour, int minutes) { 2         // 创建一个闹铃 3         Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM) 4                 .putExtra(AlarmClock.EXTRA_MESSAGE, message) 5                 .putExtra(AlarmClock.EXTRA_HOUR, hour) 6                 .putExtra(AlarmClock.EXTRA_MINUTES, minutes); 7  8         if (intent.resolveActivity(getPackageManager()) != null) { 9             startActivity(intent);10         } else {11             Toast.makeText(this, "设置闹铃失败!", Toast.LENGTH_SHORT).show();12         }13     }

 

 

全部代码如下:

技术分享
 1 package com.cash.aboutmenuapp; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.provider.AlarmClock; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Toast;10 11 public class MainActivity extends Activity {12 13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity_main);17 18         findViewById(R.id.setAlarm_btn).setOnClickListener(19                 new OnClickListener() {20 21                     @Override22                     public void onClick(View v) {23                         // TODO Auto-generated method stub24                         createAlarm("Week Up Now!", 6, 8);25                     }26                 });27 28         findViewById(R.id.setTimer_btn).setOnClickListener(29                 new OnClickListener() {30 31                     @Override32                     public void onClick(View v) {33                         // TODO Auto-generated method stub34                         createTimer("From Intent!", 3, true);35                     }36                 });37 38         findViewById(R.id.showAllAlarm_btn).setOnClickListener(39                 new OnClickListener() {40 41                     @Override42                     public void onClick(View v) {43                         // TODO Auto-generated method stub44                         Intent intent = new Intent(45                                 AlarmClock.ACTION_SHOW_ALARMS);46                         if (intent.resolveActivity(getPackageManager()) != null) {47                             startActivity(intent);48                         }49                     }50                 });51         52         53         54 55     }56 57     private final void createAlarm(String message, int hour, int minutes) {58         // 创建一个闹铃59         Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)60                 .putExtra(AlarmClock.EXTRA_MESSAGE, message)61                 .putExtra(AlarmClock.EXTRA_HOUR, hour)62                 .putExtra(AlarmClock.EXTRA_MINUTES, minutes);63 64         if (intent.resolveActivity(getPackageManager()) != null) {65             startActivity(intent);66         } else {67             Toast.makeText(this, "设置闹铃失败!", Toast.LENGTH_SHORT).show();68         }69     }70 71     private final void createTimer(String message, int seconds, boolean skipUi) {72         Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER);73         intent.putExtra(AlarmClock.EXTRA_MESSAGE, message)74                 .putExtra(AlarmClock.EXTRA_LENGTH, seconds)75                 .putExtra(AlarmClock.EXTRA_SKIP_UI, skipUi);76 77         if (intent.resolveActivity(getPackageManager()) != null) {78             startActivity(intent);79         } else {80             Toast.makeText(this, "设置定时器失败!", Toast.LENGTH_SHORT).show();81         }82     }83 84 }
MainActivity
技术分享
 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.cash.aboutmenuapp" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="19" 9         android:targetSdkVersion="21" />10 11     <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />12         13     <application14         android:allowBackup="true"15         android:icon="@drawable/ic_launcher"16         android:label="@string/app_name"17         android:theme="@style/AppTheme" >18         <activity19             android:name=".MainActivity"20             android:label="@string/app_name" >21             <intent-filter>22                 <action android:name="android.intent.action.MAIN" />23                 <category android:name="android.intent.category.LAUNCHER" />24             </intent-filter>25             26             <intent-filter>27                 <action android:name="android.intent.action.SET_ALARM" />28                 <category android:name="android.intent.category.DEFAULT" />29             </intent-filter>30             31             <intent-filter>32                 <action android:name="android.intent.action.SET_TIMER" />33                 <category android:name="android.intent.category.DEFAULT"/>34             </intent-filter>35             36             <intent-filter>37                 <action android:name="android.intent.action.SHOW_ALARMS" />38                 <category android:name="android.intent.category.DEFAULT"/>39             </intent-filter>40             41         </activity>42     </application>43 44 </manifest>
Manifest
技术分享
 1 <?xml version="1.0" encoding="utf-8" ?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:paddingBottom="@dimen/activity_vertical_margin" 7     android:paddingLeft="@dimen/activity_horizontal_margin" 8     android:paddingRight="@dimen/activity_horizontal_margin" 9     android:paddingTop="@dimen/activity_vertical_margin"10     android:orientation="vertical"11     tools:context="com.cash.aboutmenuapp.MainActivity" >12 13     <Button 14         android:id="@+id/setAlarm_btn"15         android:layout_width="match_parent"16         android:layout_height="wrap_content"17         android:text="Set Alarm By Intent" />18     19     <Button 20         android:id="@+id/setTimer_btn"21         android:layout_width="match_parent"22         android:layout_height="wrap_content"23         android:text="Set Timer By Intent" />24     25     <Button 26         android:id="@+id/showAllAlarm_btn"27         android:layout_width="match_parent"28         android:layout_height="wrap_content"29         android:text="Show All Alarm By Intent" />30     31     32 </LinearLayout>
layout

 

implicit intent 隐式意图