Android 如何启动和应用选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21800948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:06:09  来源:igfitidea点击:

How to start and App Chooser

android

提问by Mabjik

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".

此应用程序的任务是隐式激活单独的应用程序以查看 URL,“http://www.google.com”。因此,应用选择器应该出现并让我在至少两个浏览器之间进行选择:默认浏览器将显示 www.google.com 站点,另一个简单的“浏览器”仅显示 URL。问题是 - 当我使用隐式活动时,应用选择器没有出现。可能我为第二个“简单浏览器”编写了错误的意图过滤器。

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>

回答by neo7

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.

看起来像 Adam Porter 的第三周Android 手持系统移动应用程序编程作业。无论如何,我希望您在 AndroidManifest.xml 中有您的解决方案,您需要再添加三个意图过滤器。

<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,

此外,如果您无法运行 AndroidUnit 测试,

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

确保您在隐式按钮调用的 ActionListener 中执行了类似的操作。

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.

Coursera 已经有一个邮件列表。请使用那个。

回答by weaknespase

Seriously, you have a GUI editor for manifest.

说真的,您有一个用于清单的 GUI 编辑器。

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:

应该放在<intent-filter>标签中,正如您在清单中已经看到的那样。像这样:

<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>

回答by Barbarian

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);

回答by Neil

You have to run lab3a_MyBrowserbefore you run lab3a_IntentsLab, so that the browser gets installed on your phone. Then it will be visible to your chooser.

您必须在运行lab3a_MyBrowser之前运行lab3a_IntentsLab,以便在手机上安装浏览器。然后它将对您的选择器可见。

回答by mike20132013

Here's how I am doing:

这是我的做法:

Step 1: First in the strings,

第 1 步:首先在字符串中,

<string name="webklink">&lt;a href="http://www.google.com">SomeLink&lt;/a></string>

Step 2: My Layout:

第 2 步:我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/webklink"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Step 3: Getting the link from the strings.

第 3 步:从字符串中获取链接。

public class OpenWeb extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webopen);

        TextView someLink = (TextView)findViewById(R.id.textView1);
        if(someLink!=null){

            someLink.setMovementMethod(LinkMovementMethod.getInstance());
            someLink.setText(Html.fromHtml(getResources().getString(R.string.webklink)));
        }

    }
}

Hope this helps..:)

希望这可以帮助..:)