Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package ${YYAndroidPackageName};
  2.  
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.support.v4.content.WakefulBroadcastReceiver;
  8. import android.util.Log;
  9.  
  10. import android.os.Build;
  11. import android.app.job.JobInfo;
  12. import android.app.job.JobScheduler;
  13. import android.os.PersistableBundle;
  14. import java.util.Random;
  15.  
  16. /**
  17. * This {@code WakefulBroadcastReceiver} takes care of creating and managing a
  18. * partial wake lock for your app. It passes off the work of processing the GCM
  19. * message to an {@code IntentService}, while ensuring that the device does not
  20. * go back to sleep in the transition. The {@code IntentService} calls
  21. * {@code GcmBroadcastReceiver.completeWakefulIntent()} when it is ready to
  22. * release the wake lock.
  23. */
  24.  
  25. public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
  26. {
  27. @Override
  28. public void onReceive(Context context, Intent intent) {
  29.  
  30. Log.i("yoyo", "GcmBroadcastReceiver received message");
  31.  
  32.  
  33. // Explicitly specify that GcmIntentService will handle the intent.
  34. ComponentName comp = new ComponentName(context.getPackageName(),
  35. GcmIntentService.class.getName());
  36.  
  37. // Start the service, keeping the device awake while it is launching.
  38. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  39. JobInfo jobInfo = new JobInfo.Builder(new Random().nextInt(), comp)
  40. .setOverrideDeadline(0)
  41. .build();
  42. JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
  43. jobScheduler.schedule(jobInfo);
  44. }
  45.  
  46. else {
  47. startWakefulService(context, (intent.setComponent(comp)));
  48. }
  49.  
  50. setResultCode(Activity.RESULT_OK);
  51. }
  52. }
Add Comment
Please, Sign In to add comment