Android在一個(gè)app中安裝并卸載另一個(gè)app的示例代碼
Android在一個(gè)app中安裝并卸載另一個(gè)app
1.在app→src→main下新建文件夾asserts,將準(zhǔn)備安裝的apk文件放在asserts內(nèi)
2.在app→src→main→res下新建文件夾xml,右擊xml文件夾,選擇new→XML Resource File,F(xiàn)ile name為文件名,可隨意,本文中為filepaths;Root element為資源類型,輸入paths,確定并編輯以下代碼:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>
3.編輯AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/Theme.MyApplication">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="包名"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths步驟2中的文件"
/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.MainActivity.java
public class MainActivity extends AppCompatActivity {
Context mContext;
private Button bt,bt0;
private TextView tx;
public static boolean isGrantExternalRW(Activity activity) {
//權(quán)限判定
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1);
return false;
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
tx=findViewById(R.id.text);
bt=findViewById(R.id.button);//安裝
bt0=findViewById(R.id.button0);//卸載
// Toast.makeText(this, ""+Environment.getExternalStorageDirectory().getAbsolutePath(), 0).show();
if(!this.isGrantExternalRW(MainActivity.this)){return;}//判斷是否有權(quán)限修改,這很重要
else{
if(copyApkFromAssets(this, "app-debug.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/app-debug.apk")){
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(mContext)
.setIcon(R.drawable.ic_launcher)
.setMessage("是否安裝?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+"/app-debug.apk"),
// "application/vnd.android.package-archive");
File apkFile = new File(Environment.getExternalStorageDirectory()+"/app-debug.apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication", apkFile);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
mContext.startActivity(intent);
}
}).show();
}
});
bt0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri packageURI = Uri.parse("package:com.example.a22");//package:+想要卸載的包名
Intent intent =new Intent(Intent.ACTION_DELETE);
intent.setData(packageURI);
mContext.startActivity(intent);
}
});
}
}}
public boolean copyApkFromAssets(Context context, String fileName, String path) {
//將asserts中的apk文件復(fù)制到手機(jī)存儲(chǔ)中
//可以從手機(jī)存儲(chǔ)劃到最下面,是單獨(dú)的文件,沒(méi)有設(shè)置文件夾
boolean copyIsFinish = false;
try {
InputStream is = context.getAssets().open(fileName);
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
int length = is.available();
byte[] temp = new byte[length];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
} catch (IOException e) {
e.printStackTrace();
}
return copyIsFinish;
}
}
網(wǎng)絡(luò)上的教程有很多自相矛盾,本人防止權(quán)限出錯(cuò),把能加的權(quán)限都設(shè)置了一遍,有空可以選擇測(cè)試一下,應(yīng)該有的可以刪掉
到此這篇關(guān)于Android在一個(gè)app中安裝并卸載另一個(gè)app的示例代碼的文章就介紹到這了,更多相關(guān)Android app 安裝卸載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)
- Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
- Android studio開(kāi)發(fā)小型對(duì)話機(jī)器人app(實(shí)例代碼)
- Android程序靜默安裝安裝后重新啟動(dòng)APP的方法
- Android開(kāi)發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
- Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案
相關(guān)文章
android實(shí)現(xiàn)圖片閃爍動(dòng)畫效果的兩種實(shí)現(xiàn)方式(實(shí)用性高)
本文通過(guò)兩種方法給大家講解了android實(shí)現(xiàn)圖片閃爍動(dòng)畫效果,實(shí)用性非常高,對(duì)這兩種方法感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-09-09
Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android Listview上下拉動(dòng)刷新tab滑動(dòng)切換功能
這篇文章主要介紹了Android Listview上下拉動(dòng)刷新tab滑動(dòng)切換功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android微信自動(dòng)搶紅包插件優(yōu)化和實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android微信自動(dòng)搶紅包插件優(yōu)化和實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
使用AccessibilityService實(shí)現(xiàn)微信自動(dòng)切換賬號(hào)功能
這篇文章主要為大家詳細(xì)介紹了使用AccessibilityService實(shí)現(xiàn)微信自動(dòng)切換賬號(hào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件
這篇文章主要介紹了Android 實(shí)現(xiàn)帶頭部文字輸入框的自定義控件,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
flutter TextField換行自適應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了flutter TextField換行自適應(yīng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

