安卓添加快捷方式
在做软件或者游戏时,可能需要给应用创建快捷方式,其他的不说,上代码
因为我都是保存在有道云笔记中。排版都乱掉了。将就看吧。
//创建快捷方式start SharedPreferences preferences = getSharedPreferences("GT2ShortCut", Context.MODE_PRIVATE); boolean isGTShortCut = preferences.getBoolean("isGT2ShortCut", true); if (isGTShortCut) { final Intent launchIntent = getIntent(); final String action = launchIntent.getAction(); Intent shortcutIntent = new Intent(); //设置点击快捷方式时启动的Activity,因为是从Lanucher中启动,所以包名类名要写全。 shortcutIntent.setComponent(new ComponentName(getPackageName(), getPackageName() + "."+ GTDownloaderActivity.class.getSimpleName())); shortcutIntent = new Intent(this, GTDownloaderActivity.class); //设置启动的模式 //shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS| Intent.FLAG_ACTIVITY_NEW_TASK); Intent resultIntent = new Intent(); //设置快捷方式图标 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.app_icon_uc)); //启动的Intent resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); //设置快捷方式的名称 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 发送广播方式添加快捷方式----->即主动的发广播方式 resultIntent.setAction(ACTION_INSTALL_SHORTCUT); sendBroadcast(resultIntent); } SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("isGT2ShortCut", false); editor.commit(); //end
配置xml(因为我这里用到了谷歌提供的obb下载服务)
将下面的xml改成你的active就可以了。
<!-- 下载的Active,并开启下载服务--> <activity android:name="com.tt.xxxx.download.GTDownloaderActivity" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <service android:name="com.tt.xxxx.download.GTDownloaderService" />