奥利奥日历更改 [英] Oreo Calendar changes

查看:109
本文介绍了奥利奥日历更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用清单中的以下代码获取日历更改:

I used to get the Calendar changes using this code in the manifest:

    <receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROVIDER_CHANGED" />
            <data android:scheme="content" />
            <data android:host="com.android.calendar" />
        </intent-filter>
    </receiver>

但是Android 8 Oreo无法做到这一点.我已经读过它需要一份Job或类似的东西(我从未使用过工作,我不需要它们).日历中有任何更改时,任何人都可以键入要被解雇的工作示例吗?就像我上面的代码曾经用于该简单的广播接收器一样.

But this is not possible with Android 8 Oreo. I have read it needs a Job or something similar (I have never used jobs yet,I haven't need them). Could any one type an example of a job to get fired every time there is a change in the Calendar. Just like my above code used to work with that simple Broadcast Receiver.

推荐答案

感谢CommosWare的技巧,我能够做到这一点.我在这里输入完整的代码:

Thanks to CommosWare's tip, I was able to do it. I type here the complete code:

清单文件:

<service android:name=".MyReceiverAsJob"
         android:permission="android.permission.BIND_JOB_SERVICE" />

文件MyReceiverAsJob.java:

File MyReceiverAsJob.java:

package my.package;        
import android.annotation.TargetApi;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.provider.CalendarContract;
import android.widget.Toast;


@TargetApi(Build.VERSION_CODES.N)
public class MyReceiverAsJob extends JobService {
    private static final int iJobId = 1005; //Job Id
    final Handler mHandler = new Handler(); //Just to display Toasts

    static void schedule(Context oContext) {    
        ComponentName oComponentName = new ComponentName(oContext, clsJobServiceRecibidorDeGoogleCalendar.class);
        JobInfo.Builder oJobInfoBuilder = new JobInfo.Builder(ME_MYSELF_AND_I, oComponentName);
        final Uri CALENDAR_URI = Uri.parse("content://" + CalendarContract.AUTHORITY + "/");
        oJobInfoBuilder.addTriggerContentUri(new JobInfo.TriggerContentUri(CalendarContract.CONTENT_URI, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
        oJobInfoBuilder.addTriggerContentUri(new JobInfo.TriggerContentUri(CALENDAR_URI, 0));
        JobScheduler jobScheduler = (JobScheduler) oContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(oJobInfoBuilder.build());
    }


    @Override
    public boolean onStartJob(JobParameters params) {

        DisplayToast("Calendar has been modified. Do some work!");

        schedule(this); //Reschedule to receive future changes
        return (false);
    }


    @Override
    synchronized public boolean onStopJob(JobParameters params) {
        return (false);
    }


    void DisplayToast(final CharSequence text) {
        mHandler.post(new Runnable() {
            @Override public void run() {
                Toast.makeText(clsJobServiceRecibidorDeGoogleCalendar.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我从前台服务中这样调用它:

And I call it from my foreground service like this:

MyReceiverAsJob.schedule(this);

它可以工作,但是有时在日历中没有任何更改的情况下调用它.如果我做错了,请告诉我.

It works, but sometimes it gets called without nothing being changed in the Calendar. If I am doing something wrong, please, let me know.

这篇关于奥利奥日历更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆