Android 主界面应用图标的动态修改


       使用android 手机的用户,大多数人都喜欢将自己常用的应用放置于主界面上面;对于放置于桌面上的应用图标,系统具有两个接口允许所有的应用通过广播的方式进行删除和添加;

例如:

    static final String ACTION_INSTALL = "com.android.launcher.action.INSTALL_SHORTCUT";     
    static final String ACTION_UNINSTALL = "com.android.launcher.action.UNINSTALL_SHORTCUT"; 

卸载:

    void unInstallShortcutToDeskTop() {
        
        Intent shortcut = new Intent(ACTION_UNINSTALL);  
        String label = this.getResources().getString(R.string.app_name);
        String packageName = this.getPackageName();
        
        PackageManager packageManager = this.getPackageManager();  
        
        try {  
            ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA|PackageManager.GET_UNINSTALLED_PACKAGES);  
            if( packageName==null){  
                packageName = packageManager.getApplicationLabel(appInfo).toString();  
            }  
        } catch (NameNotFoundException e) {  
            e.printStackTrace();  
            return;  
        }
        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);  
        ComponentName comp = new ComponentName(packageName, packageName +"." + this.getLocalClassName());  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  
        sendBroadcast(shortcut);
    }



创建:

void addShortcutToDeskTop(){  
        
        Intent shortcut = new Intent(ACTION_INSTALL);  
   
        BitmapDrawable iconBitmapDrawabel = null;  
   
        String label = this.getResources().getString(R.string.app_name);
        String packageName = this.getPackageName();
        PackageManager packageManager = getPackageManager();  

        try {  
            ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA|PackageManager.GET_ACTIVITIES);  
            if(packageName==null){  
                packageName = packageManager.getApplicationLabel(appInfo).toString();  
            }  
            if(iconBitmapDrawabel==null){  
                iconBitmapDrawabel = (BitmapDrawable) packageManager.getApplicationIcon(appInfo);  
            }  
        } catch (NameNotFoundException e) {  
            e.printStackTrace();  
            return;  
        }  
        
        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap());  
   
        shortcut.putExtra("duplicate", false);   
        
        String localClassName = this.getLocalClassName();
        ComponentName comp = new ComponentName( packageName, packageName +"." + this.getLocalClassName() );  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(comp));  
   
        sendBroadcast(shortcut);  
    }  


由上面的接口,用户可以自定义主界面上的应用图标的修改;如ios 中的在应用图标中添加数字;只需要先调用上述的unInstallShortcutToDeskTop()将主界面上的应用图标删除 ;然后调用addShortcutToDeskTop() ; 需要在addShortcutToDeskTop() 中,修改   shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap()); 
为自己想要的图标显示就可以;



思考:由于所有的应用都可以通过广播使用这两个接口;那么如果应用开发者替换掉了其他应用程序图标的入口而改为了自己自定义的应用入口,并对自定义的应用进行伪装,那么是否会出现应用开发者窃取用户信息等安全问题呢?



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值