gpt4 book ai didi

Android 服务 - 启动服务并重定向到另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 16:16:50 25 4
gpt4 key购买 nike

我想在 sql server 和 android sqlite 之间进行复制。这工作正常。

一旦我输入发票/用户单击“后退”(我的应用程序后退按钮)按钮,它就需要转到上一个/下一个 Activity 并且还必须启动服务。我不想等到下一个 Activity 。

现在的问题是:我已经使用 Service 完成了此操作。这也很好。但是当用户点击后退按钮时,它会等到服务完成。之后,它只会转到上一个/下一个 Activity 。

这是我的后退按钮代码:

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

问题是不需要等到完成服务,它需要去下一个 Activity

请帮我解决这个问题。

最佳答案

我没有得到你的问题,但在阅读你的代码后我知道你的 Activity 不会在服务工作完成之前开始,是吗?

在 Android 中,服务运行在 UI 线程上。如果你想更好地在服务中做一些工作,你可以在服务中使用线程。它不会阻塞你的 UI 线程,并且线程(UI 和后台)将同时工作。

关于Android 服务 - 启动服务并重定向到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8709786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com