packagecom.zhy.shortcut; 导入安卓. app.activity; import Android.content.component name; 导入安卓. content.intent; 导入安卓. content.shared preferences; 导入安卓. content.shared preferences.editor; 导入安卓. OS.bundle; 导入Android.preference.preference manager; 导入安卓. view.view; 导入安卓. view.view.onclick listener; import Android .构件. button; pblicclasshortcutactivityextendsactivity { privatestaticfinalstringcreate _ shortcut _ action=' com.Android.launcher privatestaticfinalstringdrop _ shortcut _ action=' com.Android.launcher.action.uninstall _ shortcut '; privatestaticfinalstringpreference _ key _ shortcut _ exists=' is shortcut exists ';
蓝牙; //获取默认sharedpreferencessharedpreferencessharedpreferences; 获取SharedPreferences中是否存在快捷方式如果没有返回false的程序第一次进入一定会返回falsebooleanexists;
@ overridepublicvoidoncreate (bundlesavedinstancestate ) super.oncreate ) savedinstancestate;
shared preferences=preference manager.getdefaultsharedpreferences;
exists=shared preferences.get boolean (preference _ key _ shortcut _ exists,false ); //创建桌面快捷方式//创建第一次启动时创建,下次启动时不创建if (exists ) {
setUpShortCut (;
}
setcontentview(r.layout.main );
Button=(Button ) findviewbyid ) r.id.dropshortcut;
button.setonclicklistener (newonclicklistener ) )。
@ overridepublicvoidonclick (viewv ) {
tearDownShortCut (;
}
);
}/***创建桌面快捷方式*/privatevoidsetUpShortCut () )。
intent intent=new intent (create _ shortcut _ action; //快捷方式图像intent.put extra (intent.extra _ shortcut _ icon _ resource, intent.shortcuticonresource.from context ) thi int.shortcuticonresource.from context//快捷方式名称intent.putextra 设置是否允许重新创建//快捷方式的false表示不允许intent.putextra('duplicate ',false )。 //打开快捷方式intent//第一种方法创建打开快捷方式的目标intententtargetintent=new intent (; //将桌面快捷方式target intent.set action (intent.action _ main )也设置为在卸载APP应用程序时删除
目标intent.add category (Android.intent.category.launcher );
componentnamecomponentname=newcomponentname (get package name ),this.getClass ).getName );
目标intent.set component (component name;//第二种方法创建打开快捷方式的目标intent/**Intent
* target intent=getpackagemanager ().getlaunchintentforpackage ) ) getpackagename
* (); */intent.put extra (intent.extra _ shortcut _ intent,targetIntent ); //发送广播的sendbroadcast(intent );
编辑器编辑器=shared preferences.edit (;
editor.put boolean (preference _ key _ shortcut _ exists,true );
editor.commit (;
(}/***删除桌面快捷方式(*/privatevoidtearDownShortCut ) )。
intent intent=new intent (drop _ shortcut _ action; //指定要删除的shortcut名称intent.put extra (intent.extra _ shortcut _ name,' sina ' );
stringappclass=get package name '.' this.getlocalclassname (;
组件名称=newcomponentname (get package name )、appClass );
intent.put extra (intent.extra _ shortcut _ intent,newIntent ).setaction(intent.action_main ).setcomponent
发送广播(英特尔);
}
}