Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MainActivity
- protected void onCreate(Bundle savedInstanceState) {
- ...
- startService(new Intent(this, ConnService.class));
- ...
- }
- // ConnService
- public void onCreate() {
- DB = new DBClass();
- DB.connect(this);
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (!finishing) {
- Help.log("Service loop");
- DB.syncWithServer();
- Help.sleep(30 * 1000);
- }
- }
- }).start();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- return Service.START_STICKY;
- }
- private void setOnetimeTimer(Context context) {
- AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(context, AlarmReceiver.class);
- PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + (1000 * 30), pi);
- }
- ...
- while (!finishing) {
- Help.log("Service loop");
- DB.syncWithServer();
- setOnetimeTimer(ctx); // <------
- }
- ...
- @Override
- public void onTaskRemoved(Intent rootIntent){
- Intent intent = new Intent(getApplicationContext(), this.getClass());
- intent.setPackage(getPackageName());
- PendingIntent pi = PendingIntent.getService(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
- AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
- am.set(AlarmManager.ELAPSED_REALTIME,
- SystemClock.elapsedRealtime() + 1000,
- pi);
- super.onTaskRemoved(rootIntent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement