IntentService启动我的应用程序

时间:2013-03-22 11:31:03

标签: android android-activity intentservice

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

1 个答案:

答案 0 :(得分:1)

要在屏幕上显示活动,必须调用startActivity()。有时候弄清楚正在调用{/ 1>} 以及为什么,可能会有问题。但是,通常代码中只有几个地方可以启动任何给定的活动,因此通过断点,startActivity()语句等,您通常可以追踪罪魁祸首。

如果没有Log电话,广播和已启动的服务将完全在后台,即使该进程包含之前的活动实例。