Alex Hide
Alex Hide

Reputation: 545

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;
    }

Upvotes: 5

Views: 9171

Answers (4)

Veeresh Charantimath
Veeresh Charantimath

Reputation: 4719

android:process should be used with caution

(Quote from the link below)

A little-known and seemingly undocumented behaviour of Android is that each process of an application has is own Application instance. 

So, if you have a service which runs in a different process, calling startService() will init the Appplication class for your app.

More info - Starting Service in Android calls Applications onCreate

Upvotes: 3

Alex Hide
Alex Hide

Reputation: 545

Ok, I figured it out: When I swipe my application from recent apps, both processes (main and service) closed, then serivce restarted. I solved it by adding startForeground(R.string.app_name, new Notification()); in onCreate of my service (Can a service be killed by a task killer). Thanks all :)

Upvotes: 3

sergej shafarenka
sergej shafarenka

Reputation: 20406

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.

Upvotes: 5

nkz
nkz

Reputation: 144

When you start a service in Android, its not a separate process. From the Android Documentation:

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

I assume that you are trying to create a NON UI application that just runs the service. Please refer the official documentation that gives a very clear understanding about the service lifecycle and related concepts. It has a few sample implementations worth looking..

http://developer.android.com/reference/android/app/Service.html

Upvotes: 0

Related Questions