1

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kt.myapplication" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        </activity>
        <activity
            android:name=".ViewerActivity"
            android:label="@string/title_activity_viewer" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="myapp" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.main_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("myapp://"));
                startActivity(intent);
            }
        });

    }

}

ViewerActivity.java

public class ViewerActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);
    }
}

错误日志...

03-02 09:37:59.753  21103-21103/com.example.kt.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.kt.myapplication, PID: 21103
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=myapp:// }
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
            at android.app.Activity.startActivityForResult(Activity.java:3511)
            at android.app.Activity.startActivityForResult(Activity.java:3472)
            at android.app.Activity.startActivity(Activity.java:3714)
            at android.app.Activity.startActivity(Activity.java:3682)
            at com.example.kt.myapplication.MainActivity$1.onClick(MainActivity.java:26)
            at android.view.View.performClick(View.java:4630)
            at android.view.View$PerformClick.run(View.java:19331)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

我不知道为什么不跑...请帮助我...

4

1 回答 1

2

出于某种原因,Android 似乎不允许您在意图过滤器声明中只有 Browsable 类别,请尝试以下操作:

            <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="somettt"/>
            </intent-filter>

并按预期启动它:

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("somettt://"));
        startActivity(intent);

它现在应该可以工作...

编辑:这就是为什么它与谷歌的 DEFAULT 一起工作的原因:

CATEGORY_DEFAULT

设置活动是否应该是默认操作(中心按下)对一条数据执行的选项。在对某些数据执行操作时,设置此项将对用户隐藏任何未设置的活动。请注意,这通常在启动操作时未在 Intent 中设置——它用于包中指定的意图过滤器。

问候!

于 2015-03-02T18:29:00.883 回答