【问题标题】:Can someone please explain to me the basic function of Intents in the Android OS?有人可以向我解释一下 Android OS 中 Intents 的基本功能吗?
【发布时间】:2010-05-28 01:15:07
【问题描述】:

我是为 Android 操作系统编写应用程序的新手。就操作系统的一般架构而言,我理解进程是作为 Linux 进程实现的,并且每个进程都是沙盒。

但是,我对使用的 IPC 和系统调用(如果有的话)完全感到困惑。我知道 IBinder 就是这种形式;包裹在进程之间来回发送,并且捆绑包是包裹的数组形式(?)。但即便如此,我仍然不熟悉。与意图相同。总而言之,我不明白实现了什么样的IPC以及如何实现。

有人可以简要向我解释一下 Android 操作系统中用户级应用程序用于相互通信和操作系统的具体方法吗?我已经完成了内核编程并在 Linux(Ubuntu 和 Debian)中使用了各种 IPC,所以如果这一切都与我所熟悉的内容相关地进行了解释,那将非常有帮助......

提前致谢!

【问题讨论】:

    标签: android ipc operating-system android-intent


    【解决方案1】:

    Android 是Service Oriented Architecture,这意味着设备上的所有应用程序都由请求工作由使用high level messages called Intents 的其他组件执行的组件组成。虽然在幕后跨应用程序的意图是使用依赖于special Android flavor of shared memory 的 Binder 发送的,但目标是让应用程序开发人员完全不知道实现。唯一的要求是,当一个组件想要传递一个对象及其“意图”以请求另一个位于不同进程中的组件工作时,该对象必须是parcelable(认为是可序列化的)。此外,为了让应用程序使用其他应用程序的 Intent,这些 Intent 必须使用Intent Filtermanifest file 中发布。

    想要触发网页显示的应用程序的代码如下所示:

    public class OpenInternet extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            ((Button) findViewById(R.id.OpenInternetButton))
                    .setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(Intent.ACTION_VIEW, Uri
                                    .parse("http://www.google.com"));
                            startActivity(i);
                        }
                    });
        }
    }
    

    另一个能够通过显示网页来为该 Intent 提供服务的应用程序将在其清单中定义以下意图过滤器,以便捕获另一个应用程序发送的 Intent:

            <!-- For these schemes were not particular MIME type has been
                 supplied, we are a good candidate. -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="about" />
                <data android:scheme="javascript" />
            </intent-filter>
            <!--  For these schemes where any of these particular MIME types
                  have been supplied, we are a good candidate. -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="inline" />
                <data android:mimeType="text/html"/>
                <data android:mimeType="text/plain"/>
                <data android:mimeType="application/xhtml+xml"/>
                <data android:mimeType="application/vnd.wap.xhtml+xml"/>
            </intent-filter>
    

    除了使用 Intents 之外,您还可以使用 AIDL 创建具有代理对象的对象,从而允许您跨进程边界执行 remote procedure calls

    您可能不需要关心 libc 是如何执行系统调用的,因为您在 VM 中运行并且从它们中删除了几个级别。就“正常”IPC 而言,您有套接字,但您 don't have System V Shared Memory 因为它被认为有问题并被删除。

    【讨论】:

      【解决方案2】:

      在其最基本的形式中,意图是发送到指定对象的实例的数据包,以便它可以获取它并使用它执行一些操作。例如,您可能有一个从 RSS 提要中获得的 URL,并且您想在浏览器中打开它,该浏览器可能位于您的应用程序中,也可能不在您的应用程序中。您将创建一个意图,包括您想要接收您的意图的目标对象(活动)以及您想要发送到该对象的一些数据。对象可以有接收者,它们接受意图,并且被注册,这样如果你创建一个意图说,用浏览器打开 url,Android 将识别你的“意图”是使用网络浏览器对象。由于您将 URL 与它一起传递,因此浏览器应用程序可以获取该 URL 并对其进行处理。 Intent 非常灵活,可以是非常具体的消息,因此您可以将 Intent 发送到指定了 ComponentName 的特定对象,或者您可以省略它并添加一个类别,Android 会提示用户选择所有对象表示它们是该类别的对象(例如“Web 浏览器”,并且消息将发送到用户选择的任何对象(同样,实际上只是一个 Activity)。

      所以,简单地说,你有:

      • MyActivity1 读取 RSS 提要。
      • MyActivity1 创建一个意图,指定 com.superawesome.MyWebBrowserActivity 的 ComponentName 和“http://www.stackoverflow.com 的数据”
      • MyWebBrowserActivity 有一个接收器来获取意图并显示传递的 URL

      如需有关意图的精彩指南,请花一分钟时间阅读,非常值得:

      Android Dev Guide: Intents and Intent Filters

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 2014-04-20
        • 2011-04-24
        • 1970-01-01
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 1970-01-01
        相关资源
        最近更新 更多