2

嗨朋友我有一个问题...我想将字符串数据表单活动发送到服务,并且我已将数据保存到共享首选项中..我的一切正常,我能够将数据传递给服务类..但问题是那,如果我将字符串更改为共享首选项并从 Eclipse 运行我的项目,那么我不会遇到任何问题,但是我从设备或模拟器打开应用程序..然后更改我的字符串..那个时间值没有在服务中更新类,在服务类中,它采用旧数据。我如果调试它显示的值,但在显示旧值的设备中。请提供任何帮助

This is the code i tried...
Activity class,From where i sent the value to to the service class using alarm manager

@Override
    protected void onResume() {
        super.onResume();
// Preference Satting
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
}

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                    myIntent = new Intent(PZAlarmTTSActivity.this, TtsService.class);

                    passedText = prefs.getString("text", "<unset>");
                    //Log.i("passed Text  :", passedText);

                    // Passing the value to the service
                    Bundle bundle = new Bundle();
                    bundle.putString("k_key", passedText);
                    myIntent.putExtras(bundle);
                    Log.i("key Data  :  ", passedText);

                    // Pending intent to launch when the alarm triggers
                    pendintIntent = PendingIntent.getService(PZAlarmTTSActivity.this, 0,
                            myIntent, 0);
                    // Sets alarm to trigger
                    alarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pendintIntent);

}


Service class, where i want to receive the string value


@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "service started", Toast.LENGTH_LONG).show();

        Bundle bundle = intent.getExtras();  
        String data = bundle.getString("k_key"); // Here data is not updating in 2nd  case
        Log.i("From service class : ", data);
    }
4

1 回答 1

0

Doc说:

onStart:此方法已弃用。

改为实现 onStartCommand(Intent, int, int) 。

onStartCommand :每次客户端通过调用 startService(Intent) 显式启动服务时由系统调用,提供它提供的参数和表示启动请求的唯一整数令牌。不要直接调用此方法。

您需要使用线程来获取服务中的最新值,或者通过startService每次使用从 SharedPreferences 获取新值来启动服务,因为一旦服务启动,您的onStart方法不会被调用如果服务已经在运行

这也许对你有帮助!!!

于 2012-05-23T05:31:16.293 回答