Android:创建快捷方式
16lz
2021-12-04
- /**
- *创建快捷方式到Shortcut列表<br>
- *关联程序的<intent-filter>中添加:<actionandroid:name="android.intent.action.CREATE_SHORTCUT"/>
- */
- privatevoidaddShortcutToOptions(){
- Intentshortcut=newIntent(Intent.ACTION_CREATE_SHORTCUT);
- //不允许重建
- shortcut.putExtra("duplicate",false);
- //设置名字
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- this.getString(R.string.app_name));
- //设置图标
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.ic_launcher));
- //设置意图和快捷方式关联的程序
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
- newIntent(this,this.getClass()));
- //发送消息队列
- setResult(RESULT_OK,shortcut);
- }
- /**
- *添加快捷方式到桌面要点:
- *1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
- *2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
- *3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
- */
- privatevoidaddShortcutToDesktop(){
- Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
- //不允许重建
- shortcut.putExtra("duplicate",false);
- //设置名字
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,this.getString(R.string.app_name));
- //设置图标
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,
- R.drawable.ic_launcher));
- //设置意图和快捷方式关联程序
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(this,this.getClass()).setAction(Intent.ACTION_MAIN));
- //发送消息
- sendBroadcast(shortcut);
- }
- /**
- *添加权限:<uses-permissionandroid:name="com.android.launcher.permission.READ_SETTINGS"/>
- *
- *@return
- */
- privatebooleanhasInstallShortcut(){
- booleanhasInstall=false;
- finalStringAUTHORITY="com.android.launcher.settings";
- UriCONTENT_URI=Uri.parse("content://"+AUTHORITY
- +"/favorites?notify=true");
- Cursorcursor=this.getContentResolver().query(CONTENT_URI,
- newString[]{"title","iconResource"},"title=?",
- newString[]{this.getString(R.string.app_name)},null);
- if(cursor!=null&&cursor.getCount()>0){
- hasInstall=true;
- }
- returnhasInstall;
- }
更多相关文章
- android studio多渠道号,多包名打包
- 页面跳转采用滑动效果
- 设置可见和隐藏
- android dialog style属性设置
- Android(安卓)网络链接,不要忘记添加网络权限。
- android 让自己的app成为launcher
- Android添加横线和竖线分割界面
- Android(安卓)解析Html
- Android(安卓)如何设置网关和路由