Android服务 - 启动服务&重定向到另一个活动

时间:2012-01-03 08:49:39

标签: android service

我想在sql server& s之间进行复制。 android sqlite.That工作正常。

一旦我将发票/用户点击返回(我的应用程序返回按钮)按钮,就需要进行上一个/下一个活动&必须开始服务。我不想等下一次活动。

现在的问题是:我使用Service完成了这项工作。这也很好。但是当用户单击后退按钮时,它会等到服务完成。之后只进入上一个/下一个活动。

这是我的后退按钮代码:

 Button reOpCancel = (Button)findViewById(R.id.reOpCancel);
    reOpCancel.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {

                Intent i = new Intent(getBaseContext(), ListRetailerActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("RetailerName", retailerName);
                bundle.putString("RetailerCode", relailerCode);
                bundle.putString("RouteCode", routeCode);
                i.putExtras(bundle);
                View vi = SalesActivityGroup.group.getLocalActivityManager().startActivity("ListRetailerActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
                SalesActivityGroup.group.replaceView(vi);

               ConnectivityManager manager = (ConnectivityManager) getSystemService(LoginActivity.CONNECTIVITY_SERVICE);
                boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
                boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
                if(is3g ||isWifi){
                    startService(new Intent(RetailerOptionActivity.this, UploadService.class));
                }else {
                    Toast.makeText(SalesActivityGroup.group.getParent(), "GPRS/WIFI is not available", Toast.LENGTH_LONG).show();
                }
           }
    });

这是我的服务代码:

 public class UploadService extends Service {
private static final String TAG = "UploadService";
private  String APPURL = "";
private  String NAMESPACE = "http://tempuri.org/";
private  String METHOD_NAME = "convertJSONToDataSet";
private  String SOAP_ACTION = "http://tempuri.org/IXontService/convertJSONToDataSet";
ArrayList<String> uploadFiler = new ArrayList<String>();    
HashMap<Integer,String> uploadTable = new HashMap<Integer, String>();
private String strBusinessUnit= "";
private String strExecutive = "";
private ConnectivityManager manager;
private Boolean is3g = false;
private Boolean isWifi = false;
private int networkType;


@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Upload Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");
    SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_PRIVATE);
    strBusinessUnit = myPrefs.getString("BusinessUnit", "");
    strExecutive = myPrefs.getString("Executive", "");

    manager = (ConnectivityManager) getSystemService(DownlaodTableActivity.CONNECTIVITY_SERVICE);
    is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
if (is3g) {
        APPURL =  getServicePath("G");;
    }else if (isWifi) {
        APPURL =  getServicePath("L");;
    }else if(networkType == TelephonyManager.NETWORK_TYPE_GPRS) {
        APPURL =  getServicePath("G");;
    }

}
@Override
public void onDestroy() {
    Toast.makeText(this, "Upload Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startid) {

    Toast.makeText(this, "Upload Service Started", Toast.LENGTH_LONG).show();

    if(! APPURL.equals("")){
        boolean result = uploadUsingService();
        if(!result) {
            Toast.makeText(this,"Service is not working  OR Time out to Connect to service !!!!!!!",Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(this, "GPRS/WIFI is not available", Toast.LENGTH_LONG).show();
    }

    Log.d(TAG, "onStart");
    onDestroy();

}

    uploadTable = getUploadTable();

    boolean result = true;

    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
    dbAdapter.openDataBase();
    System.out.println("==APPURL="+APPURL);


        try {
            if(uploadTable.size() > 0){
                for (Map.Entry<Integer, String> entry : uploadTable.entrySet()) {
                    String value = entry.getValue();
                    if(value.equals("WMInvoiceHeader")){
                        result = getInvoiceHeader();
                    }else if(value.equals("WMInvoiceLine")){
                        result = getInvoiceLine();
                    }else if(value.equals("WMInvoiceHeaderDiscount")){
                        result = getInvoiceHeaderDiscount();
                    }else if(value.equals("WMInvoiceSpecialDiscount")){
                        result = getInvoiceSpecialDiscount();
                    }else if(value.equals("WMInvoiceCancelHeader")){
                        result = getInvoiceCancelHeader();
                    }else if(value.equals("WMInvoiceCancelLine")){
                        result = getInvoiceCancelLine();
                    }else if(value.equals("WMTransactionControl")){
                        result = getTransactionControl();
                    }else if(value.equals("WMVisitDetail")){
                        result = getVisitDetail();
                    }else if(value.equals("WMVisitHeader")){
                        result = getVisitHeader();
                    }else if(value.equals("WMReturnHeader")){
                        result = getReturnHeader();
                    }else if(value.equals("WMReturnLine")){
                        result = getReturnLine();
                    }else if(value.equals("WMPaymentReceipt")){
                        result = getPaymentReceipt();
                    }else if(value.equals("WMPaymentAllocation")){
                        result = getPaymentAllocation();
                    }
                    System.out.println("==result=="+result);
                    if(!result) break;
                }


        }
        dbAdapter.close();
        } catch (Exception e) {
            System.out.println("==ERROR=="+e.getMessage());
            e.printStackTrace();
        }


    return result;
}

Problem is don't need to wait until finish service, it need to go next activity

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:4)

我没有收到您的问题,但在阅读完您的代码后,我知道您的活动在服务工作完成之前没有开始吗?

在Android服务上运行UI线程。 如果你想在服务中做更好的工作,你可以在服务中使用Thread。它不会阻止你的UI线程,并且线程(UI和背景)都将同时工作。