2012-11-02 29 views
2

理想的情況下,打開默認瀏覽器,然後導航到谷歌地圖(臺北101)不處理的特異性目的字符,你可以簡單地執行:monkeyrunner正確而構建的shell命令

startActivity(action='android.intent.action.VIEW', data='http://maps.google.com/?q=25.033611,121.565000&z=19') 

然而,聲明多恩斯(總是)工作。跟蹤monkeyrunner的源代碼之後:

這裏是一個片段,它示出了在內部簡單地monkeyrunner級聯參數字面。請關注#388和#411

383 public void startActivity(String uri, String action, String data, String mimetype, 
384   Collection<String> categories, Map<String, Object> extras, String component, 
385   int flags) { 
386  List<String> intentArgs = buildIntentArgString(uri, action, data, mimetype, categories, 
387    extras, component, flags); 
388  shell(Lists.asList("am", "start", 
389    intentArgs.toArray(ZERO_LENGTH_STRING_ARRAY)).toArray(ZERO_LENGTH_STRING_ARRAY)); 
390 } 
... 
406 private List<String> buildIntentArgString(String uri, String action, String data, String mimetype, 
407   Collection<String> categories, Map<String, Object> extras, String component, 
408   int flags) { 
409  List<String> parts = Lists.newArrayList(); 
410 
411  // from adb docs: 
412  //<INTENT> specifications include these flags: 
413  // [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>] 
414  // [-c <CATEGORY> [-c <CATEGORY>] ...] 
415  // [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...] 
416  // [--esn <EXTRA_KEY> ...] 
417  // [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...] 
418  // [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...] 
419  // [-n <COMPONENT>] [-f <FLAGS>] 
420  // [<URI>] 
421 
422  if (!isNullOrEmpty(action)) { 
423   parts.add("-a"); 
424   parts.add(action); 
425  } 
426 
427  if (!isNullOrEmpty(data)) { 
428   parts.add("-d"); 
429   parts.add(data); 
430  } 
... 
479  return parts; 
480 } 

對於這種情況,將執行以下shell命令。

$ am start -a android.intent.action.VIEW -d http://maps.google.com/?q=25.033611,121.565000&z=19 
$ Starting: Intent { act=android.intent.action.VIEW dat=http://maps.google.com/?q=25.033611,121.565000 } 

[1] Done     am start -a android.intent.action.VIEW -d http://maps.google.com/?q=25.033611,121.565000 

您可能會發現根本原因是&符號(&)。它在shell環境中特別解釋,即在後臺執行先前的命令。

爲了避免這種誤解,我們可以通過給它加前綴\來避開這個特殊字符。

$ am start -a android.intent.action.VIEW -d http://maps.google.com/?q=25.033611,121.565000\&z=19 
Starting: Intent { act=android.intent.action.VIEW dat=http://maps.google.com/?q=25.033611,121.565000&z=19 } 

因此,在monkeyrunner,你應該將它們傳遞到startActivity(甚至其他MonkeyDevice方法)之前逃脫的參數值,來規避這個問題。

startActivity(action='android.intent.action.VIEW', data=r'http://maps.google.com/?q=25.033611,121.565000\&z=19') 

最後,它的作品!不過,我認爲monkeyrunner作爲一個友好的API,應該在內部進行轉義。你怎麼想?

回答

1

是的,你是絕對正確的。我發現這規避了其他滋擾的方式是使用

device.shell('am start ...') 

其中至少你知道會發生什麼。

+0

是的。但是,這似乎是一種迴避立即使用shell命令的解決方法。應該有別人遇到這個問題。你知道我可以正式提交票嗎? –

+0

http://source.android.com/source/life-of-a-bug.html –