Question

Right now i am consuming one web-service using Service.Now i would like to send that data(which i've consumed from Main Activity) to the next activity, has any one have any idea about it

suggestion please

Thanks for your preciuos time!..

Please find my sources for reference

MainActivity.java

public class MainActivity extends Activity {

AlarmManager alarm;
PendingIntent pintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startService(new Intent(this, MyService.class));
    Calendar cal = Calendar.getInstance();
    Intent intent = new Intent(this, MyService.class);
    pintent = PendingIntent.getService(this, 0, intent, 0);

    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Start service every 20 seconds
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),10* 1000, pintent);

}


@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    alarm.cancel(pintent);
    stopService(new Intent(MainActivity.this,MyService.class));

     }
}

MyService.java

public class MyService extends Service {


String result_data = "";

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}


@Override
public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

    Authentication_Class task = new Authentication_Class();
    task.execute();

}

@Override
public void onDestroy() 
{
    Toast.makeText(this, "Servics Stopped", Toast.LENGTH_SHORT).show();
    super.onDestroy();
}

public class Authentication_Class extends AsyncTask<Void, Void, Void>
{
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
    }


    @Override
    protected Void doInBackground(Void... params) 
    {
        // TODO Auto-generated method stub
            try {
                Call_service();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return null;
    }

    private void Call_service() throws Exception{
        // TODO Auto-generated method stub

    System.setProperty("http.keepAlive", "false");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    String URL          =   "";
    String METHOD_NAME  =   "";
    String NAMESPACE    =   "";
    String SOAPACTION   =   "";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    // Add parameters if available 

    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransportSE = new HttpTransportSE(URL,15000);
    try {

        httpTransportSE.call(SOAPACTION, envelope);
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
        result_data = String.valueOf(result);
        System.out.println(">>-- RESPONSE    :    " +result_data);


    } 

    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub

    }
}}
Was it helpful?

Solution

If the data that you want to retrieve has to be consumed directly by an Activity or any UI, you have to use an AsyncTask or a Loader to consume it and no Service. Then you can pass the data to another activity using an Intent Bundle, for example. If the data is a global session identifier, you can store it in a global variable accessible by all your activities but you also have to store it on disk, so that you can properly restore it with your application after it has been killed if the OS needed more memory.

If the data has to be loaded in the background, to do some background processing or pre-caching independently from the UI, you have to use an IntentService which will create a background thread for you automatically and you can put your download code in onHandleIntent() directly (there is no need to create any AsyncTask in a Service).

It all depends on your use case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top