Android os: Intent

Intent.aidl的作用

经常看到这样的文件和类似的内容,这次终于搞明白了!

frameworks/base/core/java/android/content/Intent.aidl

package android.content;
parcelable Intent;

这里parcelable Intent什么意思?这里的Intent.aidl会生成对应的Intent.java文件吗?答案是否定的

out/target/common/obj/JAVA_LIBRARIES/framework-full_intermediates/core/java/android/content/路径下并没有对应的文件生成

那它的作用是什么?

https://www.jianshu.com/p/a8e43ad5d7d2

Android:学习AIDL,这一篇文章就够了(上)回答了这个问题:

所有的AIDL文件大致可以分为两类。

一类是用来定义parcelable对象,以供其他AIDL文件使用AIDL中非默认支持的数据类型的。

一类是用来定义方法接口,以供系统使用来完成跨进程通信的。可以看到,两类文件都是在“定义”些什么,而不涉及具体的实现,这就是为什么它叫做“Android接口定义语言”。

原来只知道第二类(定义方法接口),这里做个实验:删除Intent.aidl再编译framework则出现
frameworks/base/core/java/android/content/pm/ILauncherApps.aidl:20: couldn't find import for class android.content.Intent

这样证明了上面的结论:"定义parcelable对象,以供其他AIDL文件使用AIDL中非默认支持的数据类型的"

看文件frameworks/base/core/java/android/content/pm/ILauncherApps.aidl中包含

import android.content.Intent;

一句话如果aidl文件中用import xxx, 就要定义文件 xxx.aidl, xxx.aidl文件的内容是固定的:parcelable xxx.

Intent是什么/什么用途


/**要执行操作的抽象描述,用于启动Activity,发送广播,启动service
 * An intent is an abstract description of an operation to be performed.  
 * It can be used with {@link Context#startActivity(Intent) startActivity} to
 * launch an {@link android.app.Activity},
 * {@link android.content.Context#sendBroadcast(Intent) broadcastIntent} to
 * send it to any interested {@link BroadcastReceiver BroadcastReceiver} components,
 * and {@link android.content.Context#startService} or
 * {@link android.content.Context#bindService} to communicate with a
 * background {@link android.app.Service}.
 * Intent提供了一个便利方法用于运行时绑定不同的应用
 * An Intent provides a facility for performing late runtime binding between the code in
 * different applications. Its most significant use is in the launching of activities, where it
 * can be thought of as the glue between activities. It is basically a passive data structure
 * holding an abstract description of an action to be performed.
 //Intents有两种基本形式:explicit and implicit Intent
 * There are two primary forms of intents you will use.
 *     Explicit Intents have specified a component (via
 *     {@link #setComponent} or {@link #setClass}), which provides the exact
 *     class to be run.  Often these will not include any other information,
 *     simply being a way for an application to launch various internal
 *     activities it has as the user interacts with the application.
 *
 *     Implicit Intents have not specified a component;
 *     instead, they must include enough information for the system to
 *     determine which of the available components is best to run for that
 *     intent.
 *当使用不明确的Intent时,Intent resolution要得到具体的intent
 * When using implicit intents, given such an arbitrary intent we need to
 * know what to do with it. This is handled by the process of Intent
 * resolution, which maps an Intent to an {@link android.app.Activity},
 * {@link BroadcastReceiver}, or {@link android.app.Service} (or sometimes two or
 * more activities/receivers) that can handle it.

 *Intent resolution根据package中intent-filter去找匹配的Intent
 * The intent resolution mechanism basically revolves around matching an
 * Intent against all of the intent-filter, descriptions in the
 * installed application packages.  (Plus, in the case of broadcasts, any {@link BroadcastReceiver}
 * objects explicitly registered with {@link Context#registerReceiver}.) 

 *Intent resolution根据 action/type/category去向PackageManager查询一个组件是否能处理该intent
 * There are three pieces of information in the Intent that are used for
 * resolution: the action, type, and category.  Using this information, a query
 * is done on the {@link PackageManager} for a component that can handle the
 * intern filter信息在文件AndroidManifest.xml
 * intent. The appropriate component is determined based on the intent
 * information supplied in the <code>AndroidManifest.xml</code> file 

除了action/type/category外还有Extra data, 从上面的描述中看Extra data不参与match

例子

private static final String DUMP_BUGREPORT = "xxx.intent.action.DUMP_BUGREPORT";
public static void startReportService(Context context) {
        Intent intent = new Intent();
        intent.setAction(DUMP_BUGREPORT);
        context.sendBroadcast(intent);
 }

        <receiver
            android:name=".receiver.StartupReceiver"
            >
            <intent-filter>
                <action android:name="xxx.intent.action.DUMP_BUGREPORT" />
            </intent-filter>
        </receiver>
    public void onReceive(Context context, Intent intent) {}

 

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/81191249
今日推荐