[英]How to use adb shell controll android app?
我正在嘗試開發一個Android演示程序。我想在adb shell中使用命令來使演示程序做我想做的事。我可能需要一個shell文件。
我想要這樣的結果。
root@device:/ # <shell> log "hello world"
然后在logcat上我的應用程序控制台“ hello world”。
但我不知道如何連接它們。
盡管您的應用必須注冊適當的BroadcastListener
,但是您可以通過外殼將Intent發送到您的應用。 這是一個簡單的例子:
public class MyActivity extends Activity {
private static final String TAG = MyActivity.class.getSimpleName();
public static final String INTENT_FILTER = "com.example.myapp.TEST";
public static final String EXTRA_KEY = "new_text";
private class MyIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final String newText = intent.getExtras().getString(EXTRA_KEY);
Log.d(TAG, "Received new text: " + newText);
}
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(new MyIntentReceiver(), new IntentFilter(INTENT_FILTER));
}
}
然后,您可以在shell中執行以下操作:
am broadcast -a com.example.myapp.TEST -e "new_text" "New text"
您將看到您的應用如何接收和記錄它。
只是為了澄清一下:您要將命令從adb shell(本地android shell)發送到您的應用? 如果沒有幾行代碼,這是不可能的,並且可能需要植根設備。
您最好將一個UI添加到可以調用事件的應用程序中。
編輯:正如m0skit0指出的那樣,可以通過命令行發送意圖。 這絕對是最干凈的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.