IntentService启动我的app



我有一个每5分钟运行一次的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);

要在屏幕上显示活动,必须调用startActivity()。有时,弄清楚调用startActivity()的内容以及为什么调用可能会有问题。但是,通常在你的代码中只有几个地方你开始任何给定的活动,所以通过断点,Log语句,或者类似的,你通常可以追踪罪魁祸首。

在没有startActivity()调用的情况下,广播和启动的服务将纯粹在后台,即使进程包含之前的活动实例。

最新更新