开发者

send debug commands to app running in emulator

To make development easier, I want to be able to send textual commands 开发者_Go百科to my android app (especially prior to having prior the UI completely fleshed out). Presumably in the emulator, but it would be great if it also worked on a device connected via USB. Of course, I could add an editText control to my app and type the commands in there, but would prefer not have deal with debug things cluttering up the UI and all the complications of that. I also would like to be able to paste text from computer clipboard, which sorta rules out EditText controls.

Is there any way to do this, say via any of the android-sdk/tools/ stuff? I would implement the command processor java class/method, but need some way to be able to actually feed it a string. I suppose I could do something that talks to the emulator via the file system or something, but would prefer not have to spend a lot of time doing this if there is something already available.


If it were me I'd just have the application listen on an unprivileged TCP port for connections from telnet or netcat (nc) run on the host passed into the emulator by setting up an adb port forward, or run nc from within the emulator under the adb shell. But I say that as someone who spent a few years building test equipment with TCP remote control channels.

Actually, if you want to use nc from the adb shell, you could use a unix domain socket instead of tcp, which may simplify some of the pesky reconnect issues with TCP.

(NickT's SMS idea is an interesting one, not sure which would prove better)


One way of feeding a string to the emulator would be to implement a BroadcastReceiver for SMS messages. Then from a DOS box you could 'telnet localhost 5554' (or whatever number your emulator starts up as) Then you can use the emulator command sms send 1234 yourString. (1234 is just a dummy for the 'sending' phone number.

You would have to parse the string inside the receiver to make it alter different class member variables etc or whatever.

It's a pretty clunky method but it wouldn't impact your UI and it would only need a dozen or so lines of code for the receiver.

I just stuck this bit of code into onCreate and set a breakpoint to test the principle

   rcvIncoming = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          String message = "";
          Bundle data = intent.getExtras();
          if (data != null) {
              Object pdus[] = (Object[]) data.get("pdus");
              String sender = null;
              for (Object pdu : pdus) {
                 SmsMessage part = SmsMessage.createFromPdu((byte[]) pdu);
                 message += part.getDisplayMessageBody();
                 if (sender == null) {
                 sender = part.getDisplayOriginatingAddress();
              }
           }
         }
         String test = message;// breakpoint here to test
      }
   };
   registerReceiver(rcvIncoming, new IntentFilter(
                "android.provider.Telephony.SMS_RECEIVED"));

you'll need this in the manifest too:

  <uses-permission android:name="android.permission.RECEIVE_SMS" /> 

.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜