Service Automatisch aufgerufen, wenn Aktivität zerstört wird
Ich bin mit dem Problem von @ steckActivity + Service
darin habe ich folgende Anzahl von Aktivitäten und Dienstleistungen.
LoginActivity => OrderListActivity => AddOrderActivity => ConfirmOrderActivity
Dienstleistungen ReceivingOrderService - Neue Daten vom Server empfangen SendingOrderService - Neue Daten an Server sendenÜber beide Dienste Aufrufen von einem anderen separaten Dienst bei einer Dauer von einigenInterval.
CheckAutoSyncReceivingOrder - ReceivingOrderService aufrufen (Intervall 15Mins) CheckAutoSyncSendingOrder - SendingOrderService aufrufen (Intervall 3Mins)CheckAutoSyncReceivingOrder:public class CheckAutoSyncReceivingOrder extends Service {
Timer timer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
if(timer != null) {
timer.cancel();
Log.i(TAG, "RECEIVING OLD TIMER CANCELLED>>>");
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(InternetConnection.checkConnection(getApplicationContext())) {
if(getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
startService(new Intent(CheckAutoSyncReceivingOrder.this, ReceivingOrderService.class));
} else {
Log.d(TAG, "Connection not available");
}
}
}, 0, 60000); // 1000*60*15 = 9,00,000 = 15 minutes
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(timer != null)
timer.cancel();
Log.d(TAG, "Stopping Receiving...");
}
}
CheckAutoSyncSendingOrder:public class CheckAutoSyncSendingOrder extends Service {
Timer timer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
if(timer != null) {
timer.cancel();
Log.i(TAG, "OLD TIMER CANCELLED>>>");
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG, ">>>>>>>> SENDING AUTO SYNC SERVICE >>>>>>>>");
if(InternetConnection.checkConnection(getApplicationContext())) {
if(getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
startService(new Intent(CheckAutoSyncSendingOrder.this, SendingOrderService.class));
} else {
Log.d(TAG, "connection not available");
}
}
}, 0, 120000); // 1000*120*15 = 1,800,000 = 15 minutes
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(timer != null)
timer.cancel();
Log.d(TAG, "Stopping Sending...");
}
}
ConfirmOrderActivity # Letzte Aufgabe, die ich für Insert Data aufgerufen habe:new AsyncTask<Void, Void, Integer>() {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(
ConfirmOrderProductActivity.this);
progressDialog.setMessage("Inserting "
+ (isInquiry ? "Inquiry" : "Order") + "...");
progressDialog.setCancelable(false);
progressDialog
.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
@Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
int account_id = context.getSharedPreferences(PREF_DATA,
MODE_APPEND).getInt(DATA_ACCOUNT_ID, 0);
/**
* Check Whether isInquiry or not...
*/
product_type = isWeight ? 1 : 0;
if (isInquiry) {
/*
* INSERTING DATA IN INQUIRY TABLE
*/
return m_inquiry_id;
} else {
/*
* INSERTING DATA IN ORDER TABLE
*/
return m_order_id;
}
}
@Override
protected void onPostExecute(Integer m_order_id) {
// TODO Auto-generated method stub
super.onPostExecute(m_order_id);
progressDialog.dismiss();
if (dbHelper.db.isOpen())
dbHelper.close();
String title = "Retry";
String message = "There is some problem, Go Back and Try Again";
AlertDialog.Builder alert = new AlertDialog.Builder(
ConfirmOrderProductActivity.this);
if (m_order_id != -1) {
title = isInquiry ? "New Inquiry" : "New Order";
message = isInquiry ? "Your Inquiry Send Successfully." : "Your Order Saved Successfully.";
alert.setIcon(R.drawable.success).setCancelable(false);
} else {
alert.setIcon(R.drawable.fail).setCancelable(false);
}
alert.setTitle(title).setMessage(message)
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
startActivity(new Intent(
ConfirmOrderProductActivity.this,
FragmentChangeActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
/* Opening Left to Right Animation */
overridePendingTransition(R.anim.right_out,
R.anim.right_in);
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
}.execute();
Alles funktioniert einwandfrei gemäß dem Ablauf des Einfügens von Datensätzen in die Datenbank.
Nach dem Hinzufügen der Anfrage: Aktivität zerstören und Logcat folgen:HauptproblemWenn ich erfolgreich bei @ bestellt ha ConfirmOrderActivity, Es wird angezeigtAlertDialog
vonErfol Nachricht die @ i stornierbar false
. Wenn ich die Anwendung von dieser Aktivität beende,Its ruft beideCheckAutoSyncReceivingOrder
undCheckAutoSyncSendingOrder
automatisch.
Ich rufe beide Dienste von @ LoginActivity
nur, danach wird es automatisch nach bestimmten Intervallen aufgerufen Aber Problem tritt auf, wenn ich zerstöre ConfirmOrderActivity wenn der Dialog angezeigt wird.
Ich wusste nicht, warum es passiert, dass Warum es automatisch ausgeführt wird, wenn ich Activity Directly beende.
Ich habe versuchtonStartCommand()
mitSTART_NON_STICKY
imService
funktioniert aber nicht. (wieSTART_STICKY
ist die Standardeinstellung.)@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
Gibt es eine Lösung?