草庐IT

Android-JobScheduler 的 setPeriodic 将不起作用

coder 2023-12-20 原文

我编写了一个使用本地 SqliteDatabase 创建和提醒任务的应用程序。 我编写了一个 jobScheduler 服务来检查设备时间和日期以及数据库中的任务,如果匹配则显示推送通知。 我还想要在后台运行并每 5 秒检查一次数据的服务。 但是当我写

builder.setPeriodic(5000); builder.setPersisted(true);

服务停止检查数据。

这是我的代码

主 Activity

      public class MainActivity extends AppCompatActivity  {
ImageButton plusImageBtn;
DatabaseHelper databaseHelper;
BottomNavigationView navigation;
Toolbar toolbar;
private ComponentName mServiceComponent;
private int jobId=0;
private static final String TAG = MainActivity.class.getSimpleName();
public static final String WORK_DURATION_KEY =
        BuildConfig.APPLICATION_ID + ".WORK_DURATION_KEY";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    navigation = (BottomNavigationView) findViewById(R.id.navigation);
    plusImageBtn = (ImageButton) findViewById(R.id.plusImagBtn);
    toolbar= (Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);

    mServiceComponent = new ComponentName(this, JobSchedulerService.class);

    databaseHelper= new DatabaseHelper(this);
    navigation.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.navigation_calendar:
                            selectedFragment = new CalendarFragment();
                            break;
                        case R.id.navigation_home:
                            selectedFragment = new ViewListContents();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;

                }
            });
    FragmentTransaction transaction = 
     getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout,new  ViewListContents());
    transaction.commit();
    scheduleJob();

}

@Override
protected void onStart() {
    super.onStart();
    // Start service and provide it a way to communicate with this class.
    Intent startServiceIntent = new Intent(this, JobSchedulerService.class);
    startService(startServiceIntent);
}

@Override
protected void onStop() {
    stopService(new Intent(this,JobSchedulerService.class));
    super.onStop();
}

public void scheduleJob() {
    JobInfo.Builder builder = new JobInfo.Builder(jobId++, mServiceComponent);
 //  builder.setPeriodic(5000);
   // builder.setPersisted(true);

    builder.setMinimumLatency(1000);
    builder.setOverrideDeadline(1000);

    // Extras, work duration.
    PersistableBundle extras = new PersistableBundle();
    extras.putLong("",5000);
    builder.setExtras(extras);
    // Schedule job
    Log.d(TAG, "Scheduling job");
    JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.schedule(builder.build());
    Toast.makeText(this, "Scheduling job  ", Toast.LENGTH_SHORT).show();

}}

JobSchedulerService

  public class JobSchedulerService extends JobService {
int id;
String stringId;
String date;
String taskDate,taskTitle,taskTime,sepYear,sepMonth, 
 sepDay,convert,DeviceDate;
Cursor taskDateCursor;
DatabaseHelper databaseHelper;
Roozh roozh;
String[] seperatedString;
int iYear,iMonth, iDay;
SimpleDateFormat dateFormat,  timeFormat;
private static final String TAG = JobSchedulerService.class.getSimpleName();

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Service created");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "Service destroyed");   }

@Override
public boolean onStartJob(final JobParameters params) {
    stringId = String.valueOf(params.getJobId());
    id = params.getJobId();
    final long duration = params.getExtras().getLong(WORK_DURATION_KEY);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            databaseHelper= new DatabaseHelper(getApplicationContext());

            taskDateCursor=databaseHelper.getDateForNotification();
            if (taskDateCursor.moveToFirst()){
                do {
                    taskTitle=taskDateCursor.getString(taskDateCursor.getColumnIndex(DatabaseHelper.COL2));
                    taskDate=taskDateCursor.getString(taskDateCursor.getColumnIndex(DatabaseHelper.COL3));
                    taskTime=taskDateCursor.getString(taskDateCursor.getColumnIndex(DatabaseHelper.COL4));


                    roozh= new Roozh();
                    seperatedString=taskDate.split("/");
                    sepYear=  seperatedString[0];
                    sepMonth=  seperatedString[1];
                    sepDay=  seperatedString[2].trim();
                    iYear= Integer.parseInt(sepYear);
                    iMonth= Integer.parseInt(sepMonth);
                    iDay= Integer.parseInt(sepDay);
                    roozh.PersianToGregorian(iYear,iMonth,iDay);
                    convert= roozh.toString();
                    dateFormat= new SimpleDateFormat(
                            "yyyy-MM-dd", Locale.getDefault());
                    DeviceDate= dateFormat.format(new Date());
                    timeFormat=new SimpleDateFormat("HH:m",Locale.getDefault());

                    String  deviceTime=timeFormat.format(new Date());

                    RemoteViews remoteViews= new RemoteViews(getPackageName(),R.layout.notification);
                    remoteViews.setImageViewResource(R.id.notif_image, R.mipmap.ic_launcher);
                    remoteViews.setTextViewText(R.id.title,taskTitle);
                    remoteViews.setTextViewText(R.id.text,taskTime);

                    if (DeviceDate.equals(convert)  && deviceTime.equals(taskTime) ){
                        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                                .setSmallIcon(R.drawable.drawable_circle)
                                .setContent(remoteViews)
                                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                                .setColor(ContextCompat.getColor(getApplication(), R.color.primaryDarkColor))
                                .setShowWhen(true)
                                .setAutoCancel(true);
                        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
                        notificationManager.notify(0, mBuilder.build());

                    }

                    Log.i(TAG, "data " + DeviceDate+ "task" + convert+ " " + "dd" + " "+  taskTime + "dt" + "" + deviceTime);
                }while (taskDateCursor.moveToNext());
            }            }
    }, duration);
    Log.i(TAG, "on start job: " + params.getJobId());
   return true;    }

@Override
public boolean onStopJob(JobParameters params) {
    Log.i(TAG, "on stop job: " + params.getJobId());
    return true;    }

最佳答案

我认为您的问题是您希望执行工作的频率。在标准 AOSP 中,作业的最短周期为 15 分钟。所以它可能不是适合您的 API。警报管理器可能是您想要的,但每 5 秒设置一个警报很昂贵。谷歌也随着每个版本越来越多地限制后台服务。只是要记住一些事情。

参见: JobScheduler not repeating job

关于Android-JobScheduler 的 setPeriodic 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49832467/

有关Android-JobScheduler 的 setPeriodic 将不起作用的更多相关文章

  1. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  4. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

  5. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  6. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  7. ruby-on-rails - Simple_form 必填字段不起作用 - Ruby on Rails - 2

    我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt

  8. ruby-on-rails - Heroku Action 缓存似乎不起作用 - 2

    我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错

  9. ruby-on-rails - Rake 预览在 Octopress 中不起作用 - 2

    我在我的机器上安装了ruby​​版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals

  10. ruby - 比较运算符不起作用(在 erb View 中) - 2

    我是RubyonRails的新手,我正在尝试编写一个morethan表达式:5%>大于号不断抛出异常捕获错误。我不确定如何解决这个问题?编辑:这不是rails,也不是View,它是一个Ruby构造 最佳答案 使用5%>错误来自photo_limit而不是从Integer延伸类(猜测它真的是一个字符串),因此没有混合比较方法/s有关更多信息,请参阅:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/特别是你必须混入Comparable并定义方法。虽然在这

随机推荐