Android Pass String表单服务到服务?在onCreate in Service中使用?

时间:2017-11-23 05:50:50

标签: java android string service

我有两个服务。所以我在这里将字符串从一个服务传递到另一个服务

这里我用This来传递字符串值..

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    MyURL = intent.getExtras().getString("svdata");

    System.out.print("@@@@@@@@@@@@@@@@@@@@@@@@@"+MyURL+"$$$$$$$$$$$$$$$$$");

    return START_STICKY;
}

这将收到字符串

@Override
public void onCreate() {
   try{
       new MyWebService(ServWeb.this).execute();
   }
   catch(Exception e){
       Toast.makeText(getApplicationContext(), "Failed to Connect Server", Toast.LENGTH_LONG).show();
   }

}

在onCreate上,我正在使用该Url启动我的webserive

但网址丢失,

MyWebService()

我正在使用此onCreate()来获取该网址的json数据。

但是URL正在变成了Wokring和String。确切的url值,但在onStartCommand()方法运行之前

但是我给了onCreate

字符串

任何人都可以建议我如何将字符串设置为我的服务的Url。 或者如何将字符串直接添加到onCreate。 在服务中

更新

我的问题是...... 我正在String(url)开始服务我有一个Json服务,它将从url获取json。但是我正在从startService(new Intent(Service_A.this, Service_A.class).putExtra("svdata", url));获得另一项服务。

将String表单服务传递给我使用的服务 onStartCommand

但要接收该字符串,我需要写onCreate ..

这里我收到字符串,但我想在Maven ...

中使用相同的字符串

有没有办法在onCreate上获取该字符串... IN SERVICE

1 个答案:

答案 0 :(得分:0)

编写一个SyncManager(单例类)来处理您的服务工作。

在你的oncreate方法中

:  提供同步凭据,如网址,用户信息等。

 public class SyncService extends IntentService {

     SyncManager syncManager;

     /**
      * Creates an IntentService.  Invoked by your subclass's constructor.
      *
      * @param name Used to name the worker thread, important only for debugging.
      */

     public SyncService(String name) {
      super(name);
     }

     public SyncService() {
      super("SyncService");
     }

     @Override
     protected void onHandleIntent(Intent intent) {
       syncManager = SyncManager.getInstance(getApplicationContext());
       syncManager.startSynchronisation();
      }
      /* The service is starting, due to a call to startService() */
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
      return super.onStartCommand(intent, flags, startId);
     }


     @Override
     public void onDestroy() {
      super.onDestroy();
     }

    }