Android 安卓api-26+;未调用快捷广播接收器
我正在尝试在桌面上为应用程序创建一个固定的快捷方式。CreateShortcut方法通过按钮调用,并显示android创建快捷方式对话框。当调用者选择ok时,广播接收器应被调用并执行finish,以便活动退出。Android 安卓api-26+;未调用快捷广播接收器,android,android-broadcastreceiver,desktop-shortcut,Android,Android Broadcastreceiver,Desktop Shortcut,我正在尝试在桌面上为应用程序创建一个固定的快捷方式。CreateShortcut方法通过按钮调用,并显示android创建快捷方式对话框。当调用者选择ok时,广播接收器应被调用并执行finish,以便活动退出。 这是我第一次使用广播接收器,但它看起来很简单。只需创建一个接收者,将其注册到一个与意图具有相同操作的意图过滤器,当发送意图时,它应该会导致调用接收者,对吗? 快捷方式创建得很好,但从未调用广播接收器。我没有在logcat上看到任何消息 private void CreateShortcu
这是我第一次使用广播接收器,但它看起来很简单。只需创建一个接收者,将其注册到一个与意图具有相同操作的意图过滤器,当发送意图时,它应该会导致调用接收者,对吗?
快捷方式创建得很好,但从未调用广播接收器。我没有在logcat上看到任何消息
private void CreateShortcut(final Context c) {
if (ShortcutManagerCompat
.isRequestPinShortcutSupported(c)) {
Intent shortcutIntent = new Intent(
c, CreateAppHomeShortcut.class);
shortcutIntent.setAction(
Intent.ACTION_CREATE_SHORTCUT);
ShortcutInfoCompat shortcutInfo
= new ShortcutInfoCompat
.Builder(c, "shortcut")
.setShortLabel(c.getString(R.string.app_name))
.setIcon(IconCompat.createWithResource(
c, R.drawable.qmark)
)
.setIntent(shortcutIntent)
.build();
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(
Context context, Intent intent) {
Log.d(TAG, "msg received");
unregisterReceiver(this);
finish();
}
}
, new IntentFilter(
Intent.ACTION_CREATE_SHORTCUT
)
);
PendingIntent successCallback =
PendingIntent.getBroadcast(
c, 99
, shortcutIntent, 0
);
ShortcutManagerCompat.requestPinShortcut(c,
shortcutInfo,
successCallback.getIntentSender()
);
}
}
我已经在这方面工作了好几天了,我被难住了。谢谢我终于收到了广播接收器的回电。我的主要问题是我使用的意图是错误的。我认为brodcast接收器意图和快捷方式意图可以相同,只要操作正确。错了!快捷方式的意图必须有一个动作集,但在我做的测试中,它似乎并不关心那个动作是什么。广播接收器被创建为“Intent=newintent(context,class);setAction(…);”,快捷方式将被创建,功能正常,但广播接收器从未被调用。我能让广播接收机工作的唯一方法就是只为它准备一个动作集(或者可能是额外的)集。我无法让程序使用相同的意图来创建快捷方式并调用广播接收器 遇到的另一个问题是,该接口允许您创建多个固定的快捷方式,然后为创建的每个快捷方式调用广播接收器一次。我发现你可以在界面上查询所有固定的快捷方式,并按id进行筛选,以确定你的快捷方式是否已经存在,并使用该信息避免在主页上创建多个相同的固定快捷方式 下面的代码在创建快捷方式方面似乎很好,只要用户接受快捷方式,就会调用接收者。文档声明,他们只会在用户接受后给您的接收者打电话。当然,这使得检测用户交互的结束变得相当困难。由于请求隐藏在我的实际应用程序中,所以计划将其作为单独活动的一部分打开,但如果用户不想要快捷方式,我无法检测到他是否完成了请求。如果有人有什么建议,我很乐意听取
// Create a shortcut and exit the activity. If the shortcut
// already exists,just exit.
private void CreateShortcut(final Context c) {
if (Build.VERSION.SDK_INT >= 26) {
ShortcutManager sm =
getSystemService(ShortcutManager.class);
if (sm != null && sm.isRequestPinShortcutSupported()) {
final String shortcutId = "StartApp";
boolean shortcutExists = false;
// We create the shortcut multiple times if given the
// opportunity. If the shortcut exists, put up
// a toast message and exit.
List<ShortcutInfo> shortcuts
= sm.getPinnedShortcuts();
for (int i = 0;
i < shortcuts.size() && !shortcutExists; i++) {
shortcutExists
= shortcuts.get(i).getId().equals(shortcutId);
if (shortcutExists) {
Toast.makeText(c , String.format(
"Shortcut %s already exists."
, shortcutId
)
, Toast.LENGTH_LONG
).show();
finishActivity();
}
else {
// this is the intent that actually creates the
// shortcut.
Intent shortcutIntent
= new Intent(c, CreateAppHomeShortcut.class);
shortcutIntent.setAction(
Intent.ACTION_CREATE_SHORTCUT);
ShortcutInfo shortcutInfo = new ShortcutInfo
.Builder(c, shortcutId)
.setShortLabel(
c.getString(R.string.app_name))
.setIcon(createWithResource(c
, R.drawable.qmark))
.setIntent(shortcutIntent)
.build();
// this intent is used to wake up the broadcast
// receiver.
// I couldn't get createShortcutResultIntent to
// work but just a simple intent as used for a
// normal broadcast intent works fine.
Intent broadcastIntent
= new Intent(Intent.ACTION_CREATE_SHORTCUT);
// create an anonymous broadcaster. Unregister
// to prevent leaks when done.
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(
Context c, Intent intent) {
unregisterReceiver(this);
Log.d(TAG, String.format(
"ShortcutReceiver activity = "
+ "\"$1%s\""
, intent.getAction()));
finishActivity();
}
}
, new IntentFilter(
Intent.ACTION_CREATE_SHORTCUT)
);
PendingIntent successCallback
= PendingIntent.getBroadcast(
c, 99
, broadcastIntent, 0);
// Shortcut gets created here.
sm.requestPinShortcut(shortcutInfo
, successCallback.getIntentSender());
}
}
}
}
//创建快捷方式并退出活动。如果走捷径
//已经存在,请退出。
私有void CreateShortcut(最终上下文c){
如果(Build.VERSION.SDK_INT>=26){
ShortcutManager sm=
getSystemService(ShortcutManager.class);
if(sm!=null&&sm.isRequestPinShortcutSupported()){
最终字符串shortcutId=“StartApp”;
布尔shortcutExists=false;
//如果给定快捷方式,我们会多次创建快捷方式
//机会。如果存在快捷方式,请张贴
//祝酒辞和退出。
列出快捷方式
=sm.getPinnedShortcuts();
对于(int i=0;
i