首先连上设备后adb shell进入交互模式,然后am start 包名/.launch activity 例如:am start com.example/.activity.WelcomActivity
也可以直接adb shell am start com.example/.activity.WelcomActivity
android程序执行adbshell命令(实例源码)packagenet.gimite.nativeexeimportjava.io.BufferedReaderimportjava.io.FileOutputStreamimportjava.io.IOExceptionimportjava.io.InputStreamimportjava.io.InputStreamReaderimportjava.net.HttpURLConnectionimportjava.net.MalformedURLExceptionimportjava.net.URLimportnet.gimite.nativeexe.Rimportandroid.app.Activityimportandroid.os.Bundleimportandroid.os.Handlerimportandroid.view.Viewimportandroid.view.View.OnClickListenerimportandroid.widget.*publicclassMainActivityextendsActivity{privateTextViewoutputViewprivateButtonlocalRunButtonprivateEditTextlocalPathEditprivateHandlerhandler=newHandler()privateEditTexturlEditprivateButtonremoteRunButton/**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){try{Processprocess=Runtime.getRuntime().exec(command)BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()))intreadchar[]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){try{URLurl=newURL(urlStr)HttpURLConnectionurlconn=(HttpURLConnection)url.openConnection()urlconn.setRequestMethod("GET")urlconn.setInstanceFollowRedirects(true)urlconn.connect()InputStreamin=urlconn.getInputStream()FileOutputStreamout=newFileOutputStream(localPath)intreadbyte[]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)}}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)