SHARE
TWEET

Untitled

a guest Aug 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MainActivity
  2. protected void onCreate(Bundle savedInstanceState) {
  3.     ...
  4.     startService(new Intent(this, ConnService.class));
  5.     ...
  6. }
  7.  
  8. // ConnService
  9. public void onCreate() {
  10.     DB = new DBClass();
  11.     DB.connect(this);
  12.     new Thread(new Runnable() {
  13.         @Override
  14.         public void run() {
  15.             while (!finishing) {
  16.                 Help.log("Service loop");
  17.                 DB.syncWithServer();
  18.                 Help.sleep(30 * 1000);
  19.             }
  20.         }
  21.     }).start();
  22. }
  23. @Override
  24. public int onStartCommand(Intent intent, int flags, int startId) {
  25.     return Service.START_STICKY;
  26. }
  27.      
  28. private void setOnetimeTimer(Context context) {
  29.     AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  30.     Intent intent = new Intent(context, AlarmReceiver.class);
  31.     PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  32.     am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + (1000 * 30), pi);
  33. }
  34. ...
  35.             while (!finishing) {
  36.                 Help.log("Service loop");
  37.                 DB.syncWithServer();
  38.                 setOnetimeTimer(ctx); // <------
  39.             }
  40. ...
  41.      
  42. @Override
  43. public void onTaskRemoved(Intent rootIntent){
  44.     Intent intent = new Intent(getApplicationContext(), this.getClass());
  45.     intent.setPackage(getPackageName());
  46.     PendingIntent pi = PendingIntent.getService(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
  47.     AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
  48.     am.set(AlarmManager.ELAPSED_REALTIME,
  49.            SystemClock.elapsedRealtime() + 1000,
  50.            pi);
  51.     super.onTaskRemoved(rootIntent);
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top