简体   繁体   中英

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..

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

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.

But URL is wokring and String in getting. The exact url value, But before that onCreate() method is running

But I have given String to onStartCommand()

can any one suggest me how to set the string as Url to my service. or How to add string directly to onCreate. In Service

Update

My problem is.. I am starting Service at onCreate I have a Json service it will get json from url. But I am getting String(url) form another Service.

To pass String form Service to Service I used startService(new Intent(Service_A.this, Service_A.class).putExtra("svdata", url));

But to Receive that string I need to write onStartCommand ..

Here I am getting string, but I want to use the same string in onCreate ...

is there any way to get that string at onCreate...IN SERVICE

Write a SyncManager (singleton class) for handling your service work.

in your oncreate method : supply sync credentials like url,user info, etc..

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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