Android 使用自定义操作启动活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10921451/
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:29:29  来源:igfitidea点击:

Start Activity Using Custom Action

androidandroid-intentandroid-activity

提问by Jason Crosby

I am looking to start an activity in my app using a custom action. I have found a few answers but everything I try it throws java.lang.RuntimeExceptionsaying No Activity found to handle Intent { act=com.example.foo.bar.YOUR_ACTION }.

我希望使用自定义操作在我的应用程序中启动一项活动。我找到了一些答案,但我尝试的所有内容都抛出java.lang.RuntimeException说 No Activity found to handle Intent { act=com.example.foo.bar.YOUR_ACTION }。

This is the activity in my manifest file:

这是我的清单文件中的活动:

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
    </intent-filter>
</activity>

And this is how I'm starting the activity:

这就是我开始活动的方式:

Intent intent = new Intent("com.example.foo.bar.YOUR_ACTION");
startActivity(intent);

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by FoamyGuy

I think you are creating your intent wrong. Try like this:

我认为你在创造你的意图是错误的。像这样尝试:

String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";

//Intent i = new Intent(this, FeedBackActivity.class);  // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);

startActivity(i);

回答by Maks

I think what you need is to add a default category to your intent-filter, eg.

我认为您需要的是为您的意图过滤器添加一个默认类别,例如。

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

see this answerfor more info.

有关更多信息,请参阅此答案

回答by Abhiroop Nandi Ray

Just add and intent-filter category as Default.

只需添加和意图过滤器类别作为默认值。

Implicit intent works perfectly and in many cases its better to use a implicit intent with Intent-action to call a service/Activity than using class-name.

隐式意图完美地工作,在许多情况下,使用带有 Intent-action 的隐式意图来调用服务/活动比使用类名更好。

Before startActivty() / startService()with proper context you cane use this method 'queryIntentActivities(Intent intent, int flags)'from package manager class.

startActivty() / startService()使用适当的上下文之前,您可以使用'queryIntentActivities(Intent intent, int flags)'包管理器类中的此方法。

It helps the ActivityManager (responsible for launching activities) to check whether the Android system is getting any match with you Intent.

它帮助 ActivityManager(负责启动 Activity)检查 Android 系统是否与您的 Intent 匹配。

If it doesn't it returns a list size 0 or else >0.

如果不是,则返回列表大小 0 或 >0

By this you can also check if your app is getting the call,and in this case even if your app is not installed / has got some problem, it will not crash but will throw a warning in Log. Users will face no big trouble apart from app not being launched.

通过这种方式,您还可以检查您的应用程序是否收到呼叫,在这种情况下,即使您的应用程序未安装/出现问题,它也不会崩溃,但会在日志中发出警告。除了没有启动应用程序外,用户不会遇到大麻烦。

(users will never forgive you if tour app crashes).

(如果旅游应用程序崩溃,用户永远不会原谅你)。

Hope this will help !!! Happy Coding. :)

希望这会有所帮助!!!快乐编码。:)