Android 下载APK 安装APK 打开APK
来源:互联网 发布:使命召唤12优化补丁 编辑:程序博客网 时间:2024/04/30 01:16
今天有了一个这样的需求 :下载一个apk文件,然后当你下载完成后,按钮的文字发生改变,变成点击安装,然后安装完成之后,变成打开。
这是下载apk的方法:
/** * 后台在下面一个Apk 下载完成后返回下载好的文件 * * @param httpUrl * @return */private File downFile(final String httpUrl) {new Thread(new Runnable() {@Overridepublic void run() {try {URL url = new URL(httpUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);FileOutputStream fileOutputStream = null;InputStream inputStream;if (connection.getResponseCode() == 200) {inputStream = connection.getInputStream();if (inputStream != null) {file = getFile(httpUrl);fileOutputStream = new FileOutputStream(file);byte[] buffer = new byte[1024];int length = 0;while ((length = inputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, length);}fileOutputStream.close();fileOutputStream.flush();}inputStream.close();}System.out.println("已经下载完成");// 往handler发送一条消息 更改button的text属性Message message = handler.obtainMessage();message.what = 1;handler.sendMessage(message);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}).start();return file;}
/** * 安装APK */private void installApk() {Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");startActivity(intent);}
这是打开APK的方法:
/** * 打开已经安装好的apk */private void openApk(Context context, String url) {PackageManager manager = context.getPackageManager();// 这里的是你下载好的文件路径PackageInfo info = manager.getPackageArchiveInfo(Environment.getExternalStorageDirectory().getAbsolutePath()+ getFilePath(url), PackageManager.GET_ACTIVITIES);if (info != null) {Intent intent = manager.getLaunchIntentForPackage(info.applicationInfo.packageName);startActivity(intent);}}打开APK 这里弄了好久,之前不知道有个getLaunchIntentForPackage方法 这个方法只要你能得到这个apk的报名,然后将包名加到后面,startActivity 它就会自动自动你的APK的主界面了。相信得到一个APK的的信息这个大家都会了,这里就不说了。
下面是我的所有代码:
/** * 下载Apk 安装Apk 打开APK * * @author Administrator * */public class MainActivity extends Activity {private Button button1;private static final String URL_STRING = "http://gdown.baidu.com/data/wisegame/b7d7e4efd8199dea/tianyiyuedu_310.apk";private static int down = 0;File file;private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case 1:button1.setText("点击安装");down = 1;break;case 2:down = 2;button1.setText("打开");break;}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 下载apkif (down == 0) {downFile(URL_STRING);button1.setText("正在下载");// 安装APK} else if (down == 1) {installApk();// 打开apk} else if (down == 2) {openApk(MainActivity.this, URL_STRING);}}});}// 接收到安装完成apk的广播BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("接收到安装完成apk的广播");Message message = handler.obtainMessage();message.what = 2;handler.sendMessage(message);}};/** * 后台在下面一个Apk 下载完成后返回下载好的文件 * * @param httpUrl * @return */private File downFile(final String httpUrl) {new Thread(new Runnable() {@Overridepublic void run() {try {URL url = new URL(httpUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);FileOutputStream fileOutputStream = null;InputStream inputStream;if (connection.getResponseCode() == 200) {inputStream = connection.getInputStream();if (inputStream != null) {file = getFile(httpUrl);fileOutputStream = new FileOutputStream(file);byte[] buffer = new byte[1024];int length = 0;while ((length = inputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, length);}fileOutputStream.close();fileOutputStream.flush();}inputStream.close();}System.out.println("已经下载完成");// 往handler发送一条消息 更改button的text属性Message message = handler.obtainMessage();message.what = 1;handler.sendMessage(message);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}).start();return file;}/** * 安装APK */private void installApk() {Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");startActivity(intent);}@Overrideprotected void onStart() {super.onStart();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);intentFilter.addDataScheme("package");// 注册一个广播registerReceiver(broadcastReceiver, intentFilter);}@Overrideprotected void onDestroy() {super.onDestroy();// 解除广播unregisterReceiver(broadcastReceiver);}/** * 打开已经安装好的apk */private void openApk(Context context, String url) {PackageManager manager = context.getPackageManager();// 这里的是你下载好的文件路径PackageInfo info = manager.getPackageArchiveInfo(Environment.getExternalStorageDirectory().getAbsolutePath()+ getFilePath(url), PackageManager.GET_ACTIVITIES);if (info != null) {Intent intent = manager.getLaunchIntentForPackage(info.applicationInfo.packageName);startActivity(intent);}}/** * 根据传过来url创建文件 * */private File getFile(String url) {File files = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), getFilePath(url));return files;}/** * 截取出url后面的apk的文件名 * * @param url * @return */private String getFilePath(String url) {return url.substring(url.lastIndexOf("/"), url.length());}}
IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.PACKAGE_ADDED"); filter.addAction("android.intent.action.PACKAGE_REMOVED"); filter.addDataScheme("package");
监听安装apk和卸载apk的广播,其它的相信大家看代码也能看懂了,代码有点粗糙(菜鸟一枚),有哪里写的不好的地方,欢迎大家指正。
这个程序我没有考虑其他的情况,比如apk安装出错了,要怎么处理,等等。。
忘记说了,还需要在配置文件中添加访问网络和往sd卡写文件的权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
源代码的下载地址
2 0
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- Android 下载APK 安装APK 打开APK
- Android 更新下载apk 安装apk 打开apk
- Android 打开安装apk
- Android apk下载 安装 卸载 打开
- android 下载安装并打开apk
- Android 下载安装APK
- Android 下载安装APK
- android下载安装APK
- android下载安装APK
- android下载安装APK
- apk下载 安装 卸载 打开
- android下载apk并安装
- Android下载并安装APK
- android --APK下载、安装、启动
- android下载apk且安装
- 处理空格文本节点
- ubuntu挂载新硬盘
- 大型互联网站解决高并发的常见策略
- Ubuntu下的配置文件
- 攻击工具SYN Flood的源代码
- Android 下载APK 安装APK 打开APK
- 时分多路复用(TDM)
- 求两个字符串最长公共子串(动态规划)
- ckermit 使用xcom 协议发送文件
- android取消开机时候的震动
- ubuntu安装nfs中的一些问题总结
- sscanf的基本用法
- 高并发量网站解决方案
- 关注C++细节——字面值初始化字符数组及字符串拷贝注意