简体   繁体   中英

how can i use .apk file into my own application android?

In developer.android.com I read central feature of android is you can make use of elements of other applications in your own application.Iam interested in this line. 1)How can we use the .apk downloaded suppose from android market in to our application ? 2)How can we use our own created applicaton into newly created application?

please give me guidance on this and if possible sample snippet?

Regards, Rajendar

I guess you mean that :

A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn't incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises.

Two last sentences are crucial. And the link provides more information about it. But briefly: application author can write his code in a manner that it can be reusable for others. He/she can do that by putting "intent filters" into his/her application's AndroidManifest.xml . Eg Google's Camera application (the one that provides camera functionality and image gallery as well - yeah, the application can "expose" many "entry points" = icons in the home screen) has activity definition (one of many) as follows:

<activity android:name="CropImage" android:process=":CropImage" android:configChanges="orientation|keyboardHidden" android:label="@string/crop_label">
    <intent-filter android:label="@string/crop_label">
        <action android:name="com.android.camera.action.CROP"/>
        <data android:mimeType="image/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.ALTERNATIVE"/>
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE"/>
    </intent-filter>
</activity>

That means that one can use it's cropping the image functionality by sending intent:

/* prepare intent - provide options that allow 
Android to find functionality provider for you;
will match intent filter of Camera - for matching rules see: 
http://developer.android.com/guide/topics/intents/intents-filters.html#ires */
Intent i = new Intent("com.android.camera.action.CROP");
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("image/*");
/* put "input paramters" for the intent - called intent dependent */
i.putExtra("data", /*... Bitmap object ...*/);
i.putExtra( /*... other options, e.g. desired dimensions ...*/ );
/* "call desired functionality" */
startActivityForResult(i, /* code of return */ CROPPING_RESULT_CODE);

CROPPING_RESULT_CODE that one can define in one's Activity is used to distinguish which external activity returned (helpfull if one calls several external functions) and is provided in calling activity's onActivityResult() method which is called when "remote" application finishes - below an example:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case CROPPING_RESULT_CODE:
        /* ... crop returned ... */
        if (data != null) {
            /* get cropped bitmap */
            Bitmap bmp = data.getParcelableExtra("data");       
            /* ... and do what you want ... */
        }
    case ANOTHER_RESULT_CODE:
        /* ... another external content ... */

    }
}

Other options are using: other Services or ContentProviders.

If you have any more questions don't hesitate.

Android uses Intents to allow applications request work be done by software residing in other applications. See my answer to this question for more details on Intents and an example of how your application can ask the Browser application to display a webpage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM