国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android在一個app中安裝并卸載另一個app的示例代碼

瀏覽:32日期:2022-09-20 16:53:49

Android在一個app中安裝并卸載另一個app

1.在app→src→main下新建文件夾asserts,將準備安裝的apk文件放在asserts內

2.在app→src→main→res下新建文件夾xml,右擊xml文件夾,選擇new→XML Resource File,File 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) { //權限判定 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;}//判斷是否有權限修改,這很重要 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() {@Overridepublic 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文件復制到手機存儲中 //可以從手機存儲劃到最下面,是單獨的文件,沒有設置文件夾 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; }}

網絡上的教程有很多自相矛盾,本人防止權限出錯,把能加的權限都設置了一遍,有空可以選擇測試一下,應該有的可以刪掉

到此這篇關于Android在一個app中安裝并卸載另一個app的示例代碼的文章就介紹到這了,更多相關Android app 安裝卸載內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 免费看特级淫片日本 | 91精品久久国产青草 | 国产亚洲精品成人婷婷久久小说 | 91久久国产露脸精品免费 | 成人丝袜激情一区二区 | 精品综合 | 亚洲国产精品激情在线观看 | 欧美综合在线观看 | 成人欧美一区二区三区 | 精品在线免费观看 | 国产一级片儿 | 国产一区二区三区在线看 | 日本高清在线精品一区二区三区 | 国产成人在线视频网站 | 欧美国产高清 | 欧美色偷偷 | 免费看又黄又爽又猛的网站 | av成人天堂 | 日韩精品一区二区三区四区 | 男人天堂网2022 | 韩国毛片一级 | 成人一级片在线观看 | 国产特级全黄一级毛片不卡 | 欧美精品久久久亚洲 | 国产一区二区三区高清视频 | 美女又黄又免费视频 | 欧美在线视频免费 | 国产精品毛片 | 日韩毛片免费视频一级特黄 | 夜色邦合成福利网站 | 在线观看国内自拍 | 韩国免费播放一级毛片 | 欧美日韩精品一区二区三区高清视频 | 91欧美在线视频 | 欧美视频在线观看网站 | 深夜福利视频在线观看 | 午夜三级理论在线观看视频 | 国产一区二区三区在线免费 | 日本三级毛片 | 欧美另类 videos黑人极品 | 三级网址在线观看 |