dimanche 19 juin 2016

Handling intents serially between 2 apps

I got 2 applications. A user of the first application can open a dialog in which he got 2 options of actions he could do. This is done like this:

public void sendSms(String number)
{
    Intent myIntent = new Intent(Intent.ACTION_SENDTO);
    myIntent.setData(Uri.parse("smsto:" + number);
    startActivity(myIntent);
}

public void call(String number)
{
    Intent myIntent = new Intent(Intent.ACTION_CALL);
    myIntent.setData(Uri.parse("tel:" + number);
    startActivity(myIntent);
}

The second app has intent filter and it identifies this action:

<activity android:name="com.my.path.MyActivity"
          android:label="@string/app_name"
          android:configChanges="orientation"
          android:screenOrientation="portrait"
          android:launchMode="singleTask" >


 <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

        <action android:name="android.intent.action.CALL" />
        <action android:name="android.intent.action.CALL_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />

    </intent-filter>

    <intent-filter>

        <action android:name="android.intent.action.SENDTO" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="smsto" />
    </intent-filter>

    <intent-filter android:priority="100" >

        <action android:name="android.intent.action.DIAL" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />

    </intent-filter>

    <intent-filter>

        <action android:name="android.intent.action.CALL_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />

    </intent-filter>

    <intent-filter>

        <action android:name="android.intent.action.CALL_PRIVILEGED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />

    </intent-filter>

    <intent-filter>

        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.DIAL" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="tel" />

    </intent-filter>    
</activity>

The second application handles the intents like this:

protected void onNewIntent(Intent inten)
{
    super.onNewIntent(intent);
    String action = intent.getAction();
    if(action.equals(Intent.ACTION_CALL))
    {
        *here make a fragment transaction to the fragment which handles calls *
    }
    else if(action.equals(Intent.ACTION_SENDTO))
    {
        *here make a fragment transaction to the fragment which handles sms*

    }
}

When the user opens the first application and chooses one of the actions, it works fine and when he presses the back button he goes back to the first application which is also what I want. When he goes back to the first application, the dialog is still open like it was when he pressed the action.

The problem is that if the user press the call action again to call the second app, the second app is coming to the foreground, but it doesn't seem that the onNewIntent is called again, thus the fragment transaction does not happen.

I then noticed that for the second time the onCreate method is invoked so I tried handling the intent there. It did work for the SENDTO action but not for the CALL action. For the CALL action, the onCreate intent handling is being called but for some reason the fragment transaction does not work there (while like I said, the fragment transaction for SENDTO action does work).

Why isn't the onNewIntent method called when trying to do the same action again? And why won't the transaction work for the call action?

UPDATE: the fragment transaction now do work in the onCreate method. However I'd still prefer to handle everything in onNewIntent

Aucun commentaire:

Enregistrer un commentaire