Intent官方教程(2)Intent的两种类型

Intent Types


There are two types of intents:

  • Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.
    精确型,根据完整类名启动目标组件。通常在同一个应用内使用。
  • Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
    含蓄型:只定义一个模糊查找的名字,系统来查找它。通常在不同应用内使用。系统查找过滤过程如下图:


   When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the Intent object.

  
  Figure 1. Illustration of how an implicit intent is delivered through the system to start another activity: [1] Activity A creates an Intent with an action description and passes it to startActivity(). [2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

  When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.

  An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. Likewise, if you do not declare any intent filters for an activity, then it can be started only with an explicit intent.

  Caution: To ensure your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you cannot be certain what service will respond to the intent, and the user cannot see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent. 

 注意:通常启动服务时要用精确型

示例1:

在应用A启动应用B中的服务:

1     Intent i = new Intent("com.e.weixin.test.RemoteService");
2     bindService(i, mConnection, Context.BIND_AUTO_CREATE);

在应用B中定义服务:

1     <service
2             android:name=".test.RemoteService"
3             android:enabled="true"
4             android:exported="true" >
5             <intent-filter >
6                 <action android:name="com.e.weixin.test.RemoteService" />
7                 
8             </intent-filter>
9         </service>

示例2:implicit intent

 1 // Create the text message with a string
 2 Intent sendIntent = new Intent();
 3 sendIntent.setAction(Intent.ACTION_SEND);
 4 sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
 5 sendIntent.setType("text/plain");
 6 
 7 // Verify that the intent will resolve to an activity
 8 if (sendIntent.resolveActivity(getPackageManager()) != null) {//注意resolveActivity可验证是否有可用组件能收到这个intent
 9     startActivity(sendIntent);
10 }

示例3:explicit intent

1 // Executed in an Activity, so 'this' is the Context
2 // The fileUrl is a string URL, such as "http://www.example.com/image.png"
3 Intent downloadIntent = new Intent(this, DownloadService.class);
4 downloadIntent.setData(Uri.parse(fileUrl));
5 startService(downloadIntent);

 

posted @ 2016-09-24 22:42  f9q  阅读(414)  评论(0编辑  收藏  举报