gpt4 book ai didi

Android 自定义方案不起作用

转载 作者:行者123 更新时间:2023-11-29 20:52:16 26 4
gpt4 key购买 nike

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

主 Activity .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)

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

最佳答案

出于某种原因,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);

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

编辑:这就是它按照谷歌的默认值工作的原因:

CATEGORY_DEFAULT

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

问候!

关于Android 自定义方案不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28816137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com