Sgo*_*nks 57 android shortcut homescreen
我想做的是:
1)我在一个活动中,有2个按钮.如果我单击第一个,则会在主屏幕中创建一个快捷方式.快捷方式打开html
以前下载的页面,所以我希望它使用默认浏览器,但我不想使用因特网,因为我已经有了该页面.
2)第二个按钮创建另一个启动活动的快捷方式.我想传递一些额外的参数(例如字符串)...........
那些事情可能吗?我发现了一些链接和一些类似Android的问题 :是否有一种编程方式可以在主屏幕上创建Web快捷方式
他们似乎是我的问题的答案,但有人告诉我,这个代码不会在所有设备上工作,这是不赞成的,我想做的是不可能.......
不建议使用此技术.这是一个内部实现,不是Android SDK的一部分.它不适用于所有主屏幕实现.它可能不适用于所有以前版本的Android.它可能无法在Android的未来版本中使用,因为Google没有义务维护内部未记录的界面.请不要使用它
内部实施意味着什么?该代码是否可信赖.....请帮助我.....
ant*_*oft 83
示例代码使用未记录的接口(权限和意图)来安装快捷方式.正如"有人"告诉你的那样,这可能无法在所有手机上运行,并且可能会在未来的Android版本中出现问题.不要这样做.
正确的方法是从主屏幕监听快捷方式请求 - 在您的清单中使用类似意图的过滤器:
<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
然后在接收意图的活动中,为快捷方式创建意图并将其作为活动结果返回.
// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intent = new Intent();
Intent launchIntent = new Intent(this,ActivityToLaunch.class);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
setResult(RESULT_OK, intent);
Run Code Online (Sandbox Code Playgroud)
Sid*_*kar 66
我已经开发了一种方法,用于在Android主屏幕[在我自己的应用程序上测试]创建快捷方式图标.打电话吧.
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Run Code Online (Sandbox Code Playgroud)
不要忘记更改您的活动名称,图标资源和权限.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Run Code Online (Sandbox Code Playgroud)
快乐的编码!
编辑:
对于重复问题,第一个选项是在代码中添加以下行,否则每次都会创建新行.
addIntent.putExtra("duplicate", false);
Run Code Online (Sandbox Code Playgroud)
第二个选项是首先卸载应用程序快捷方式图标,然后再次安装它如果第一个选项不起作用.
Q L*_*ker 11
自从android oreo以来,com.android.launcher.action.INSTALL_SHORTCUT广播不再有任何影响.链接
如果你想支持所有Android版本,特别是android 8.0或者oreo和更新版本,请使用下面的代码创建快捷方式:
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
Run Code Online (Sandbox Code Playgroud)
在清单文件中添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Run Code Online (Sandbox Code Playgroud)
我在上面改进了一点解决方案.现在,它会保存首选项是否已添加快捷方式,如果用户将其删除,则不会在应用程序的新启动中添加快捷方式.这也节省了一点时间,因为添加现有快捷方式的代码不再运行.
final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
// Creates shortcut on Android widget screen
private void createShortcutIcon(){
// Checking if ShortCut was already added
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
if (shortCutWasAlreadyAdded) return;
Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
// Remembering that ShortCut was already added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
从Android O开始,这是创建快捷方式的方法:
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.setShortLabel(label)
.setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
.build();
shortcutManager.requestPinShortcut(pinShortcutInfo, null);
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,它有很多局限性:
对于AndroidO之前的版本,您也可以使用ShortcutManagerCompat创建新的快捷方式,而不受任何限制.
归档时间: |
|
查看次数: |
91242 次 |
最近记录: |