gpt4 book ai didi

Android 服务未在后台正常运行

转载 作者:行者123 更新时间:2023-11-29 01:51:14 25 4
gpt4 key购买 nike

我一打开我的应用程序,就开始这一行:

Intent intent = new Intent(this, MyIntentService.class);
startService(intent);

这是我的 IntentService 类:

public class MyIntentService extends Service {
SharedPreferences prefs;
public static String prefName = "SecretFile";
public static String version = "56";
public final static int uniqueID = 1394885;



public void onCreate() {
super.onCreate();
prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
version = prefs.getString("Version", "1");
System.out.println(version);
calllsharedprefs();
new loadSomeStuff().execute();
}

public class loadSomeStuff extends AsyncTask<String, Integer, String> {

protected void onPreExecute() {

}

@Override
protected String doInBackground(String... params) {
//starting script
JSONObject json = jArray.getJSONObject(i);
json.getString("Version");
Editor editoi = prefs.edit();
editoi.putString("Version", json.getString("Version"));
//finishing script
}

protected void onProgressUpdate(Integer... progress) {

}

@SuppressWarnings("deprecation")
protected void onPostExecute(String result) {
if (prefs.getString("Version", "1").equals(version)) {
} else {
System.out.println(prefs.getString("Version", "1"));
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(uniqueID);
Intent intent = new Intent(MyIntentService.this, Drawer.class);
PendingIntent pi = PendingIntent.getActivity(
MyIntentService.this, 0, intent, 0);
String body = "New Updates are available!";
String title = "Price list changed";
Notification n = new Notification(
R.drawable.about, body,
System.currentTimeMillis());
n.setLatestEventInfo(MyIntentService.this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
version = prefs.getString("Version", "1");
}
}
}

基本上,我从 phpMyAdmin 中的表中获取版本字段,如果版本代码相同,则不发送通知,如果不发送通知并将其设置为与 phpmyadmin 版本相同,但是 然而,当我打开我的应用程序时,代码启动并向我发送通知,但是当我重新编辑表中的值以将其设置为 9 时,它不起作用,它不会再次发送通知!该怎么办?谢谢 =)

最佳答案

您正在 oncreate 中调用 >>loadSomeStuff().execute();。它只会执行一次,并执行您要求它执行的任何操作。

要么在每次编辑值时启动此 IntentService,要么在 Activity 中自行执行。看来您不需要服务,您可以自行显示来自 Activity 的通知。

关于Android 服务未在后台正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com