Android service starts only in debug mode

I created a service that manages an XMPP connection. My application should receive XMPP messages periodically. Everything seems to work as expected, but only when the phone is connected to Android Studio and I launch the application in debug mode. When I disconnect the phone or even if it is connected, but I start the application from the phone and not from AS, the service does not seem to start ...

I made sure my service is declared correctly in the manifest:

<service
  android:name=".xmpp.MyService"
  android:enabled="true" />

where .xmpp is the subpackage in my main package.

Here is my service

public class MyService extends Service {
    public static ConnectivityManager cm;
    public static MyXMPP xmpp;
    private static String LOG_TAG = "MyService";


    @Override
    public IBinder onBind(final Intent intent) {
        Log.v(LOG_TAG, "in onBind");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (CurrentInfo.getUser() != null) {
            xmpp = MyXMPP.getInstance(MyService.this, getResources().getString(R.string.xmpp_url), CurrentInfo.getUser().getJabberId(), getString(R.string.xmpp_password));
            xmpp.connect("onCreate");
        }
    }


    @Override
    public int onStartCommand(final Intent intent, final int flags,
                              final int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        xmpp.connection.disconnect();
    }

    public static boolean isNetworkConnected() {
        return cm.getActiveNetworkInfo() != null;
    }
}

And I start my service as follows:

// Start xmpp service here
Intent i = new Intent(getBaseContext(), MyService.class);
startService(i);

Thank!

+4
source share

All Articles