Android之在launcher里面动态加载桌面图标

举报
chenyu 发表于 2021/07/27 00:30:08 2021/07/27
1.7k+ 0 0
【摘要】 1、在手机桌面加载图标方式          1)、动态加载         Launcher.javaprivate void addSourceList() { Intent launchIntent = new In...

1、在手机桌面加载图标方式

         1)、动态加载

       


      Launcher.java
      private void addSourceList() {
       Intent launchIntent = new Intent(this, RcGrpActivity.class);
       launchIntent.setAction(Intent.ACTION_MAIN);
       launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       Intent addIntent = new Intent();
       addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
       addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.source_list));
       addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
       ShortcutIconResource.fromContext(this, R.drawable.keycard));
       ShortcutInfo shortcut = mModel.infoFromShortcutIntent(this, addIntent);
       shortcut.deletable = false;
       shortcut.titleResource = getResources().getResourceName(R.string.source_list);
       shortcut.presetItemId = getResources().getInteger(R.integer.preset_source_list_icon);
       if (LauncherModel.presetItemExists(this, shortcut.presetItemId)) {
       return;
       }
       ArrayList<ItemInfo> list = new ArrayList<ItemInfo>();
       list.add(shortcut);
       mModel.addAndBindAddedApps(this, list, new ArrayList<AppInfo>(), true);
       }
      LauncherModel.java
       static boolean presetItemExists(Context context, int presetItemId) {
       final ContentResolver cr = context.getContentResolver();
       Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
       new String[]{"title"},
       "presetItemId=?",
       new String[]{Integer.toString(presetItemId)},
       null);
       if (c == null) {
       return false;
       }
       try {
       return c.moveToFirst();
       } finally {
       c.close();
       }
       }
  
 


    优点:不需要平板适配

     2)、静态加载

     通过xml文件加载

      1、xml文件


      <?xml version="1.0" encoding="utf-8"?>
      <favorites xmlns:launcher="http://schemas.android.com/apk/res-auto">
      <shortcut
      launcher:uri="#Intent;action=com.sangfor.action.ACTION_OPEN_RESOURCE;category=android.intent.category.DEFAULT;end"
      launcher:iconRes="@drawable/ic_setting_app"
      launcher:titleRes="@string/source_list"
      launcher:container="-100"
      launcher:screen="0"
      launcher:x="2"
      launcher:y="2"
      launcher:deletable="false"
      launcher:presetItemId="@integer/preset_settings_icon"
       />
      </favorites>
  
 


     2、  AndroidManifest.xml


      <activity
      android:name="com.sangfor.vpn.client.phone.resource.RcGrpActivity"
      android:configChanges="orientation|keyboardHidden|screenSize"
      android:label="@string/about"
      android:screenOrientation="behind"
      android:theme="@android:style/Theme.Light.NoTitleBar" >
      <intent-filter>
      <action android:name="com.sangfor.action.ACTION_OPEN_RESOURCE" />
      <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
      </activity>
  
 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/75268951

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。