2

has anyone know how to restarting a service in android?? i have a service that called when device is booting.. and i have an option.java for saving my configuration..

if i editing a configuration in option.java, then i must restarting my service to takes the effect..

i only know how to start a service and after it running, i don't know how to restart it after a new configuration was made.. any idea??

startService(new Intent(this, ListenSMSservice.class));
Michael Frans
  • 613
  • 3
  • 23
  • 49
  • why do you need to restart your service for the changes to take effect? – MrJre Aug 04 '11 at 11:00
  • just out of curiosity: after saving the configuration, why can't your service read from the same config? i mean, what is the need of restarting the service? if at all you want to re-read the config, why not an observer-observable pattern? – Viren Aug 04 '11 at 11:01
  • @mrjre: i make a lockscreen application that send a report to the owner via SMS (number are registed before in database via option form) when the phone is set to locked (triggered by SMS command), it will send a SMS report to the owner said that his/her device has been locked. a service is started on the phone boot, so if i change an owner number via option, a service will not detect a new configuration (a new owner number) and send SMS report to the old number, because the service has been started before making a new configuration. because of that, i rally need reswtart my service. any idea? – Michael Frans Aug 04 '11 at 23:54
  • @Viren Shakya: the reason i want to restart my service is in my previous comment.. can you explain me about your comments "why not an observer-observable pattern?" thanks.. – Michael Frans Aug 04 '11 at 23:57
  • Please see my response below in details. I can't write code here properly, so providing it as an answer. Please don't downvote if it does not fit your requirements, but I'm just throwing an idea here anyway :) – Viren Aug 05 '11 at 03:27

3 Answers3

3

Just stop the service and start it again

stopService(new Intent(this, ListenSMSservice.class));
startService(new Intent(this, ListenSMSservice.class));
MRK
  • 721
  • 1
  • 8
  • 15
0

In your element:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

public class MyBroadcastreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}

for more elaboration : this

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
0

So by observer-observable design pattern, I meant making use of FileObserver class provided by Android.

For example, here is a snippet from WallPaperManagerService.java from android's source code:

So, in your case, you would create a file observer (see sample code below) on the config file, and each time this config file changes, you will read all the values from your (already running) service.

Hope you got the essence of the idea.

/**
 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
 * that the wallpaper has changed. The CREATE is triggered when there is no
 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
 * everytime the wallpaper is changed.
 */
private final FileObserver mWallpaperObserver = new FileObserver(
        WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
            @Override
            public void onEvent(int event, String path) {
                if (path == null) {
                    return;
                }
                synchronized (mLock) {
                    // changing the wallpaper means we'll need to back up the new one
                    long origId = Binder.clearCallingIdentity();
                    BackupManager bm = new BackupManager(mContext);
                    bm.dataChanged();
                    Binder.restoreCallingIdentity(origId);

                    File changedFile = new File(WALLPAPER_DIR, path);
                    if (WALLPAPER_FILE.equals(changedFile)) {
                        notifyCallbacksLocked();
                    }
                }
            }
        };
Viren
  • 2,161
  • 22
  • 27
  • still confused here, but thanks for your code.. i'm really appreciate that.. :) – Michael Frans Aug 05 '11 at 04:49
  • I am thinking of using this observer relationship for my app, but I'm worried about the memory / battery usage. How would this be a better option than just manually pushing a change when an option is changed? – Josh Feb 01 '12 at 11:07
  • as far as i know, the fileobserver class is a simple implementation/wrapper on linux' inotify system. many other system components in android use this. so it would be safe to use as long as you are not putting observers on a very large set of files/directories. – Viren Feb 01 '12 at 23:01