从库提供的类派生几个类,代码如下:
public class DemoJobCreator implements JobCreator { @ Override public Job create( String tag) { switch (tag) { case DemoSyncJob. TAG: return new DemoSyncJob(); default: return null; } } } public class DemoSyncJob extends Job { public static final String TAG = "job_ demo_ tag"; @ Override @ NonNull protected Result onRunJob( final Params params) { if (params. isPeriodic()) { PendingIntent pendingIntent = PendingIntent. getActivity(
getActivity( getContext(), 0, new Intent (getContext(), MainActivity. class), 0); Notification notification = new NotificationCompat. Builder (getContext()) . setContentTitle(" Job Demo") . setContentText(" Periodic job run") . setAutoCancel( true) . setContentIntent( pendingIntent) . setSmallIcon( R. drawable. ic_ notifications_ black_ 24dp) . setShowWhen( true) . setColor( Color. GREEN) . setLocalOnly( true) . build(); NotificationManagerCompat. from( getContext()). notify( new Random(). nextInt(), notification); EamLog. v(" job", "isPeriodic== true"); } else { EamLog. v(" job", "isPeriodic== false"); } return Result. SUCCESS; }}