How to run service in background every 10 minute?

后端 未结 5 1005
独厮守ぢ
独厮守ぢ 2021-02-10 03:04

I want to use a service that run in background indefinitely and call a method every 10 minute and its running even app killed

How to create it?

相关标签:
5条回答
  • 2021-02-10 03:41

    For "how to work with services", see
    Services - Android
    Services in Android - Vogella

    Here is a clear solution that focus on "every 10 minutes" part using AlarmManager: https://stackoverflow.com/a/10222390/2591556

    0 讨论(0)
  • 2021-02-10 03:45

    Assuming you have a Running Service

    User AlarmManager to run Service every 10 minutes

       AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Intent i = new Intent(context, YourService.class);
             PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
             am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 600000, pi); // Millisec * Second * Minute
         }
    
    0 讨论(0)
  • 2021-02-10 03:46

    You can do by using service as follows

    @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
    
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
    
            @Override
            public void run() {
                //performe the deskred task
            }
        }, 10minutes time in milisecods);
    
          // If we get killed, after returning from here, restart
          return START_STICKY;
      }
    

    This service will get started automatically even if app get killed, and postdelayed will run

    0 讨论(0)
  • 2021-02-10 03:52

    You can use the Alarm manager which will be called after every 10 mins

            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Random random = new Random();
            int m = random.nextInt(9999 - 1000) + 1000;
    
            Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
            notificationIntent.setClass(this,AlarmReceiver_.class);
            notificationIntent.addCategory("android.intent.category.DEFAULT");
    
            PendingIntent broadcast = PendingIntent.getBroadcast(YourClass.this, m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + 300000L,
                    600000L, broadcast);
    

    ManiFest receiver Code where you will get a receiver response

    <receiver android:name="com.yourpackage.AlarmReceiver_"
    
            >
            <intent-filter>
                <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.REBOOT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
    
            </intent-filter>
        </receiver>
    

    You will have to create Receiver where you will receive data as an above-specified name of AlarmReceiver_.class

    0 讨论(0)
  • 2021-02-10 04:04

    you could write a background Service: Running in a Background Service

    and start the service every 10-11 min (cause of AlarmManager power saving behaviour), or with exact timing (needs to shedule next execution every time) with AlarmManager.setExact

    Example:

    private static PendingIntent createClockIntent(Context context) {
            Intent intent = new Intent(context.getString(R.string.widget_broadcast_clock_update));
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 1,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            return pendingIntent;
        }
    
        public static void startClockAlarm(Context context) {
            AlarmManager alarmManager = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            clockIntent = createClockIntent(context);
            alarmManager.setRepeating(AlarmManager.RTC, 0,
                    600000, clockIntent);
        }
    
    0 讨论(0)
提交回复
热议问题