0

我有一个每 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);

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

[编辑1]

// 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);
4

1 回答 1

1

为了让活动出现在屏幕上,必须调用startActivity(). 有时弄清楚什么在召唤startActivity(),以及为什么,可能会有问题。但是,通常您的代码中只有几个地方可以启动任何给定的活动,因此通过断点、Log语句等,您通常可以找到罪魁祸首。

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

于 2013-03-22T16:35:03.363 回答