android通過代碼的形式來實(shí)現(xiàn)應(yīng)用程序的方法
注意:
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");這一句話中,第一個(gè)參數(shù)是要安裝的apk的路徑,第二個(gè)參數(shù)是apk所對(duì)應(yīng)的類型??梢栽襱omcat的安裝目錄下的conf目錄下的web.xml中找到
程序運(yùn)行截圖:

代碼實(shí)現(xiàn)如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安裝"
android:onClick="install"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卸載"
android:onClick="uninstall"
/>
</LinearLayout>
2、MainActivity
package com.njupt.install;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void install(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory(),"HtmlUI1.apk");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
public void uninstall(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.njupt.htmlui1"));
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
相關(guān)文章
Android新建水平節(jié)點(diǎn)進(jìn)度條示例
這篇文章主要為大家介紹了Android新建水平節(jié)點(diǎn)進(jìn)度條示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android自定義View實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android應(yīng)用框架之應(yīng)用啟動(dòng)過程詳解
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用框架,應(yīng)用啟動(dòng)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法分析
這篇文章主要介紹了Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法,分析了Android數(shù)據(jù)庫事務(wù)的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06
fragment實(shí)現(xiàn)隱藏及界面切換效果
這篇文章主要為大家詳細(xì)介紹了fragment實(shí)現(xiàn)隱藏及界面切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
詳解Kotlin Android開發(fā)中的環(huán)境配置
這篇文章主要介紹了詳解Kotlin Android開發(fā)中的環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2017-06-06
Flutter實(shí)現(xiàn)視頻壓縮功能的示例代碼
移動(dòng)應(yīng)用程序中,視頻占用了大量的存儲(chǔ)空間和帶寬,這在一定程度上影響了應(yīng)用程序的性能和用戶體驗(yàn),所以本文為大家準(zhǔn)備了Flutter實(shí)現(xiàn)視頻壓縮的方法,需要的可以參考一下2023-06-06

