Oreo Calendar changes

╄→尐↘猪︶ㄣ 提交于 2019-12-11 17:10:05

问题


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>

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.


回答1:


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

Manifest file:

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

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.



来源:https://stackoverflow.com/questions/49616809/oreo-calendar-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!