【问题标题】:How to transfer data which is consumed from webservice using "Service" in android?如何使用android中的“服务”传输从webservice消耗的数据?
【发布时间】:2014-05-03 12:19:57
【问题描述】:

现在我正在使用 Service 使用一项 Web 服务。现在我想发送该数据(我已从 Main Activity)到下一个Activity,有没有人知道呢

请给个建议

感谢您的宝贵时间!..

请查找我的来源以供参考

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

    }
}}

【问题讨论】:

    标签: android service data-transfer


    【解决方案1】:

    如果要检索的数据必须由 Activity 或任何 UI 直接使用,则必须使用 AsyncTaskLoader 来使用它,而不是 Service。然后,您可以使用 Intent Bundle 将数据传递给另一个活动,例如。如果数据是全局会话标识符,您可以将其存储在所有活动都可访问的全局变量中,但您还必须将其存储在磁盘上,以便您可以在应用程序被操作系统杀死后正确恢复它需要更多内存。

    如果数据必须在后台加载,要独立于 UI 进行一些后台处理或预缓存,您必须使用IntentService,它会自动为您创建一个后台线程,您可以将您的直接下载onHandleIntent()中的代码(无需在Service中创建任何AsyncTask)。

    这完全取决于您的用例。

    【讨论】:

    • 如果有的话,能否给个样品,因为我是“服务”概念的新手
    • Android SDK 中有很多示例,并且有完整的文档可以解释服务在 Android 中的工作方式:developer.android.com/guide/components/services.html。您必须首先确定您的应用程序中是否需要服务。大多数应用程序没有。
    • 为什么我更喜欢服务,因为现在我有一个新闻显示项目,在该项目中,我想在显示启动画面时在后台使用项目所需的所有数据起初,稍后我将展示我迄今为止计划的下一个活动的数据。但我不知道做新闻应用程序的实际程序。是否有任何创建新闻应用程序的程序?
    • 您可以在启动画面期间使用 AsyncTask 或 Loader 加载所有数据,完成后退出启动画面,则不需要服务。但是闪屏通常是个坏主意。总结一下,如果你想在应用不显示时加载数据,请使用服务,否则不要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多