【问题标题】:Android JobScheduler multiple threadsAndroid JobScheduler 多线程
【发布时间】:2018-01-08 00:35:23
【问题描述】:

我正在使用 JobScheduler 每 15 分钟发送一封电子邮件。 JobScheduler 安排在 MainActivity onCreate 中,因此每次打开主要活动时,都会发送电子邮件。但 15 分钟后,一次只发送一封电子邮件。我希望该动作每 15 分钟发生一次,而不是每次调用 MainActivity 的 onCreate 时发生一次。

ComponentName componentName = new ComponentName(getApplicationContext(), JobSchedulerService.class);
    JobInfo jobInfo = new JobInfo.Builder(1, componentName).setPeriodic(900000).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build();

    JobScheduler jobScheduler =
            (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(jobInfo);

这是我在 jobService 类中的 onStart

   public boolean onStartJob(JobParameters params) {
    this.params = params;
    String filename = "TodaysRecords.csv";
    this.dbHandler = new MyDBHandler(this, null, null, 1);
    File recordsFile = dbHandler.exportDB(getApplicationContext().getFilesDir(), filename);

    String receiverEmail = dbHandler.returnEmail();

    String[] recipients = { receiverEmail };
    SendEmailAsyncTask email = new SendEmailAsyncTask();
    //email.activity = this;
    email.m = new Mail("someemail", "password");
    email.m.set_from("someemail");
    email.m.setBody("Attached is today's records from in&out android application");
    email.m.set_to(recipients);
    email.m.set_subject("In&Out Records");
    try {
        email.m.addAttachment(recordsFile.getAbsolutePath().toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    email.execute();

    return false;
}

【问题讨论】:

  • 您是否在您的postExecute 上致电jobFinished?此外,您可以将调度从 mainactivity 移动到应用程序类,以避免每次打开应用程序时都启动它。

标签: android android-jobscheduler


【解决方案1】:

您可以在广播接收器中安排您的作业,以侦听启动完成的意图。安装您的应用程序后,用户必须在启动完成广播开始发送之前打开它一次。然后将在每次启动时安排该作业。请评论是否必须在您的 MainActivity onCreate() 中完成调度。

public class BootCompletedRcvr extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // check for boot complete intent
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Context mContext = context.getApplicationContext();
        ComponentName componentName = new ComponentName(
            mContext, JobSchedulerService.class);
        JobInfo jobInfo = new JobInfo.Builder(1, componentName)
            .setPeriodic(900000)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .build();

        JobScheduler jobScheduler = (JobScheduler)
                 mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(jobInfo);
    }
}
}

请务必在您的 AndroidManifest.xml 中声明:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

还有这个:

<receiver android:name=".BootCompletedRcvr">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

【讨论】:

    【解决方案2】:

    首先: 您应该将 JobInfo 添加到 ApplicationClass 中的 JobScheduler 第二: 内部

    onPostExecute
    

    使用 false 调用 JobFinish 方法,如下所示

    jobFinished(params,false)
    

    它应该可以工作:)

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多