Android从EditText打开另一个应用程序

时间:2018-03-28 10:52:06

标签: android

我们可以通过执行Edittext视图给出的命令,通过此代码打开其他应用程序。如果是,那么如何做?

package com.example.honey.shell;

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
 TextView op;
 EditText ip;
 Button exec;
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  op = (TextView) findViewById(R.id.textView);
  ip = (EditText) findViewById(R.id.editText);
  exec = (Button) findViewById(R.id.button);
 }

 public void execute(View view) {
  String input = ip.getText().toString();
  StringBuffer output = new StringBuffer();
  Process p;
  try {
   p = Runtime.getRuntime().exec(input);
   p.waitFor();
   BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
   String line = "";
   while ((line = reader.readLine()) != null) {
    output.append(line + "\n");
   }
  } catch (Exception e) {}
  op.setText(output.toString());
 }

}

实际上我希望通过执行命令从我的应用程序中打开一些其他应用程序,但它不起作用,因为我尝试了诸如cd之类的命令,但它不起作用只有ls命令正在工作...

1 个答案:

答案 0 :(得分:0)

从另一个应用程序打开一个应用程序,您可以使用Intents

Intent launchNewApp = getPackageManager().getLaunchIntentForPackage("com.example.name");//specify the package name of other application which you intended to launch
if (launchNewApp != null) { 
    startActivity(launchIntent);//null pointer check in case package name was not found
}