Android - service binding

Hey. It seems I can not associate the operation with the service in the same package.

The action is as follows:

public class myApp extends TabActivity {
    static private String TAG = "myApp";
    private myService mService = null;
    private ServiceConnection mServiceConn = new ServiceConnection(){
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.v(TAG, "Service: " + name + " connected");
            mService = ((myService.myBinder)service).getService();
        }

        public void onServiceDisconnected(ComponentName name) {
            Log.v(TAG, "Service: " + name + " disconnected");
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        doBind();
        Log.i(TAG, "Started (UI Thread)");

        // set content
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost

        ... add some tabs here....

        tabHost.setCurrentTab(0);
    }

    private void doBind(){      
        Intent i = new Intent(this,myService.class);
        if( bindService(i, mServiceConn, 0 )){
            Log.i(TAG, "Service bound");
        } else {
            Log.e(TAG, "Service not bound");
        }
    }

}

Then the service:

public class myService extends Service {
    private String TAG = "myService";

    private boolean mRunning = false;

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        Log.i(TAG,"Service start");

        mRunning = true;

        Log.d(TAG,"Finished onStartCommand");
        return START_STICKY;
    }

    /*
     * Called on service stop
     */
    @Override
    public void onDestroy(){
        Log.i(TAG,"onDestroy");
        mRunning = false;
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    boolean isRunning() {
        return mRunning;
    }

    /*
     * class for binding
     */
    private final IBinder mBinder = new myBinder();
    public class myBinder extends Binder {
        myService getService() {
            return myService.this;
        }
    }
}

bindService returns true, but onServiceConnection is never called (mService is always null, so I cannot do something like mService.isRunning ())

The manifest entry for the service is simple:

<service android:name=".myService"></service>

I copied the code directly from the Android developers site, but I must have missed something.

+3
source share
2 answers

This is a known problem that actions launched as subactivity in tabactivity (works in tabhost) cannot communicate with services even in one package. there is a "hacker" solution to the problem. if you call:

 getApplicationContext().bindService(Intent, connection, flags);

and not just:

 bindService(Intent, connection, flags);

. , : http://code.google.com/p/android/issues/detail?id=2483

+25

. startService(bindIntent)

:

Intent bindIntent = new Intent(this,ServiceTask.class);
if (ServiceTools.isServiceRunning() == false){
    Log.d(Global.TAG,"-->service will be started.");
    startService(bindIntent);
}else{
    Log.d(Global.TAG,"-->service already is running");
}
boolean bound = bindService(bindIntent,mConnection,0);

BIND_AUTO_CREATE 0.

ServiceTools, , . , , onServiceConnected, , onCreate()/doBind() .

public class ServiceTools {
    public static boolean isServiceRunning(){
        final ActivityManager activityManager = (ActivityManager)Global.gContext.getSystemService(Global.gContext.ACTIVITY_SERVICE);
        final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        boolean isServiceFound = false;

        for (int i = 0; i < services.size(); i++) {
            //Log.d(Global.TAG, "Service" + i + " :" + services.get(i).service);
            //Log.d(Global.TAG, "Service" + i + " package name : " + services.get(i).service.getPackageName());
            //Log.d(Global.TAG, "Service" + i + " class name : " + services.get(i).service.getClassName());

            if ("com.atClass.lmt".equals(services.get(i).service.getPackageName())){
                if ("com.atClass.lmt.ServiceTask".equals(services.get(i).service.getClassName())){
                    isServiceFound = true;
                }
            }
        }
        return isServiceFound;
     }
}
+3

Source: https://habr.com/ru/post/1794959/


All Articles