繁体   English   中英

更新/重新启动Android服务偏好设置

[英]Update/Restart Android Service Preferences

我是Android的新手,所以这可能是一个非常基本的问题:

我有一项服务,当用户按下按钮时会启动

该服务将启动一个新任务,并每N秒循环一次。 在我的Android应用程序中的“设置”标签上设置秒数。

在服务的OnStart()方法中,获取首选项:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

并设置设置,例如:

final Long pollInterval = Long.parseLong(settings.getString("pollInterval", "1000"));

任务是在数据库上轮询新条目。 我的目标是在回到主框架后重新启动服务,以便onCreate方法获得新的首选项,或者刷新正在运行的任务中的首选项以更改轮询间隔。

我尝试使用以下方法重新启动服务:

startService(serviceIntent);
stopService(serviceIntent);

但是由于任何原因这都不起作用。

预先感谢您的回答。

Use this one to repeat service



try {

            //Create a new PendingIntent and add it to the AlarmManager
            Intent intent = new Intent(this, Service.class);
            PendingIntent pendingIntent = PendingIntent.getService(this,
                12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am =
                (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    2*60*60,pendingIntent);

          } catch (Exception e) {}

Also stop service when your task is completed

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM