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         <activity
12             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     @Override
14     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                     @Override
22                     public void onClick(View v) {
23                         // TODO Auto-generated method stub
24                         createAlarm("Week Up Now!", 6, 8);
25                     }
26                 });
27
28         findViewById(R.id.setTimer_btn).setOnClickListener(
29                 new OnClickListener() {
30
31                     @Override
32                     public void onClick(View v) {
33                         // TODO Auto-generated method stub
34                         createTimer("From Intent!", 3, true);
35                     }
36                 });
37
38         findViewById(R.id.showAllAlarm_btn).setOnClickListener(
39                 new OnClickListener() {
40
41                     @Override
42                     public void onClick(View v) {
43                         // TODO Auto-generated method stub
44                         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     <application
14         android:allowBackup="true"
15         android:icon="@drawable/ic_launcher"
16         android:label="@string/app_name"
17         android:theme="@style/AppTheme" >
18         <activity
19             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

时间: 2024-08-06 16:03:28

implicit intent 隐式意图的相关文章

Intent隐式意图

Intent隐式意图 就是没有指定的组件 可以有多个 1,首先要在mainactivity里建立Button b类, 然后用 setContentView (R.layout.activity_main);来查找要按钮, 让 b = (Button)setContentView (R.layout.activity_main); 为b添加鼠标事件 b.setOnClickListener(this); 此时要实现implements OnClickListener接口 实现onClick方法 要

隐式意图Intent

在我们想往下一个页面传递数据时,要想到显式意图和隐式意图,显示意图用于内部活动跳转时比较方便,而隐式意图用于应用程序中外部活动的跳转时较为方便,在使用隐式意图时我们要想到清单文件 代码如下: <intent-filter> <action android:name="xx,xx,xx,xx"/> <category android:name="android.intent.category.DEFAULT"/> </inte

Intent 显示意图 隐式意图

//显式意图  :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) // 一般激活自己应用的组件的时候 采用显示意图  //隐式意图: 只需要指定要动作和数据就可以 ( 好处应用程序之间没有耦合) //激活别人写的应用  隐式意图, 不需要关心对方的包名和类名    public void enter(View view){  String name = et_name.getText().toString().trim();  if(TextUtils.isEmpty(name

隐式意图调用系统自带组件的各种Uri总结

调用系统应用解析(必需要加各自使用的权限) android intent 隐式意图和显示意图(activity跳转) 显示意图要求必须知道被激活组件的包和class 隐式意图仅仅须要知道跳转activity的动作和数据,就能够激活相应的组件 A 主activity  B 接收跳转的activity 步骤 1:在主配置文件里声明B 至少要声明一个android:name属性 [html] <activity android:name=".DemoActivity" android:

显式意图和隐式意图总结

package com.example.wang.myapplication; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class Mai

【黑马Android】(07)多线程下载的原理/开源项目xutils/显示意图/隐式意图/人品计算器/开启activity获取返回值

多线程下载的原理 司马光砸缸,多开几个小水管,抢救小朋友. import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import

隐式意图 Intent

/显式意图 :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) // 一般激活自己应用的组件的时候 采用显示意图 //隐式意图: 只需要指定要动作和数据就可以 ( 好处应用程序之间没有耦合) //激活别人写的应用 隐式意图, 不需要关心对方的包名和类名 1.Manifest <activity android:name="com.itheima.intent2.SecondActivity" > <intent-filter> <actio

基础学习总结(八)--Intent中显示意图和隐式意图的用法

Intent(意图)主要是解决Android应用的各项组件之间的通讯.Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用.因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦.例如,在一个联系人维护的应用中,当我们在一个联系人列表屏幕(假设对应的Activity为listActivity)上,点击某个联系人

intent 显式意图和隐式意图之Activity的激活方式

对于intent主要的分类主要包括隐式意图和显式意图.显式意图通常主要是启动本应用中的Activity之间的数据,而隐式意图则常见于启动系统中的某些特定的动作,比如打电话,或者是跨应用的Activity启动.所以激活本应用的Activity用显式意图,如果MainActivity与你要激活的Activity不在同一个包下,在同一个工程下,在采用显式意图激活时,一定要写明当前Activity所在的包名,否则可能会激活不了.最后要写上startActivity(intent);,开启Activity