Starting service on boot to to do work every 15 minutes

核能气质少年 提交于 2019-12-10 12:19:41

问题


So, i have gone through internet looking for solution, but didnt find anything that combines what I need.

I need this: if mobile gets restarted, I need to start a service on boot that will every 15 minutes send data to my server. I made code that is working for boot, but don't know how to implement timer. Do I need 2 broadcast receivers and 2 services or?

My broadcast receiver:

public class BrReceiver extends BroadcastReceiver {

    final public static String ONE_TIME = "onetime";

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

            Intent serviceIntent = new Intent(context, Service.class);
            context.startService(serviceIntent);
        }

    }

}

and my Service:

public class Service extends IntentService {

    private static final String TAG = "DownloadService";

    public Service() {
        super(Service.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d(TAG, "Service Started!");


    }


}

and my AndroidManifest:

<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
        <receiver android:name=".services.BrReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

          <service
            android:name=".services.Service"
            android:exported="false" />

This service that is going to be repeated every 15 minutes must not start from the activity, but on boot.


回答1:


Best way to start service on a regular interval use alarm manager as below:

// Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);
    Intent intent = new Intent(Main.this, Service_class.class);
    PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
            0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            36000 * 1000, pintent);

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), Service_class.class));

        }
    });

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), Service_class.class));
        }
    });
}

Thanks



来源:https://stackoverflow.com/questions/32297816/starting-service-on-boot-to-to-do-work-every-15-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!