Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ${YYAndroidPackageName};
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v4.content.WakefulBroadcastReceiver;
- import android.util.Log;
- import android.os.Build;
- import android.app.job.JobInfo;
- import android.app.job.JobScheduler;
- import android.os.PersistableBundle;
- import java.util.Random;
- /**
- * This {@code WakefulBroadcastReceiver} takes care of creating and managing a
- * partial wake lock for your app. It passes off the work of processing the GCM
- * message to an {@code IntentService}, while ensuring that the device does not
- * go back to sleep in the transition. The {@code IntentService} calls
- * {@code GcmBroadcastReceiver.completeWakefulIntent()} when it is ready to
- * release the wake lock.
- */
- public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("yoyo", "GcmBroadcastReceiver received message");
- // Explicitly specify that GcmIntentService will handle the intent.
- ComponentName comp = new ComponentName(context.getPackageName(),
- GcmIntentService.class.getName());
- // Start the service, keeping the device awake while it is launching.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- JobInfo jobInfo = new JobInfo.Builder(new Random().nextInt(), comp)
- .setOverrideDeadline(0)
- .build();
- JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
- jobScheduler.schedule(jobInfo);
- }
- else {
- startWakefulService(context, (intent.setComponent(comp)));
- }
- setResultCode(Activity.RESULT_OK);
- }
- }
Add Comment
Please, Sign In to add comment