Android 自定义方案无效

Android custom scheme not working

Manifest.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.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)

我不知道为什么 运行... 请帮助我...

出于某种原因,似乎 Android 不允许您在 intent 过滤器声明中只有 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);

现在应该可以了...

编辑:这就是为什么它根据 Google 的:

使用 DEFAULT

CATEGORY_DEFAULT

Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data. Note that this is normally -not- set in the Intent when initiating an action -- it is for use in intent filters specified in packages.

此致!