Android 服务未在后台正常运行

标签 android service

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

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/

相关文章:

android - 如何检查用户是否拒绝了 android.permission.FOREGROUND_SERVICE

c# - Windows服务——封装现有功能

java - 如果触摸屏幕时有电话进来会崩溃

android - 支持旧版本的安卓设备

java - HashMap 给出一个无序列表的值?

android - 当没有可用的联系人图像时显示图像

linux - 如何在 nslcd.service 之后启动 docker.service?

.net - 在 Windows 服务和应用程序之间共享内存,什么最简单?

javascript - Angular js 服务/工厂属性行为

Android - 语音识别