简体   繁体   中英

Android service won't start - in manifest… but nothing?

UPDATE:

After adding the suggested methodes ( doBindService() and doUnbindService() ) along with calls to no avail) From here suggested by @Nick Campion

I've been trying for a while to get this service running but nothing seems to be working - I know I'm probably missing a semicolon or something :)

The program calls startNotificationService() , then the Log shows the log message... and the app continues to run without the Service showing up. I can't find the Service in Advance Task Killer. HELP!!!

XML (In Manifest) :

    <service 
        android:icon="@drawable/icon" 
        android:label="Smart Spdate Service"
        android:name="notifyService">
    <intent-filter 
        android:label="FULL_PATH_NAME_HERE.updateService">
    </intent-filter>
</service>

Service Call

    Log.v("NOTICE", "Notification Service was not found running - starting");
    //startService(new Intent(this, notifyService.class));
    startService(new Intent(notifyService.class.getName()));
    //startService(new Intent(TweetCollectorService.class.getName()));

     /* FROM GOOGLE */
     void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    this.bindService(new Intent(this, updateService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}
/* END OF GOOGLE CODE */
@Override
public void onDestroy() {
    web.close();
    doUnbindService(); // Added to `onDestroy` - suggested by Google page
    super.onDestroy();
    Log.v("NOTICE", "PROGRAM TERMINATED");
}

updateService.java

public class updateService extends Service {
private String TAG = "SERVICE";
public static final int INTERVAL = 60000;
private Timer timer = new Timer();
private static updateService Pointer;

public updateService() {
    Pointer = updateService.this;
}

public static class LocalBinder extends Binder {
    static updateService getService() {
        return Pointer;
    }
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onDestroy() {
    if (timer != null) {
        timer.cancel();
    }
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            doStuff();
        }
    }, 0, INTERVAL);
    super.onStart(intent, startId);
}

public void doStuff() {
    Log.v(TAG, "doStuff");
}

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

private final IBinder mBinder = new LocalBinder();

}

I don't see anywhere where your client binds to your service. Take a look at the local service example. . The reason for using the bind pattern even though you call startService is because the startService call is asynchronous. You need to make an additional call to bind the service to make sure you get a call back once the startup is complete.

I've found that a really great example of a service client and service are available in the NPR Open Source App for you to learn from!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM