Can someone please explain to me the basic functio

2019-03-21 15:44发布

I'm new to programming applications for the Android OS. As far as general architecture of the OS goes, I understand that processes are implemented as Linux processes and that each one is sandboxed.

However, I'm utterly confused on the IPCs and syscalls (if any) used. I know that the IBinder is a form of this; parcels are sent back and forth between processes and Bundles are array forms of parcels (?). But even that is still unfamiliar to me. Same with Intents. All in all, I don't understand what kinds of IPCs are implemented and how.

Could someone briefly explain to me the specific methods used by user level applications in Android OS to communicate with each other and the OS? I've done kernel programming and played with various IPCs in Linux (Ubuntu and Debian) so it would help immensely if this was all explained in relation to what I'm familiar with...

Thanks in advance!

2条回答
家丑人穷心不美
2楼-- · 2019-03-21 16:43

Android is a Service Oriented Architecture which means that all the applications on a device are made up of components that request work be performed by other components using a high level messages called Intents. While behind the scenes Intents that span applications are sent using Binder which relies on a special Android flavor of shared memory, the goal is for applications developers to be blissfully unaware of the implementation. The only requirement is, when a component wants to pass an object along with its "intention" to request work by another component that lives in different process, that object must be parcelable (think serializable). Additionally, in order for applications to use Intents of other applications, those Intents must be published in the manifest file using an Intent Filter.

An application that wanted to trigger the display of a webpage would have code that looks like this:

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);
                    }
                });
    }
}

Another application that was capable of servicing that Intent by displaying the webpage would define the following intent-filters in its manifest so it would catch the Intent sent by the other application:

        <!-- 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>

Beyond using Intents, you can create objects with proxy objects using AIDL that allow you to perform remote procedure calls across the process boundary.

You probably need not be concerned with how libc is performing syscalls since you are running inside a VM and are several levels removed from them. As far as "normal" IPC goes, you have sockets but you don't have System V Shared Memory as it was deemed problematic and removed.

查看更多
疯言疯语
3楼-- · 2019-03-21 16:44

In its most basic form, an intent is bundle of data that is sent to an instance of a specified object so that it can take it and perform some action with it. For instance, you could have a URL that you got from an RSS feed, and you want to open it in a browser that may or may not be inside your application. You would create an intent, including what target object (Activity) you want to recieve your intent and some data that you want to send to that object. Objects can have recievers, which accept intents, and are registered such that if you create an intent to say, open the url with the browser, Android will recognize that your "intent" is to use the web browser object. Since you pass along with it a URL, then the browser application can pick that up and do something with it. Intents are very flexible, and can be very specific messages, so you can send an intent to a specific object with a ComponentName specified, or you can leave that out and just add a category and Android will prompt the user with an option of all objects that say they are an object of that category ("Web Browsers" for example, and the message will be sent to whatever object (again, really just an Activity) that the user selected.

So, briefly, you have:

  • MyActivity1 reads an RSS feed.
  • MyActivity1 creates an intent, specifying a ComponentName of com.superawesome.MyWebBrowserActivity and Data of "http://www.stackoverflow.com
  • MyWebBrowserActivity has a reciever to pick up the intent and displays the passed URL

For a great guide on intents, just take a minute to read thid this, it's well worth it:

Android Dev Guide: Intents and Intent Filters

查看更多
登录 后发表回答