Android service in a separate process

I start my application with one activity and call startService(new Intent(this, TCPClient.class));

in onStart

. Also I run a thread to onCreate()

services that establishes a TCP connection to my server. The service runs in a separate process. It works well until I restart the application (I don't stop the service when the application is closed). When I do this, I get 1 more connection from the same IP. So I have 2 clients connected to the same IP address. Q: how can I prevent additional services from being created?

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

      

+4


source to share


4 answers


Ok, I figured it out: when I scroll my app from recent apps, both processes (main and service) are closed and then restart serivce. I solved this by adding startForeground(R.string.app_name, new Notification());

to onCreate

my service ( Can a service be killed by a task killer ). Thanks everyone :)



+3


source


When I do this, a new process with the service is created.

Open a process view (for example, DDMS perspective -> Devices) and check how many services are running. I bet there will only be one.



So I have 2 clients connected to the same IP address. Q: how can I prevent additional services from being created?

I suspect that you need to check the connect / disconnect logic on the service, since Android only allows one instance of the service to start. When the service starts, it onCreate()

is called. All of the following commands startService()

go into a onStartCommand()

service method . Just put a breakpoint in your life onCreate()

and onStartCommand()

and see what happens there.

+5


source


When you start a service in Android, it is not a separate process. From android documentation:

A lot of the confusion about the Service class actually revolves around the fact that it isn't:

  • The service is not a separate process. A service object itself does not mean that it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • Service is not a thread. This does not mean that you have to do the work of shutting down the main thread (to avoid application errors not responding).

I am assuming you are trying to create a NON UI application that just starts the service. Please refer to the official documentation which gives a very clear understanding of the service lifecycle and related concepts. It has several implementation examples worth looking at.

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

0


source


Android: the process should be used with care

(Quoted from the link below)

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

So, if you have a service running in a different process, calling startService () will initiate the Appplication class for your application.

More - Launching the service in Android calls

0


source







All Articles