Thursday, 3 September 2015

android open another app by intent


A app will open B app



the code of A app for open B app

Intent emailIntent = new Intent("com.marksandspencer.guardianangel.main");
//emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
//emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); 
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(emailIntent, 0);
boolean isIntentSafe = activities.size() > 0;

if (isIntentSafe) {
    startActivity(emailIntent);
}



this is the AndroidManifest.xml file of B app:



<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>


    <intent-filter>
        <action android:name="com.marksandspencer.guardianangel.main"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <!--<data android:mimeType="text/plain" />-->
    </intent-filter>

</activity>



Note:

If B app need a text input, you should open all Green highlighted codes.

If B app doesn't need a text input, you should not open both Green highlighted codes above, otherwise the system will think this is a different intent.


If not intent matches, you will see no error and logs.



















No comments:

Post a comment