我有一个 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/