我的应用程序中有一个广播接收器,从服务器响应URL获取服务器响应的URL我分割包名并保存在共享首选项中,现在的情况是,当在设备中添加任何应用程序时,它将检查该应用程序包名称从我保存的包名称,如果匹配,然后传递意图。问题是,当我之前声明的变量为空时,如何匹配添加了我的已保存packge的新包。显示空指针异常。
这里是代码: -
public class CAppDownloadReceiver extends BroadcastReceiver {
String url, packageName, s_szDealCode;
CDealBuySharedPreference m_oSharedPreference;
/**
* 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;
}
@Override
public void onReceive(Context context, Intent intent) {
m_oSharedPreference = new CDealBuySharedPreference(context);
HashMap<String, String> user = m_oSharedPreference.getDealUrlDetails();
try {
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String installAppPackgeName = intent.getData().getSchemeSpecificPart();
url = user.get(CDealBuySharedPreference.s_szDealUrl);// getting mobile from saved preferences..........
packageName = getQueryMap(url).get("id");
System.out.println("Previous package stored :" + packageName);
System.out.println("new Package added" + installAppPackgeName);
if (installAppPackgeName.equals(packageName)) {
// If install app package name is same as url package name then do some task.
// Your app is installed now.
s_szDealCode = user.get(CDealBuySharedPreference.s_szDealCode);
System.out.println("Match...");
System.out.println("Broadcast url..." + url);
System.out.println("Broadcast deal code:-" + s_szDealCode);
} else {
System.out.println("does not match ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}