0

我有一个应用程序,它在后台的服务中做事(也当屏幕被关闭时)。我在以下代码的 Activity 中启动服务。

Intent i=new Intent(this, AppService.class);
i.putExtra(AppService.VOL_ALM, amanager.getStreamVolume(amanager.getStreamVolume(AudioManager.STREAM_ALARM)));

PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
alarmManager.cancel(pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 *60, pi);

Service 中的代码如下所示:

  public int onStartCommand(Intent intent, int flags, int startId) {

    test = intent.getIntExtra(VOL_ALM, 0);


    timer = new Timer("TweetCollectorTimer");
    timer.schedule(updateTask, 0, 15 * 1000L);

    return(START_NOT_STICKY);
  }

在服务中,我必须更改变量“test”并使用它。但是我总是会丢失变量的更改值,因为有时会执行 onStartCommand。我尝试使用首选项存储变量,但我在启动服务时还需要变量的原始值。最好的方法是从方法中的意图更改数据“VOL_ALM”。但这可能吗?

提前致谢

4

1 回答 1

1

?

将这两个值存储在 SharedPreferences 中?这是持久值的公认方法。

于 2012-10-23T17:12:09.787 回答