我有一个Android广播接收器(使用Eclipse,如果它很重要),是由一个服务启动的,假设检查一个在线数据版本是否更新。如果是这样,我想在通知栏中触发通知。BroadcastReceiver SharedPreferences只是不可靠
一切似乎工作正常,但是,它从SharedPreferences获得的结果是不可靠的。广播接收器假设获取存储在SharedPreferences中的当前值并将其与来自Web网址的值进行比较。如果它比较了值并发现有更新(即url中的值大于存储值),它会触发通知并更新存储在SharedPreferences中的值。广播接收机每天检查一次这个更新,这仅仅是为了通知用户,仅此而已。
我用来做这个的代码似乎工作。但在这里和那里,它从SharedPreferences中提取默认值,并触发更新通知,即使没有任何更改。
我意识到我可能会在广播接收器的onReceive中做太多的工作,但是我非常困惑如何从广播接收器开始工作。
有没有人有任何实现这个最好的方法的代码示例?我已经搜索和搜索,但我似乎无法找到明确的步骤来做到这一点。以下是我有:
public class AlarmReceiver extends BroadcastReceiver {
public static JSONArray myJArray2;
public static InputStream is = null;
public static final String URL_DatabaseVersion = "http://www.mysite.com/mobile/version.php";
public static NotificationManager nm;
public static Boolean updated = false;
public static int latestVersion = 0;
public static int internalVersion = 2;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onReceive(Context context, Intent intent) {
updated = false;
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
internalVersion = settings.getInt("dataversion", 1);
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("",""));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_DatabaseVersion);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
}
try{
JSONArray myJArray = new JSONArray(result);
myJArray2 = myJArray;
}catch(JSONException e){
}
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = context.getSharedPreferences("MyPrefsFile",mode);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putInt ("dataversion", latestVersion);
editor.commit();
if (!Arrays.asList(myJArray2).contains(null))
{
try{
for(int i=0;i<myJArray2.length();i++)
{
JSONObject json_data = myJArray2.getJSONObject(i);
latestVersion = json_data.getInt("title");
if (internalVersion < latestVersion)
{
updated = true;
}
else
{
updated = false;
}
}
}
catch(Exception e)
{}
finally
{}
}
else
{
updated = false;
}
if (updated)
{
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Database Updated";
CharSequence message = "I: " + internalVersion + " L: " + latestVersion + "Click to open.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon, "Database Updated", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
}
}
是的,我可能是试图做太多的广播接收器,并且可能会引起问题,但是我无法弄清楚如何做这任何其他方式。当我记录这段代码的结果时,第一次通过它的效果很好。第二次通过internalVersion时出现0.它从URL获得的值总是正确的,它是internalVersion,它是从SharedPreferences中抽取的,这是多次错误的。
有什么想法?对不起,这么久。谢谢。
科瑞
不知道它是否可以解决您的问题,但我会在“服务”中完成所有这些工作。我没有明显的理由可以在'BroadcastReceiver'中看到这一点。 – kabuko
这可能是真的,但我不知道如何设置它来做到这一点。我是否需要从广播接收器的onReceive中调用服务?不知道如何获得24小时重复检查,即使用户目前没有与应用程序交互。 – Tom
是的,你可以从'BroadcastReceiver'开始'Service'。 – kabuko