Android create and verify that and remove the desktop shortcut of has been tested for availability

  • 2020-05-09 19:17:52
  • OfStack

The test environment is above Adnroid 2.1.
Step 1: AndroidManifest.xml permission configuration:
Add shortcut permissions:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 

Verify whether the shortcut has permissions:

<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

Delete shortcut permissions:  

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Code:
 
public class ShortCutSample { 
/** 
*  Add shortcuts  
* */ 
public void creatShortCut(Activity activity,String shortcutName,int resourceId) 
{ 
Intent intent = new Intent(); 
intent.setClass(activity, activity.getClass()); 
/* The following two sentences are intended to remove the desktop shortcut when uninstalling the application */ 
intent.setAction("android.intent.action.MAIN"); 
intent.addCategory("android.intent.category.LAUNCHER"); 
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
// Duplicate creation is not allowed  
shortcutintent.putExtra("duplicate", false); 
// You need a realistic name  
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName); 
// Fast image  
Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId); 
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
// Click the shortcut picture to run the main entry of the program  
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); 
// Broadcast. OK 
activity.sendBroadcast(shortcutintent); 
} 
/** 
*  Delete shortcut  
* */ 
public void deleteShortCut(Activity activity,String shortcutName) 
{ 
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
// The name of the shortcut  
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName); 
// Basically what you see online 1 In the next few sentences, the test found that the shortcut could not be deleted.  
//String appClass = activity.getPackageName()+"."+ activity.getLocalClassName(); 
//ComponentName comp = new ComponentName( activity.getPackageName(), appClass); 
//shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 
/** Change to the following method can be successfully deleted, it is estimated that the deletion and creation need corresponding to find the shortcut and successfully deleted **/ 
Intent intent = new Intent(); 
intent.setClass(activity, activity.getClass()); 
intent.setAction("android.intent.action.MAIN"); 
intent.addCategory("android.intent.category.LAUNCHER"); 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent); 
activity.sendBroadcast(shortcut); 
} 
/** 
*  Determine if there are shortcuts  
* */ 
public boolean hasShortcut(Activity activity,String shortcutName) 
{ 
String url = ""; 
int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK); 
/* Is greater than 8 When in com.android.launcher2.settings  Query inside (not tested) */ 
if(systemversion < 8){ 
url = "content://com.android.launcher.settings/favorites?notify=true"; 
}else{ 
url = "content://com.android.launcher2.settings/favorites?notify=true"; 
} 
ContentResolver resolver = activity.getContentResolver(); 
Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null); 
if (cursor != null && cursor.moveToFirst()) { 
cursor.close(); 
return true; 
} 
return false; 
} 
} 

Call test code:
 
 public class mainActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
ShortCutSample sample =new ShortCutSample(); 
String shortcutName=getString(R.string.app_name); 
if(sample.hasShortcut(this, shortcutName)) 
sample.deleteShortCut(this,shortcutName); 
else 
sample.creatShortCut(this,shortcutName,R.drawable.icon); 
} 
} 

Looking for a long time on the Internet are 1 kind of code, delete that piece of work took 1 afternoon to get ready, in fact, very simple stuff.
First post, Adnroid new guy. Communicate and coach a lot. Ha ha.

Related articles: