代碼安裝apk
- Intent i = new Intent(Intent.ACTION_VIEW);
- String filePath = "/sdcard/XXX.apk";
- i.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");
- context.startActivity(i);
程序卸載
- Intent intent = new Intent(Intent.ACTION_DELETE, uri);
- intent.startActivity();
監聽是否卸載成功
- DeleteReceiver mDeleteReceiver = new DeleteReceiver();//自定義的廣播接收類,接收到結果后的操作
- IntentFilter filter = new IntentFilter(
- Intent.ACTION_PACKAGE_DATA_CLEARED);
- filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
- filter.addDataScheme("package");
- registerReceiver(mDeleteReceiver, filter); //注冊廣播和過濾
- Intent undeleteIntent = new Intent(Intent.ACTION_DELETE, packageURI);
- startActivity(undeleteIntent);
文件下載
DownloadProvider的權限級別改成 normal了就可以使用了 網上也有說明
首先要在AndroidManifest.xml中申請訪問DownloadManager的權限
- <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
添加一個下載任務:
- ContentValues values = new ContentValues();
- values.put(Downloads.URI, url);//指定下載地址
- values.put(Downloads.COOKIE_DATA, cookie);//如果下載Server需要cookie,設置cookie
- values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//設置下載提示 是否在屏幕頂部顯示
- values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//設置下載完成之后回調的包名
- values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//設置下載完成之后負責接收的Receiver,這個類要繼承 BroadcastReceiver
- values.put(Downloads.DESTINATION,save_path);//設置下載到的路徑,這個需要在Receiver里 自行處理
- values.put(Downloads.TITLE,title);//設置下載任務的名稱
- this.getContentResolver().insert(Downloads.CONTENT_URI, values);//將其插入到DownloadManager的數據庫中,數據庫會觸發修改事件,啟動下載任務
- ContentValues values = new ContentValues();
- values.put("uri", uri.toString());
- values.put("useragent", "Mozilla/5.0 (Linux; U; Android 1.5; en-us; sdk Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1");
- values.put("notificationpackage", getPackageName());
- values.put("notificationclass", "HelloWorld");
- values.put("visibility", 1);
- values.put("mimetype", mimetype);
- values.put("hint", filename);
- values.put("description", uri.getHost());
- values.put("total_bytes", 1349528);
- mResolver = getContentResolver();
- mResolver.insert(Uri.parse("content://downloads/download"), values);