How can I restart the service in android programmatically?

In Android, I have an application with various actions and services. These actions are intended for direct interaction with the user, in which the user can change data, update data, enter data, whatever.

The application also has a (background) service, using data entered by the user (stock data). A regular service checks stock prices (for example, every 12 hours) and gives the user a notification if some conditions match.

However, as a user, I want to be able to change the settings for this service. I run the application and can enter the update interval in one of the applications that is stored in SharedPreference. I can change the value, for example, so that the service checks the stock data every 3 hours.

Should I restart the service? Since I understand the β€œservices” in the operating system, the service starts until it stops by an event. But if the service is not stopped, the service will not β€œnotice” that the update interval has changed! Thus, in my activity from my main application, I need a way to "restart" the service, so it checks stock prices for 3 hours, not 12 hours!

Any idea how to do this? A google search really didn't help. Maybe I was looking for the wrong keywords ...?

+4
source share
4 answers

A simple solution to this, since your service works in the same process as your application, would register a method OnSharedPreferenceChangeListenerin your service onCreatefor the same SharedPreferencesone that will be updated.

Then, every time a preference onSharedPreferenceChanged(...)is called in your service , it will be called. You can check when the preference from the update interval in particular will change and update your service accordingly, without restarting it.

0
source

AIDL. AIDL . , . .

+1

Intent s. Service BroadcastReceiver . . :

public class CustomService extends Service {

    private BroadcastReceiver mReceiver;

    @Override
    public void onCreate() {
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
              // get the action from the intent
            }
        }
        registerReceiver(wifiChangedState, new IntentFilter("CUSTOM");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

Intent Activity, BroadcastReceiver.

0

Service Binder

    public class CustomService extends Service {

        public IBinder onBind(Intent intent) {
            return new CustomBinder(this);
        }

        public void doSomething(){
            //Do somthing with service
        }

    }

    public class CustomBinder extends Binder {

        private CustomService mCustomService;

        public MyBinder(CustomService service) {
            mCustomService = service;
        }

        public CustomService getCustomService() {
            return mCustomService;
        }
    }

CustomService ServiceConnection ( ServiceConnection - ...) , .

    Intent customServiceIntent = new Intent(context, CustomService.class);
    context.bindService(
            customServiceIntent, 
            mServiceConnection, 
            Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
    );

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            CustomBinder binder = (CustomBinder) service;
            binder.getCustomService().doSomething();
        }
        public void onServiceDisconnected(ComponentName name) {
            /* Do something when service disconnect */
        }
    };

. ( ), AIDL .

, "" , AlarmManager .

    /* define when intent will be send */
    long wakeUpTimeInMillis = Calendar.getInstance().getTimeInMillis() + 1000 * 30 ; // now + 30second 

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, CustomService.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, wakeUpTimeInMillis, pending);

intent , onStartCommand(Intent intent, int flags, int startId) CustomService

0

All Articles