应用程序创建快捷方式并判断是否创建(android)

来源:互联网 发布:山东大学软件学院吧 编辑:程序博客网 时间:2024/06/05 13:30
先定义两个函数: 
 public void addShortCut(){        Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");        String myTitle=getResources().getString(R.string.app_name);        Parcelable icon=Intent.ShortcutIconResource.fromContext(this,R.drawable.app);        //设置点击快捷方式操作后的Intent        //Intent myIntent=new Intent(this,Start.class);                // 是否允许重复创建                  addIntent.putExtra("duplicate", false);                   Intent intent = new Intent(Intent.ACTION_MAIN);                  intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);                  intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);                  intent.addCategory(Intent.CATEGORY_LAUNCHER);                  intent.setClass(this,Start.class);         addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,myTitle);        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);        sendBroadcast(addIntent);    }    public  boolean  IfaddShortCut(){             boolean isInstallShortcut = false ;                 final ContentResolver cr = AppstoreActivity.this.getContentResolver();                   final String AUTHORITY = "com.android.launcher2.settings";               final Uri CONTENT_URI = Uri.parse("content://" +                                  AUTHORITY + "/favorites?notify=true");              Cursor c = cr.query(CONTENT_URI,                new String[] {"title","iconResource" },                "title=?",                new String[] {getString(R.string.app_name ) }, null);                      if(c!=null && c.getCount()>0){                     isInstallShortcut = true ;                 }               return isInstallShortcut ;             }  


然后在程序启动时:

 //创建快捷方式        boolean  flag =IfaddShortCut();//如果已经创建,则不需要在创建                  if(flag==false){                         addShortCut();               }