我一直在研究一个使用 Runtime.exec 并获取其输出的类,但是它不是很稳定......现在我使用这个类来获取命令“ls /sdcard/”的输出(知道我有我的清单中的正确权限)...(我将在下面编写代码)我的问题是命令的 Text 输出可以显示为 Toast ,它可以设置为 button 的文本,但不能设置为一个文本视图!当我运行我TextView.settext()
的时,TextView 根本不显示任何文字!更奇怪的是,如果我使用settext()
likeTextView.settext("Hey there , I am reset");
它可以工作并显示该文本!以下是代码:
MainActivity.class/ Button.setOnClickListener:
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String sdcard = File.separator+Environment.getExternalStorageDirectory().toString()+File.separator; //unused
String[] arr = {"ls" , "/sdcard/..ROMS/"};//unused - was for experimentation
String r = shell.sendSingleCommand("ls /sdcard/"); //"r" should now hold the output (stdin and stderr) of the command - shell is defined as Shells shell = new Shells();
B3.setText(r); //Text of the button changes to the String r
text.setText(r); //Text is blank , no text is shown at all , not even what it originally was - "text" is my TextView ... Here's my problem
Show(r); // a method that shows a Toast for a given String - shows the String r normally in a normal Toast ...
}
});
MainActivity 中大多数变量的声明:
final Button B1 = (Button) findViewById(R.id.button1);
final Button B2 = (Button) findViewById(R.id.button2);
final Button B3 = (Button) findViewById(R.id.button3);
final SharedPreferences pref = getSharedPreferences("seaskyways.testingproject",0);
final TextView text = (TextView) findViewById(R.id.testing_text_view);
final Shells shell = Shells.Shells(); //which returns new Shells()
布局/activity_main.xml:
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="52dp"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Button 3" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3" >
<TextView
android:id="@+id/testing_text_view"
android:layout_width="222dp"
android:layout_height="236dp"
android:gravity="center|top"
android:padding="10sp"
android:text="Hey there !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/abs__primary_text_holo_dark" />
</ScrollView>
</RelativeLayout>
</ScrollView>
显示():
public void Show(String a){
Toast.makeText(getApplicationContext(),a , Toast.LENGTH_SHORT).show();
}
Shells.class/ sendSingleCommand():
public String sendSingleCommand(String singlecmd) {
String a = "";
final BufferedReader in ;
final BufferedReader er;
final String r;
try {
p = Runtime.getRuntime().exec(singlecmd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
er = new BufferedReader(new InputStreamReader(p.getErrorStream()));
try{
String n;
while((n = er.readLine())!=null){
a = a + n + "\n";
}
while((n=in.readLine())!=null){
a = a +"\n"+ n;
while((n = er.readLine())!=null){
a = a +"\n"+ n;
}
}
}catch(Exception e){
e.printStackTrace();
}
p.destroy();
try {
in.close();
er.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final StringBuilder build = new StringBuilder();
build.append(a);
if(a.startsWith("null")){
build.substring(4);
}
a= build.toString();
return a;
}
AndroidManifest.xml(如果需要):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="seaskyways.testingproject"
android:versionCode="1"
android:versionName="beta" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock" >
<activity
android:name="seaskyways.testingproject.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.testingproject.MenuActivity"
android:label="@string/title_activity_menu" >
</activity>
<activity
android:name="seaskyways.testingproject.Splash"
android:label="@string/title_activity_splash" android:theme="@style/Theme.Sherlock.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.testingproject.HorizontalScrollView"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.HSV" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
如果您需要在评论中了解任何内容,请!