zoukankan      html  css  js  c++  java
  • Android中的桌面快捷方式

    一、判断是否已有快捷方式

        private String getAuthorityFromPermission(Context context, String permission){
            if (permission == null) return null;
            List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
            if (packs != null) {
                for (PackageInfo pack : packs) { 
                    ProviderInfo[] providers = pack.providers; 
                    if (providers != null) { 
                        for (ProviderInfo provider : providers) { 
                            if (permission.equals(provider.readPermission)) return provider.authority;
                            if (permission.equals(provider.writePermission)) return provider.authority;
                        } 
                    }
                }
            }
            
            return null;
        }
    private boolean hasShortcut(Context context,String shortCutName)
        {
            boolean has = false;
            final ContentResolver cr = context.getContentResolver();
            final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");
    
            final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
            
            //确认content Provider中是否有快捷键信息
            Cursor c = cr.query(CONTENT_URI,
                                new String[] {"title","iconResource" },
                                "title=?",
                                new String[] {shortCutString.trim()}, 
                                null);
            if(c != null && c.getCount() > 0){
                has= true ;
            }
            return has;
        }

    二、添加快捷方式

    private void addShortCut(Context mContext)
        {              
    
            boolean has = hasShortcut(mContext, mContext.getString(R.string.str_app_name));
    
            if(has)
            {
                return;
            }
    
            Intent shortCutIntent = null;
            int shortCutNameId = R.string.app_name;
            int shortCutIconId = R.drawable.app_icon;
            String pkg = PACKAGE_NAME;
    
            boolean installed = isInstalledApp(mContext);
    
    
            if(installed)
            {
    
                Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
                resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                resolveIntent.setPackage(pkg);          
                List<ResolveInfo> apps = mContext.getPackageManager().queryIntentActivities(resolveIntent, PackageManager.GET_ACTIVITIES);
                
    
                shortCutIntent = new Intent();
                if((apps != null) && (apps.size() != 0))
                {
                    shortCutIntent.setComponent(new ComponentName(pkg, apps.get(0).activityInfo.name));
                }
            }
            else 
            {
                shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
            }
            
    
            Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
            
            //快捷方式的名称  
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(shortCutNameId));  
            shortcut.putExtra("duplicate", false); //不允许重复创建  
     
            //快捷方式的图标  
            ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), shortCutIconId);  
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  
            
            //程序入口
            shortCutIntent.setAction(Intent.ACTION_MAIN);
            shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
    
            mContext.sendBroadcast(shortcut);   
        }

    三、删除快捷方式

       private void deleteShortCut(Context mContext)
        {
            Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
            Intent shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
            shortCutIntent.setAction(Intent.ACTION_MAIN);
            shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
            // 快捷方式的名称
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name));
    
            // 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent);
    
            mContext.sendBroadcast(shortcut);
        }

    四、被快捷方式启动的Activity的Intent-filter

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>    
  • 相关阅读:
    信号signal的监听与处理
    oracle 12cR1&12cR2核心高实用性新特性
    Tomcat 7服务器线程模型
    抓取awr、语句级awr、ashrpt
    从percona server 5.7换到mariadb 10.2
    关于typeid和typeof
    mysql查询INFORMATION_SCHEMA表很慢的性能优化
    使用ccache大幅度加速gcc编译速度至少1倍以上(不需要修改任何编译选项)
    c++ linux下输出中文
    visual studio 2015下使用gcc调试linux c++开发环境搭建完整详解
  • 原文地址:https://www.cnblogs.com/CoolPigs/p/3317234.html
Copyright © 2011-2022 走看看