android - 为什么我的代码没有在 Android 8+ (Oreo+) 中启动我的应用程序的固定快捷方式?
问题描述
我正在尝试固定我的应用程序的一些动态快捷方式,它们将在用户创建自定义时间时创建。
我有两个版本的代码,我正在使用JavaScriptInterface从WebView启动代码,但它们都没有按预期工作,因为其中一个正在尝试打开 Play 商店,而第二个说:“应用程序没有'不存在”当我从应用程序创建快捷方式时。
这个正在启动Play 商店:
[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
if (manager.IsRequestPinShortcutSupported)
{
try
{
//Create the new intent
var intent = new Intent(Intent.ActionView);
//Set the flag of the new task
intent.AddFlags(ActivityFlags.NewTask);
//Get the apps from the Play Store
intent.SetData(Android.Net.Uri.Parse("market://details?id=" + context.PackageName));
//Set the custom time as a variable
intent.PutExtra("customTime", time);
//Set the info of the shortcut
var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
.SetShortLabel("TM Timer")
.SetLongLabel("TM Timer")
.SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
.SetIntent(intent)
.Build();
//Set values
var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
intent, /* flags */ 0);
//Creates the shortcut
manager.RequestPinShortcut(info, successCallback.IntentSender);
}
catch (System.Exception ex)
{
}
}
}
这是说该应用程序不存在:
[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
if (manager.IsRequestPinShortcutSupported)
{
try
{
//Set the info of the shortcut with the App to open
var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
.SetShortLabel("TM Timer")
.SetLongLabel("TM Timer")
.SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
.SetIntent(new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName)))
.Build();
//Create the new intent
var intent = manager.CreateShortcutResultIntent(info);
intent.PutExtra("customTime", time);
//Set values
var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
intent, /* flags */ 0);
//Creates the shortcut
manager.RequestPinShortcut(info, successCallback.IntentSender);
}
catch (System.Exception ex)
{
}
}
}
我尝试了第三个代码,但那个代码试图打开任何不是我自己的应用程序的应用程序。有没有人经历过类似的事情?或者知道我错过了什么?
我遵循了多个教程和示例,例如:
谢谢你的支持。
PS:
- 我所有的测试都是在 Android Pie 下完成的。
- 我已经用 C# 在 Xamarin.Android 中构建了代码,但是如果您对 Kotlin 或 Java 有想法,我可以迁移它。
解决方案
当用户单击快捷方式时,将启动此意图:
new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName))
要启动特定活动,请将其替换为(在 java 中):
Intent i = new Intent(context.getApplicationContext(), MainActivity.class);
i.setAction(Intent.ACTION_VIEW);
推荐阅读
- vue.js - PrimeVue 数据表不显示
- javascript - 仅在 Firefox 中,“location.href = ..”重定向后未到达或停止警报框
- c - 为什么我的 scanf 函数在我的代码中不起作用并被忽略?
- javascript - Npm 使用正确的 package.json 共享 node_modules
- javascript - 获取 UTC 日期范围
- node.js - 为什么 resolve("./../....") 和 resolve("foo") 有区别
- python - Excel在Openpyxl生成的工作簿中添加@符号
- ios - 当 APNS 推送通知进来时无法播放声音
- python - 如何将我的搜索字段从查找商品 ID 号转换为根据商品名称查找商品?
- ghost-blog - Ghost — 在索引中显示带有特定标签的帖子