2013-03-22 97 views
0

我有一個每5分鐘運行一次的IntentService。我遇到的問題是當用戶不在應用程序中時,服務啓動應用程序。我怎樣才能指定的服務,該應用程序不應該啓動?它目前沒有預期的效果,因爲用戶可能在應用程序啓動時發短信。IntentService啓動我的應用程序

我如何開始服務。

// get a Calendar object with current time 
      Calendar cal = Calendar.getInstance(); 
      // add 5 minutes to the calendar object 
      cal.add(Calendar.MINUTE, 1); 
      Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class); 
      intent.putExtra("alarm_message", "sending outstanding transactions"); 
      // In reality, you would want to have a static variable for the request code instead of 192837 
      PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      // Get the AlarmManager service 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
      //43200000 = 12 hours 
      //3600000 = 1hr 
      //1800000 = 30 mins 
      //300000 = 5 mins 

      am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300000 , sender); 

接收器。

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    try { 

    Bundle bundle = intent.getExtras(); 
    String message = bundle.getString("alarm_message"); 
    // Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
    Intent myIntent = new Intent(context, SendOutstandingTransactions.class); 
    myIntent.setAction("com.carefreegroup.startatboot.MyService"); 
    context.startService(myIntent); 

    } catch (Exception e) { 
    Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
    e.printStackTrace(); 

    } 
} 

} 

public class SendOutstandingTransactions extends IntentService { 

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName(); 
    NfcScannerApplication nfcscannerapplication; 
    Cursor c; 
    //LocationManager    mlocManager; 
    //LocationListener   mlocListener; 
    SharedPreferences appSharedPrefs; 
    Editor prefsEditor; 
    String companyID; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     nfcscannerapplication = (NfcScannerApplication)getApplication(); 
     //mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(SendOutstandingTransactions.this.getApplicationContext()); 
     prefsEditor = appSharedPrefs.edit(); 

    } 





    @Override 
    protected void onHandleIntent(Intent intent) { 

     companyID = null; 
     ContentValues messageValues = null; 
     ContentValues phoneNumbers = null; 
     c = nfcscannerapplication.loginValidate.queryAllFromCarer(); 
     String carerId = null; 
     if(c != null && c.getCount() > 0){ 

     c.moveToLast(); 

     carerId = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID)); 
     companyID = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID)); 
     } 

     //check to see if this service has run before 
     Cursor howManyRuns = nfcscannerapplication.loginValidate.queryAllFromBackgroundServicesTable(); 

     if(howManyRuns.getCount() > 0){ 
      //service has run at least once before 
      //do nothing 

     }else{ 

      String hasRunOnce = "true"; 
      ContentValues cv = new ContentValues(); 
      cv.put(LoginValidate.C_BACKGROUNDSERVICES_HAVE_RUN_ONCE, hasRunOnce); 
      nfcscannerapplication.loginValidate.insertIntoBackgroundServicesTable(cv); 

     } 


     Log.e(TAG, "inside onHandleIntent and about to do the service stuff"); 

     if(nfcscannerapplication.getSignalStrength() > 0 && isOnline() == true){ 


      DateTime now = new DateTime(); 
      now = now.minusDays(3); 
      nfcscannerapplication.loginValidate.deleteTransactionsOlderThanSpecificTime(now); 
      nfcscannerapplication.loginValidate.sendOutstandingTransactions(); 
      nfcscannerapplication.loginValidate.checkCompanyOptions(); 

      nfcscannerapplication.loginValidate.deleteTablePhone(); 
      phoneNumbers = nfcscannerapplication.loginWebservice.getCompanyPhonenumbers(companyID); 

    .................. 
.................. 
    .................. 

[EDIT1]

// get a Calendar object with current time 
      Calendar cal = Calendar.getInstance(); 
      // add 5 minutes to the calendar object 
      cal.add(Calendar.MINUTE, 1); 
      Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); 
      intent.putExtra("alarm_message", "sending outstanding transactions"); 
      // In reality, you would want to have a static variable for the request code instead of 192837 
      PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      // Get the AlarmManager service 
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
      //43200000 = 12 hours 
      //3600000 = 1hr 
      //1800000 = 30 mins 
      //300000 = 5 mins 

      am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 150000 , sender); 
+0

你的意思是'不應該開始'?服務是你的應用程序的一部分嗎? – sandrstar 2013-03-22 11:39:41

+0

@sandrstar是的,服務將檢查應用程序的數據庫是否有任何未完成的事務,如果有任何事務將它們發送到Web服務。這些都不需要展示活動。這個想法是在後臺完成的。 – turtleboy 2013-03-22 11:51:55

+0

對不起,我指的是如何指定服務不應該啓動的應用程序 – turtleboy 2013-03-22 11:56:53

回答

1

對於活動出現在屏幕上,東西只好打電話startActivity()。有時計算出什麼正在呼叫startActivity()爲什麼,可能會有問題。但是,通常在代碼中只有幾個地方可以開始任何特定的活動,所以通過斷點,Log語句等,您通常可以追蹤罪魁禍首。

如果沒有startActivity()調用,即使過程包含之前的活動實例,廣播和啓動的服務也將純粹處於後臺。