Restarting a service.

by M.Manjunatha » Wed, 29 Apr 2009 20:44:52 GMT

Sponsored Links
 Hi Folks,

How do I restart my service automatically when my service gets killed
or crashes, until and unless I myself deregister the service.

public class Myservice extends Service {

      public void Myservice(){

      public void onStart(Intent intent, int startId) {



Restarting a service.

by Todd Sjolander » Thu, 30 Apr 2009 22:47:45 GMT

 As I understand it, if your Service (or Activity, for that matter),
dies due to the system reclaiming resources or a crash, the OS will
restart it for you automatically.  That means if you were to configure
it to start upon boot (using android.permission.RECEIVE_BOOT_COMPLETED
and implementing a receiver that handles
android.intent.action.BOOT_COMPLETED), you could be pretty certain
that your service is always running...once the user restarts the phone
after installation.  Or if you have some kind of user interface for
the product (can't see how you wouldn't!), it could start the service
if it isn't already running.  Then there wouldn't be a restart needed.

Todd Sjolander


Sponsored Links

Other Threads

1. Problems with AccountManager

I've researched the earlier threads about AccountManager and still am
having problems. I'm trying to extend my Twitter OAUTH demo to use the
AccountManager. I've built the beginnings of a class based on
AbstractAccountAuthenticator and a Service that seems to do what it is
supposed to do.

My Authenticator does get installed on a 2.1 Emulator. I can see it
when I go to the Accounts & Sync Settings app. If I click on Add
Accounts and then on My Authenticator, I go through the OAUTH process,
with an Activity being fired off (by the Account Manager?) from the
KeyIntent that the authenticator returns. The authentication process
proceeds and the Activity fields the redirection from the browser in
its onNewIntent() function (it's a SingleInstance activity).
Everything appears to be working and the Activity returns its results
the way it seems it is supposed to. The problem is, the new account
never shows up in the Accounts list on the Accounts & Sync page.

My second problem is that I also have an Activity in the application
that contains the authenticator. When I try to do an addAccount() from
that activity, which is my original demo tiny Twitter client, I get a
very succinct bind failure of my service to the AccountManager.

So, when I run the Authenticator from the Accounts & Sync Settings
page, it all works, except the new account isn't showing up. When I
try to run the Authenticator from my Activity which is in the same
package, I get a bind failure.

Now I'll document how I'm returning the result in the Activity, which
is derived from AccountAuthenticatorActivity:

    public void onNewIntent(Intent i) {
            Bundle b = new Bundle();
        String verifier;
            try {
                Uri data = i.getData();
                if(data == null || (verifier = data.getQueryParameter
(OAuth.OAUTH_VERIFIER)) == null) {
                                b.putInt(AccountManager.KEY_ERROR_CODE, -10000);
                } else {
                            String token = mConsumer.getToken();
                            String secret = mConsumer.getTokenSecret();
                            b.putString(AccountManager.KEY_ACCOUNT_NAME, token);
                            b.putString(AccountManager.KEY_AUTHTOKEN, secret);
                } catch (OAuthMessageSignerException e) {
                        b.putInt(AccountManager.KEY_ERROR_CODE, -1);
                        mResponse.onError(-1, e.getMessage());
                } catch (OAuthNotAuthorizedException e) {
                        b.putInt(AccountManager.KEY_ERROR_CODE, -2);
                        mResponse.onError(-2, e.getMessage());
                } catch (OAuthExpectationFailedException e) {
                        b.putInt(AccountManager.KEY_ERROR_CODE, -3);
                        mResponse.onError(-3, e.getMessage());
                } catch (OAuthCommunicationException e) {
                        b.putInt(AccountManager.KEY_ERROR_CODE, -4);
                        mResponse.onError(-4, e.getMessage());

And here is the short stack trace I get when I try to addAccount from
my Activity, rather than through the Accounts & Sync page:

W/System.err(  264): android.accounts.AuthenticatorException: bind
W/System.err(  264):    at
W/System.err(  264):    at android.accounts.AccountManager.access$500
W/System.err(  264):    at android.accounts.AccountManager$AmsTask
W/System.err(  264):    at android.accounts.IAccountManagerResponse
W/System.err(  264):    at android.os.Binder.execTransact(
W/System.err(  264):    at


2. Any way a WebView can show s context menu when not on a link

We are using a WebView for eBooks whic have few (if any) links and do
not see a good way to
add a context menu in a region with text only - any bright ideas??


3. My Space

4. Question on modifying application framework + importing to the Windows SDK

5. ItemizedOverlay animations on a MapView?

6. getContextMenuInfo() returns null on sub-menu MenuItems

7. OOT : soal IT