android - 检查 Intent 是 Activity 或服务还是无

标签 android android-intent android-activity android-service

我有一个 Intent 的 ArrayList,如何检查给定位置的 Intent 是否包含 Activity 、服务或什么都不包含?

Intent intent = intents.get(id);
context.startActivityForResult(intent,1);

您将如何检查索引 id 处的 Intent 是否可以通过 startActivityForResult 正确传递,它强调 Activity 类。

最佳答案

这不是一个优雅的解决方案,但您可以尝试像这样获取 Intent 的类名:

Intent it = new Intent(ctx, AnotherActivity.class);
it.getComponent().getClassName() // output com.package.app.MainActivity

Intent it = new Intent(ctx, JustService.class);
it.getComponent().getClassName() // output com.package.app.JustService

和使用

switch (...getClassName()) {
    case 'com.package.app.JustService' {
         //todo
    }
}

你可以使用 getShortClassName() 方法,它会输出没有默认包的类名:

.JustService

对于空 Intent ,我会检查是否为空。

关于android - 检查 Intent 是 Activity 或服务还是无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39110627/

相关文章:

java - Android Studio 外部 jar 错误

java - 基本 REST Web 服务无法注册

android - 将 Parcelable 对象从 fragment 传递到 Activity

相机 Intent 后Android崩溃

android - 如何将数据从 BroadcastReceiver 传递到正在启动的 Activity?

java - 布局放置不正确? (还是不行)

android - 你有没有在你的 androidmanifest.xml android 中声明这个 Activity

android - 返回应用时的静态变量null

android - MSAL Android Xamarin 卡住对话框 ​​- "Are you trying to sign in to {your_appname}"?

android - 如何通过添加搜索查询的 Intent 启动 GoogleNow?