如何在android中比较来自市场网址的包名?

时间:2016-04-25 04:50:16

标签: android

我正在开发一个包含广播接收器的应用程序,当用户点击任何特定的应用程序时,他将指示玩商店,并从jsons响应下载该应用程序,我得到所有的应用程序显示在列表网址中的应用程序列表#34 ; https://play.google.com/store/apps/details?id=com.example.launcher"我要做的是,当应用程序在系统中成功下载后,我将从json响应url检查该应用程序的应用程序包名称,如果匹配则显示toast else错误消息。 我怎么能实现这一点请帮助我一个星期我正在寻找。

这是我的广播接收器代码: -

    public class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED"))
        Log.e("Package Added:-", intent.getData().toString());
    }
}

2 个答案:

答案 0 :(得分:0)

使用

从URL中提取包名称
    String downPack = yourUrl.split("id\\=")[1];

从文件中提取包名称

public String getPackageNameByAPK(String apkPath, Context context) {
    String packName = "";
    PackageManager packageManager;
    PackageInfo packInfo;

    try {

        if (apkPath == null) return "";

        if (context == null) {
            return "";
        }

        packageManager = context.getPackageManager();
        packInfo = packageManager.getPackageArchiveInfo(apkPath, 0);
        packName = packInfo.packageName;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return packName;
}

比较

boolean matched = downPack.equals(getPackageNameByAPK(apkPath,context));

这可能对你有帮助..

答案 1 :(得分:0)

@Override
public void onReceive(Context context, Intent intent) {

    String installAppPackgeName = intent.getData().getSchemeSpecificPart();     
    String url = "https://play.google.com/store/apps/details?id=com.example.launcher";  
    String packageName = getQueryMap(url).get("id");
    if(installAppPackgeName.equals(packageName)){
        // If install app package name is same as url package name then do some task.
        // Your app is installed now.   

    }

}

/** 
 * Get Query String as Key-Value Pair.
 */
public static Map<String, String> getQueryMap(String query) {
    Map<String, String> map = new HashMap<>();
    String[] queryString = query.split("\\?");
    if(queryString.length>1){
        String[] params = queryString[1].split("&");
        for (String param : params) {
            String name = param.split("=")[0];
            String value = param.split("=")[1];
            map.put(name, value);
        }            
    }
    return map;
}