Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service in separate process

I'm running my application with single activity and call startService(new Intent(this, TCPClient.class)); in onStart. Also I start thread in onCreate() of service that sets up TCP connection to my server. Service is running in separate process. It works well until I restart my application (I do not stop service when app is closed). When I do that, I'm getting 1 more connection from same IP. So, I have 2 client connected from same device and same IP. Question is: How to prevent creating more services?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.servicetest" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MainActivityTheme" >
        <!-- android:theme="@style/AppTheme" > -->
        <service android:name=".TCPClient"
            android:process=":service">
        </service>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/MainActivityTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

OnStart:

@Override
    protected void onStart() {
        super.onStart();

        Log.v(TAG, "onStart");
        startService(new Intent(this, TCPClient.class));
    }

onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                if (bundle.containsKey("stop"))
                {
                    stopClient();
                }
            }
        }
        Log.v(TAG, "onStartCommand...");
        return TCPClient.START_STICKY;
    }

stopClient:

private void stopClient() {

        // send mesage that we are closing the connection
        sendCmd(CLOSED_CONNECTION);

        mRun = false;

        SharedPreferences prefSettings = getSharedPreferences("settings", MODE_PRIVATE);
        Editor settingsEd = prefSettings.edit();
        settingsEd.putInt("connected", 0);
        settingsEd.apply();

        if (mBufferOut != null) {
            mBufferOut.flush();
            mBufferOut.close();
        }

        mBufferIn = null;
        mBufferOut = null;
        mServerMessage = null;
    }
like image 583
Alex Hide Avatar asked Dec 20 '22 09:12

Alex Hide


1 Answers

When I do that, new process with service is created.

Open Process View (e.g. DDMS perspective -> Devices) and check how many services are started. I bet there will be only one.

So, I have 2 client connected from same device and same IP. Question is: How to prevent creating more services?

I suspect you need to check your connect/disconnect logic inside the service, because Android allows only one instance of a service to be started. When service is started onCreate() gets called. All following startService() commands come into onStartCommand() method of the service. Just put a break point into your service onCreate() and onStartCommand() and see what happens there.

like image 166
sergej shafarenka Avatar answered Jan 11 '23 03:01

sergej shafarenka