为android应用程序实现快捷方式时出现的问题
我使用下面的代码为我的应用程序创建快捷方式,我在我的Splashscreen(这是我的第一个屏幕)页面中添加了此方法,在第一次执行时将创建快捷方式,现在的问题是每次启动活动时都会创建快捷方式为android应用程序实现快捷方式时出现的问题,android,Android,我使用下面的代码为我的应用程序创建快捷方式,我在我的Splashscreen(这是我的第一个屏幕)页面中添加了此方法,在第一次执行时将创建快捷方式,现在的问题是每次启动活动时都会创建快捷方式 public boolean setShortCut(Context context, String appName) { System.out.println("in the shortcutapp on create method "); boolean flag = f
public boolean setShortCut(Context context, String appName) {
System.out.println("in the shortcutapp on create method ");
boolean flag = false;
int app_id = -1;
PackageManager p = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> res = p.queryIntentActivities(i, 0);
System.out.println("the res size is: " + res.size());
for (int k = 0; k < res.size(); k++) {
System.out.println("the application name is: "
+ res.get(k).activityInfo.loadLabel(p));
if (res.get(k).activityInfo.loadLabel(p).toString().equals(appName)) {
flag = true;
app_id = k;
break;
}
}
if (flag) {
ActivityInfo ai = res.get(app_id).activityInfo;
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context,
R.drawable.app_icon));
// intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
System.out.println("in the shortcutapp on create method completed");
} else
System.out.println("appllicaton not found");
return true;
}
public boolean setShortCut(上下文,字符串appName){
System.out.println(“在创建方法上的shortcutapp中”);
布尔标志=假;
int app_id=-1;
PackageManager p=context.getPackageManager();
意向i=新意向(意向.行动\主要);
i、 addCategory(意图.类别\发射器);
列表res=p.querytentActivities(i,0);
System.out.println(“分辨率大小为:“+res.size()”);
对于(int k=0;k
在应用程序的共享首选项中维护一个布尔值,一旦创建快捷方式,该值将设置为true
if (flag && !isShortcutCreatedAlready()) {
// Your code to create shortcut. Put the below code at the end of your try block
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("isShortcutCreated", true);
editor.commit();
}
private boolean isShortcutCreatedAlready(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
return prefs.getBoolean("isShortcutCreated", false);
}
你可以奖励赏金,以尽可能最好的方式表达你的感激之情!;)