如何在android程序中执行adb shell命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在android程序中执行adb shell命令相关的知识,希望对你有一定的参考价值。

android程序执行adbshell命令(实例源码)packagenet.gimite.nativeexe;importjava.io.BufferedReader;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importnet.gimite.nativeexe.R;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.*;publicclassMainActivityextendsActivityprivateTextViewoutputView;privateButtonlocalRunButton;privateEditTextlocalPathEdit;privateHandlerhandler=newHandler();privateEditTexturlEdit;privateButtonremoteRunButton;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);outputView=(TextView)findViewById(R.id.outputView);localPathEdit=(EditText)findViewById(R.id.localPathEdit);localRunButton=(Button)findViewById(R.id.localRunButton);localRunButton.setOnClickListener(onLocalRunButtonClick);privateOnClickListeneronLocalRunButtonClick=newOnClickListener()publicvoidonClick(Viewv)Stringoutput=exec(localPathEdit.getText().toString());output(output);//try//////Processprocess=Runtime.getRuntime().exec(localPathEdit.getText().toString());////catch(IOExceptione)////TODOAuto-generatedcatchblock//e.printStackTrace();//;//ExecutesUNIXcommand.privateStringexec(Stringcommand)tryProcessprocess=Runtime.getRuntime().exec(command);BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()));intread;char[]buffer=newchar[4096];StringBufferoutput=newStringBuffer();while((read=reader.read(buffer))>0)output.append(buffer,0,read);reader.close();process.waitFor();returnoutput.toString();catch(IOExceptione)thrownewRuntimeException(e);catch(InterruptedExceptione)thrownewRuntimeException(e);privatevoiddownload(StringurlStr,StringlocalPath)tryURLurl=newURL(urlStr);HttpURLConnectionurlconn=(HttpURLConnection)url.openConnection();urlconn.setRequestMethod("GET");urlconn.setInstanceFollowRedirects(true);urlconn.connect();InputStreamin=urlconn.getInputStream();FileOutputStreamout=newFileOutputStream(localPath);intread;byte[]buffer=newbyte[4096];while((read=in.read(buffer))>0)out.write(buffer,0,read);out.close();in.close();urlconn.disconnect();catch(MalformedURLExceptione)thrownewRuntimeException(e);catch(IOExceptione)thrownewRuntimeException(e);privatevoidoutput(finalStringstr)Runnableproc=newRunnable()publicvoidrun()outputView.setText(str);;handler.post(proc); 参考技术A 首先确认已经配置好adb环境,检测方法为输入adb devices,如果提示不识别adb命令时需要安装好adb配置环境后,
1. adb配置好以后,执行adb root
2. (如果要对内部文件执行操作,需要在这里加一个步骤: adb amount )
3. 然后即可执行adb shell本回答被提问者采纳

怎么在Android Studio中执行adb命令

android stuidio中默认集成了命令行窗口,你可以在下方的terminal中输入指令就可以使用shell指令。 参考技术A 😊

以上是关于如何在android程序中执行adb shell命令的主要内容,如果未能解决你的问题,请参考以下文章

如何在android程序中执行adb shell命令

怎么在Android Studio中执行adb命令

如何使用 adb shell 从 Android 设备中删除内置应用程序

怎么在Android Studio中执行adb命令

Android 代码中执行adb shell命令

如何从 adb shell 运行 Android 相机应用程序?