views:

122

answers:

2

Hi I am trying to make an app that opens android market page of selected app & lets user download it.I have used below intent to open market.

  Intent intent = new Intent (Intent.ACTION_VIEW);
    intent.setData (Uri.parse ("market://details?id=" + PackageName ));
    intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult (intent, 13);

but I always get resultcode 0 in onActivityResult.StackTrace says:

I/ActivityManager(   79): Starting activity: Intent { act=android.intent.action.VIEW dat=market://details?id=com.google.android.apps.unveil flg=0x10000000 cmp=com.an
droid.vending/.AssetInfoActivity }
W/ActivityManager(   79): Activity is launching as a new task, so cancelling activity result.

What I want is that market returns me some response that user downloaded the app or just cancelled.

EDIT:@CommonsWare I am trying to access added package here But cant figure what should be key to get packagename from extras of ACTION_PACKAGE_ADDED

public class ServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //Do this when the system sends the intent
    Bundle b = intent.getExtras();
    String packagename = b.get(?);   //cant figure what should be key to get packagename //from extras
    }

}

+5  A: 

The Android Market is not set up to support startActivityForResult(). Also, bear in mind that the download and installation happen asynchronously (i.e., user clicks Install, and the download occurs in the background, and they finish the install via the Notification).

CommonsWare
Does that mean there is no other way to get currently installed app than to query whole installed list each time on PACKAGE_ADDED?
100rabh
@Saurabh: There are extras on the `ACTION_PACKAGE_ADDED` broadcast that will help you identify what was added.
CommonsWare
@CommonsWare I followed this post to get extras http://stackoverflow.com/questions/1990855/android-how-to-get-location-information-from-intent-bundle-extras-when-using-loc but fail to get key for Packagename.See edited post above.
100rabh
@Saurabh: Use `PackageManager` and `getPackagesForUid()` to get the packages associated with the UID you get via `EXTRA_UID` in the `Intent`.
CommonsWare
A: 

@CommonsWare Superb!

Your solution proved helpful & it also landed me to another very useful page http://devdaily.com/java/jwarehouse/android/core/java/com/android/internal/content/PackageMonitor.java.shtml

Below is a code snippet from above link source to get package name of currently installed app from broadcast intent:

 String getPackageName(Intent intent) {
        Uri uri = intent.getData();
        String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
        return pkg;
    }

Thanks Commonsware,Thanks StackOverFlow

100rabh