首页 > 代码库 > 【工具类】如何通过代码安装一个apk文件

【工具类】如何通过代码安装一个apk文件

 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="安装" />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 }

 

【工具类】如何通过代码安装一个apk文件