简体   繁体   English

Android将String形式的Service从Service传递到Service?并在Service中的onCreate上使用?

[英]Android Pass String form Service to Service?And use at onCreate in Service?

I Have two services.so here I am passing string form one service to another service 我有两个服务。所以我在这里将字符串从一个服务传递给另一服务

Here I used This to pass string value.. 在这里,我使用了This来传递字符串值。

startService(new Intent(Service_A.this, Service_A.class).putExtra("svdata", url));

and this to receive the string 这接收字符串

@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;
}

And at onCreate I am starting my webserive with that Url 在onCreate上,我从该网址开始我的webserive

but the url is missing, 但是网址丢失了

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

}

I am using this MyWebService() to get json data with that URL. 我正在使用此MyWebService()获取具有该URL的json数据。

But URL is wokring and String in getting. 但是URL越来越麻烦,而String却越来越容易。 The exact url value, But before that onCreate() method is running 确切的url值,但在此onCreate()方法运行之前

But I have given String to onStartCommand() 但是我给String赋予了onStartCommand()

can any one suggest me how to set the string as Url to my service. 有人可以建议我如何将字符串设置为我的服务的网址。 or How to add string directly to onCreate. 或如何将字符串直接添加到onCreate中。 In Service 服役中

Update 更新资料

My problem is.. I am starting Service at onCreate I have a Json service it will get json from url. 我的问题是..我在onCreate启动Service我有一个Json服务,它将从url获取json。 But I am getting String(url) form another Service. 但是我从另一个服务获取String(url)

To pass String form Service to Service I used startService(new Intent(Service_A.this, Service_A.class).putExtra("svdata", url)); 为了将字符串形式的Service传递给Service,我使用了startService(new Intent(Service_A.this, Service_A.class).putExtra("svdata", url));

But to Receive that string I need to write onStartCommand .. 但是要接收该字符串,我需要编写onStartCommand ..

Here I am getting string, but I want to use the same string in onCreate ... 在这里,我正在获取字符串,但是我想在onCreate使用相同的字符串...

is there any way to get that string at onCreate...IN SERVICE 有什么办法可以在onCreate ... IN服务中获取该字符串

Write a SyncManager (singleton class) for handling your service work. 编写一个SyncManager(单个类)来处理您的服务工作。

in your oncreate method : supply sync credentials like url,user info, etc.. 在您的oncreate方法中:提供同步凭证,例如url,用户信息等。

 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();
     }

    }

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

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