这是根据toUri documentation:"The URI contains the Intent's data as the base URI, with an additional fragment describing the action, categories, type, flags, package, component, and extras."
//substitute your own intent here
Intent intent = new Intent();
intent.putExtra("first", "hello");
intent.putExtra("second", 1);
intent.putExtra("third", true);
intent.putExtra("fourth", 1.01);
// convert the set to a string array
String[] anArray = {};
Set<String> extras1 = (Set<String>) intent.getExtras().keySet();
String[] extras = (String[]) extras1.toArray(anArray);
// an arraylist to hold all of the strings
// rather than putting strings in here, you could display them
ArrayList<String> endResult = new ArrayList<String>();
for (int i=0; i<extras.length; i++) {
//try using as a String
String aString = intent.getStringExtra(extras[i]);
// is a string, because the default return value for a non-string is null
if (aString != null) {
endResult.add(extras[i] + " : " + aString);
}
// not a string
else {
// try the next data type, int
int anInt = intent.getIntExtra(extras[i], 0);
// is the default value signifying that either it is not an int or that it happens to be 0
if (anInt == 0) {
// is an int value that happens to be 0, the same as the default value
if (intent.getIntExtra(extras[i], 1) != 1) {
endResult.add(extras[i] + " : " + Integer.toString(anInt));
}
// not an int value
// try double (also works for float)
else {
double aDouble = intent.getDoubleExtra(extras[i], 0.0);
// is the same as the default value, but does not necessarily mean that it is not double
if (aDouble == 0.0) {
// just happens that it was 0.0 and is a double
if (intent.getDoubleExtra(extras[i], 1.0) != 1.0) {
endResult.add(extras[i] + " : " + Double.toString(aDouble));
}
// keep looking...
else {
// lastly check for boolean
boolean aBool = intent.getBooleanExtra(extras[i], false);
// same as default, but not necessarily not a bool (still could be a bool)
if (aBool == false) {
// it is a bool!
if (intent.getBooleanExtra(extras[i], true) != true) {
endResult.add(extras[i] + " : " + Boolean.toString(aBool));
}
else {
//well, the road ends here unless you want to add some more data types
}
}
// it is a bool
else {
endResult.add(extras[i] + " : " + Boolean.toString(aBool));
}
}
}
// is a double
else {
endResult.add(extras[i] + " : " + Double.toString(aDouble));
}
}
}
// is an int value
else {
endResult.add(extras[i] + " : " + Integer.toString(anInt));
}
}
}
// to display at the end
for (int i=0; i<endResult.size(); i++) {
Toast.makeText(this, endResult.get(i), Toast.LENGTH_SHORT).show();
}
11 回答
这是我用来获取有关未记录(第三方)意图的信息:
(确保在循环之前检查
bundle
是否为空)这就是我如何定义实用程序方法来转储Intent的所有额外内容 .
你可以在一行代码中完成它:
它输出如下:
“#Intent; action = android.intent.action.MAIN; category = android.intent.category.LAUNCHER; launchFlags = 0x10a00000; component = com.mydomain.myapp / .StartActivity; sourceBounds = 12%20870%20276%201167; l.profile=0; 结束”
在这个字符串的末尾(我加粗的部分)你可以找到额外的列表(在这个例子中只有一个额外的) .
这是根据toUri documentation:"The URI contains the Intent's data as the base URI, with an additional fragment describing the action, categories, type, flags, package, component, and extras."
Bundle的get(String key)方法返回一个Object . 最好的办法是旋转每个键上调用get(String)的键集,并使用Object上的toString()输出它们 . 这对于原语最有效,但是你可能会遇到没有实现toString()的对象的问题 .
您可以使用
for (String key : keys) { Object o = get(key);
返回一个Object,在其上调用getClass().getName()
以获取该类型,然后执行一组if name.equals("String")类型的事情以确定您应该实际调用哪个方法,以获取值?我想要一种方法将意图的内容输出到日志,并且能够轻松读取它,所以这里's what I came up with. I' ve创建了一个
LogUtil
类,然后创建了dumpIntent()
方法@Pratik,并对其进行了一些修改 . 这就是它的全貌:希望这有助于某人!
我注意到在Android源代码中,几乎所有操作都会强制Bundle取消其数据的取消 . 所以如果(像我一样)你需要经常这样做以进行调试,下面的输入速度非常快:
对不起,如果这太冗长或太晚,但这是我找到完成工作的唯一方法 . 最复杂的因素是java没有通过引用函数传递,因此get --- Extra方法需要默认返回,并且不能修改布尔值来判断是否偶然返回默认值,或者因为结果不利 . 为此,让方法引发异常比让它返回默认值更好 .
我在这里找到了我的信息:Android Intent Documentation .
Set Documentation
如果为了调试你想要的只是一个字符串(由OP暗示但没有明确说明),只需在额外的
Bundle
上使用toString
:它返回一个字符串,如:
文档:Bundle.toString()(遗憾的是默认
Object.toString()
javadoc,因此在这里毫无用处 . )