我遇到的问题是Activity + Service
,我有以下数量的活动和服务.
LoginActivity => OrderListActivity => AddOrderActivity => ConfirmOrderActivity
ReceivingOrderService - 从服务器接收新数据
SendingOrderService - 将新数据发送到服务器
在某个间隔的持续时间内,来自另一个单独服务的服务呼叫.
CheckAutoSyncReceivingOrder - 调用ReceivingOrderService(间隔15分钟)
CheckAutoSyncSendingOrder - 调用SendingOrderService(Interval 3Mins)
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..."); } }
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..."); } }
new AsyncTask() { 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();
根据在数据库中插入记录的流程,一切正常.
添加查询后: 销毁活动并获取以下Logcat:当我从放置顺序成功ConfirmOrderActivity,它显示AlertDialog
的成功消息,其是可取消 false
.当我从这个活动停止的应用程序,它的调用都CheckAutoSyncReceivingOrder
和CheckAutoSyncSendingOrder
自动.
我
LoginActivity
只调用两个Service ,之后它会在给定的时间间隔后自动调用但是当我在显示对话框时销毁ConfirmOrderActivity时出现问题.
我不知道为什么它会在我直接停止Activity时自动运行.
onStartCommand()
与START_NON_STICKY
中Service
,但不工作.(START_STICKY
默认情况下.)@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_NOT_STICKY; }
有什么解决方案吗?