1<? XML version = "1.0" encoding = "UTF-8"?> 2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" 3 Android: layout_width = "fill_parent" 4 Android: layout_height = "fill_parent" 5 Android: orientation = "vertical"> 6 <textview 7 Android: layout_width = "fill_parent" 8 Android: layout_height = "wrap_content" 9 Android: TEXT = "@ string/Hello"/> 10 <edittext11 Android: Id = "@ + ID/et_path" 12 Android: layout_width = "match_parent" 13 Android: layout_height = "wrap_content" 14 Android: text = "/sdcard/queryaddress.apk"/> 15 <button16 Android: Id = "@ + ID/button1" 17 Android: layout_width = "wrap_content" 18 Android: layout_height = "wrap_content" 19 Android: onclick = "Install" 20 Android: text = "Install"/> 21 </linearlayout>
1 import java.io.File; 2 import android.app.Activity; 3 import android.content.Intent; 4 import android.net.Uri; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 9 public class DemoActivity extends Activity {10 private EditText et_path;11 12 @Override13 public void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.main);16 et_path = (EditText) this.findViewById(R.id.et_path);17 }18 19 public void install(View view) {20 String path = et_path.getText().toString().trim();21 22 Intent intent = new Intent();23 intent.setAction(Intent.ACTION_VIEW);24 // intent.setData(Uri.fromFile(new File(path)));25 // intent.setType("application/vnd.android.package-archive");26 intent.setDataAndType(Uri.fromFile(new File(path)),27 "application/vnd.android.package-archive");28 startActivity(intent);29 }30 }
[Tool] how to install an APK file through code