1

I want to develop two apps. I need an app able to start and stop a second app. To do this I set up a messenger between these two apps using a service. The service is sticked to the second app.

I initialized the intent like this in app1:

protected void onCreate()
{
    super.onCreate();
    Intent mIntent = new Intent();
    mIntent.setAction("com.package.service");
    bindService(mIntent, mServiceConnection, BIND_AUTO_CREATE);

} 
private ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName arg0) {

        mIsBinded=false;
        mServiceConnection=null;
    }

    @Override
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {

        mIsBinded=true;
        mMessenger = new Messenger(arg1);
    }
};

And I send a message to Close the app2.

In app2 I have a service like this:

public class RemoteServiceClient extends Service
{
static final int STOP = 0;
static final int CONTINUE = 1;
private static final String TAG ="service" ;

class MyHandler extends Handler
{

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        switch(msg.what)
        {
            case STOP:
                sendBroadcast(new Intent("stop"));
                Log.d(TAG,"stopapp");
                break;
            case CONTINUE:
                break;
        }
    }
}
Messenger mMessenger = new Messenger(new MyHandler());
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mMessenger.getBinder();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    return Service.START_STICKY;
}

}

and I also put in manifest inside application:

<service android:name="com.package.app.service"
        android:process=":exported">
        <intent-filter>
            <action android:name="com.package.service" />
        </intent-filter>
    </service>

In activity of app2 I put a braodcast receiver that when the message STOP is delivered the app calls finish() .

The problem I have is that the first time the messanger delivers the message and reboot the app2. Once app2 is reboot the app1 cannot be able to stop app2 again. I get this error:

05-04 15:47:28.168 16741-16755/com.package.app1 W/System.err: android.os.DeadObjectException
05-04 15:47:28.178 16741-16755/com.package.app1 W/System.err:     at android.os.BinderProxy.transact(Native Method)
5
  • if you want to send a "kill yourself" message to some other service use startService instead of binding to a service that will be killed right away
    – pskink
    May 4, 2016 at 14:33
  • yes but my task is to send a kill yourself message to another app: app1 has to start app2 , kill it, do stuff and restart app2.
    – Daniele
    May 4, 2016 at 14:36
  • yes i know but there is really no difference
    – pskink
    May 4, 2016 at 14:37
  • If there is an easier way I would to know it :) How I can use startService?
    – Daniele
    May 4, 2016 at 14:38
  • @Daniele i really need to something like this i need 2 apps communicate each other via Messenger. I have question can you check? stackoverflow.com/questions/64478498/…
    – saulyasar
    Oct 22, 2020 at 13:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.