Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start and App Chooser

Tags:

android

The task of this application is to implicitly activate a separate application to view the URL, “http:// www.google.com”. So, App Chooser should appear and let me choose between at least two browsers: the default one, which will show me the www.google.com site, and another simple "browser", which just shows me the url. The problem is - App chooser doesn't appear, when i use implicit activity. Probably I wrote wrong intent filters for second "simple browser".

private void startImplicitActivation() {

    Log.i(TAG, "Entered startImplicitActivation()");

    // TODO - Create a base intent for viewing a URL 
    // (HINT:  second parameter uses parse() from the Uri class)
    Uri adress = Uri.parse(URL);
    Intent intentUrlView = new Intent(Intent.ACTION_VIEW, adress);

    // TODO - Create a chooser intent, for choosing which Activity
    // will carry out the baseIntent. Store the Intent in the 
    // chooserIntent variable below. HINT: using the Intent class' 
    // createChooser())
    Intent chooserIntent = Intent.createChooser(intentUrlView, CHOOSER_TEXT);
    //chooserIntent = null;

    Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
    // TODO - Start the chooser Activity, using the chooser intent
    if (intentUrlView.resolveActivity(getPackageManager()) != null) {
        startActivity(chooserIntent);
    }
}

MANIFEST

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="course.labs.intentslab.mybrowser"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyBrowserActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <!-- TODO - Add necessary intent filter information so that this
                            Activity will accept Intents with the 
                            action "android.intent.action.VIEW" and with an "http" 
                            schemed URL -->
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
        <category android:name="android.intent.category.BROWSABLE" />
        </activity>
    </application>

like image 542
Mabjik Avatar asked Feb 15 '14 17:02

Mabjik


3 Answers

Seems like the Adam Porter's Third Week Assignment of Programming mobile Application for Android HandHeld systems. Anyway, I hope you have your solution inside the AndroidManifest.xml you would need to add three more intent filters.

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

Further, if you were not able to run the AndroidUnit tests,

make sure you have done something similar to this in the ActionListener of the implicit button call.

Intent baseIntent = new Intent(Intent.ACTION_VIEW);
        baseIntent.setData(Uri.parse(URL));

Intent chooserIntent = Intent.createChooser(baseIntent, "Select Application");

Coursera already has a mailing list. Please use that.

like image 58
neo7 Avatar answered Oct 05 '22 21:10

neo7


Seriously, you have a GUI editor for manifest.

This

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

should placed in <intent-filter> tag, as you already can see in your manifest. Like this:

<activity
    android:name=".MyBrowserActivity"
    android:label="@string/app_name" >

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

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

        <!-- TODO - Add necessary intent filter information so that this
                        Activity will accept Intents with the 
                        action "android.intent.action.VIEW" and with an "http" 
                        schemed URL -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>

</activity>

That is how it done in default browser:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="about" />
            <data android:scheme="javascript" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="inline" />
            <data android:scheme="file" />
            <data android:mimeType="text/html" />
            <data android:mimeType="text/plain" />
            <data android:mimeType="application/xhtml+xml" />
            <data android:mimeType="application/vnd.wap.xhtml+xml" />
        </intent-filter>
like image 32
weaknespase Avatar answered Oct 05 '22 23:10

weaknespase


Easy implementation of multiple actions app chooser:

Intent phoneCall = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", contactNumber, null));
// Intent sms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + contactNumber));
Intent whatsapp = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + contactNumber));
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, Calendar.getInstance().getTimeInMillis());
Intent calendarIntent = new Intent(Intent.ACTION_VIEW).setData(builder.build());

Intent chooser = Intent.createChooser(whatsapp, "chooser??"); // default action
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{phoneCall, calendarIntent}); // additional actions
startActivity(chooser);
like image 34
Barbarian Avatar answered Oct 05 '22 22:10

Barbarian