【问题标题】:How to start and App Chooser如何开始和应用程序选择器
【发布时间】:2014-02-15 17:19:36
【问题描述】:

此应用程序的任务是隐式激活一个单独的应用程序来查看 URL,“http://www.google.com”。所以,App Chooser 应该会出现,让我在至少两个浏览器之间进行选择:默认的一个,它将显示 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 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>

【问题讨论】:

  • 这是来自 coursera 上的马里兰 Android 课程第一部分...

标签: android


【解决方案1】:

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

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

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

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

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

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

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

【讨论】:

  • +1 表示“似乎是亚当·波特的第三周 Android 手持系统移动应用程序编程作业”:)
【解决方案2】:

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

这个

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

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

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

这是在默认浏览器中完成的:

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

【讨论】:

  • 非常感谢!由于疲倦,我还没有认出这种愚蠢的错误。但是,即使在修复之后,我的程序也不起作用。
  • 如果您包含类别,则所有匹配意图过滤器的意图必须设置此类别。您的代码未设置必需的类别,因此您的意图与您的过滤器不匹配。阅读文档中意图过滤器的工作原理
  • "只有当 Intent 可以通过您的 Intent 过滤器之一时,系统才会向您的应用组件传递隐式 Intent。" - developer.android.com/intl/ru/guide/components/… 据我了解,如果应用程序通过和操作意图过滤器,那么它将出现在应用程序选择器中。但是我删除了类别字段,我查看了文档,但在清单文件中找不到问题。现在我确定问题出在该浏览器应用程序中。我已经安装了 opera mini 和 chrome,我看到了包含它们的应用程序选择器,但不是那个应用程序。
  • 谢谢!我只是添加了默认类别,它现在可以工作了。
  • 此处解释:developer.android.com/guide/topics/manifest/…“为了接收隐式意图,您必须在意图过滤器中包含 CATEGORY_DEFAULT 类别。方法 startActivity() 和 startActivityForResult() 将所有意图视为声明了CATEGORY_DEFAULT。如果您未在您的意图过滤器中声明它,则没有隐式意图将解析为您的活动。"
【解决方案3】:

多动作应用选择器的简单实现:

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

【讨论】:

    【解决方案4】:

    这是我的做法:

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

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

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

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

    希望这会有所帮助..:)

    【讨论】:

    • 我不明白为什么对上面的答案投反对票。它非常好,并且可以根据每个问题的要求运行良好。如果有人投反对票,应该在评论框中说明原因。
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2011-09-02
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多