如何知道Android应用程序是否从Play商店安装而不使用packageInstaller

时间:2017-04-27 05:36:08

标签: android

是否可以在不使用packageInstaller的情况下知道Android应用是否从Play商店安装?每个其他问题都与packageInstaller有关,任何人都可以编辑,但我的问题是没有使用packageInstaller

2 个答案:

答案 0 :(得分:1)

您应该使用 InstallReferrerReceiver 像文章中那样创建并注册BroadCastReceiver。 这是我的简单例子:

public class InstallReferrerReceiver extends BroadcastReceiver {

    public static final String FROM_SPECIFIC_FEED = "FROM_SPECIFIC_FEED";
    public static final String MAIN_FEED_ID = "MAIN_FEED_ID";

    private static final String REFERRER = "referrer";

    private static final String SOURCE = "utm_source=";

    @Override
    public void onReceive(Context context, Intent intent) {
        String referrer = intent.getStringExtra(REFERRER);
        //"utm_source=24news.com.ua&utm_medium=js&utm_term=fee";
        //"utm_source=12&utm_medium=js&utm_term=fee";
        String mainFeed = extractMainFeed(referrer, false);

        if (mainFeed != null && mainFeed.length() > 0) {
            try {
                setMainFeed(context, Integer.parseInt(mainFeed));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    private String extractMainFeed(String referrer, boolean toEnd) {
        if (!toEnd) {
            try {
                return referrer.substring(referrer.indexOf(SOURCE) + SOURCE.length(), referrer.indexOf("&"));
            } catch (Exception e) {
                return extractMainFeed(referrer, true);
            }
        } else {
            try {
                return referrer.substring(referrer.indexOf(SOURCE) + SOURCE.length());
            } catch (Exception e) {}
        }
        return null;
    }

    private void setMainFeed(Context context, int mainFeedId) {
        PreferenceManager.getInstance(context).setMainFeedId(mainFeedId);
        Intent fromSpecificFeed = new Intent(FROM_SPECIFIC_FEED);
        fromSpecificFeed.putExtra(MAIN_FEED_ID, mainFeedId);
        LocalBroadcastManager.getInstance(context).sendBroadcast(fromSpecificFeed);
    }
}

答案 1 :(得分:0)

在Packagemanager chek for getInstallerPackageName中,此方法将为您提供信息。