Android - 如何接收快捷方式创建结果

Android - How to receive shortcut create result

查看代码示例 here - 我发现以下评论令人费解:

// ... We assume here that the
// app has implemented a method called createShortcutResultIntent() that
// returns a broadcast intent.

这是什么意思应用程序已经实现了...这个实现是在哪里完成的?

它是广播接收器吗?注册到哪个意图过滤器?

这是抽象方法吗?其中 class?

然后我看到 this code sample - 它处理完全不同的流程(我认为),我又迷路了

您可以通过捕获您在使用requestPinShortcut 功能时设置的广播事件来获得反馈。 首先,您需要一个普通的广播接收器(在下面的代码中它的名称为 ShortcutReceiver)。您甚至可以使用现有的广播接收器并简单地添加它应该捕获的新操作。
让动作为 "general.intent.action.SHORTCUT_ADDED" 并将其存储在 ShortcutReceiver.kInstalledAction 常量中。在这种情况下,在清单中你应该有:

<receiver android:name=".ShortcutReceiver" >
  <intent-filter>
    <action android:name="general.intent.action.SHORTCUT_ADDED"/>
  </intent-filter>
</receiver>

在此之后,您可以在 activity 中使用以下代码来创建固定快捷方式(在其他地方,请在 Context class 的对象上更改此代码):

ShortcutManager manager = this.getSystemService(ShortcutManager.class);
Intent targetIntent = new Intent(ShortcutReceiver.kInstalledAction);
targetIntent.setPackage(this.getPackageName());
PendingIntent intent = PendingIntent.getBroadcast(this, 0, targetIntent, 0);
manager.requestPinShortcut(info, intent.getIntentSender());

在此代码中 infoShortcutInfo class 的正确对象。
您可以在捕获广播的同时处理事件:

public class ShortcutReceiver extends BroadcastReceiver {
  public static final String kInstalledAction = "general.intent.action.SHORTCUT_ADDED";

  @Override
  public void onReceive(Context context, Intent intent) {
    
    if (kInstalledAction.equals(intent.getAction())) {
      // Handle the event after the shortcut has been added 
      Toast.makeText(context, "The shortcut has been added", Toast.LENGTH_LONG).show();
    }
  
  }

}

请注意,根据我的经验,广播事件发生在添加快捷方式之后,但有时可能会有一些延迟(大约几分钟)。但可能对启动器有一些依赖性。

更新
正如 Android 8 上的其他答案中所述,通过广播捕获隐式意图通常不起作用。 所以我简单地通过设置当前应用程序的包名称将意图更改为显式。所以只有我们的广播接收器才能捕捉到意图。

要事第一。 Android 8.0 Oreo 的隐含意图:

Because Android 8.0 (API level 26) introduces new limitations for broadcast receivers, you should remove any broadcast receivers that are registered for implicit broadcast intents. Leaving them in place does not break your app at build-time or runtime, but they have no effect when your app runs on Android 8.0. Explicit broadcast intents—those that only your app can respond to—continue to work the same on Android 8.0. There are exceptions to this new restriction. For a list of implicit broadcasts that still work in apps targeting Android 8.0, see Implicit Broadcast Exceptions. https://developer.android.com/about/versions/oreo/android-8.0-changes

注意:有一些例外:https://developer.android.com/guide/components/broadcast-exceptions(很少)

相反,我们将使用所谓的上下文注册接收器,只要我们的应用程序存在,它就会一直存在,或者直到我们注销它。

此外,ShortcutManager 需要 API 25,这就是为什么我们将使用它的兼容版本,以免重复旧版本和新版本的代码。 (ShortcutManagerCompat 是在版本 26.1.0 中添加的)

在主屏幕上创建固定快捷方式的代码:

public static void addShortcut(Context context, String id) {
    if(context == null || note == null)
        return;

    //there may be various Home screen apps, better check it
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)){

        Intent shortcutIntent = new Intent(context, MainActivity.class);
        shortcutIntent.setAction(Constants.ACTION_SHORTCUT); // !!! intent's action must be set on oreo


        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, note.get_id().toString())
                .setIntent(shortcutIntent)
                .setShortLabel("MyShortcut") //recommend max 10 chars
                .setLongLabel("Long shortcut name")//recommend max 25 chars
                .setIcon(IconCompat.createWithResource(context, R.drawable.ic_shortcut))
                .build();


        //callback if user allowed to place the shortcut
        Intent pinnedShortcutCallbackIntent = new Intent(ACTION_SHORTCUT_ADDED_CALLBACK);

        PendingIntent successCallback = PendingIntent.getBroadcast(context, REQ_CODE_SHORTCUT_ADDED_CALLBACK,
                pinnedShortcutCallbackIntent,  0);


        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, successCallback.getIntentSender());
    }

例如,这是在您的 Activity 中接收广播的代码。请注意,仅当您的应用是 运行、接收器已注册并且用户 允许 快捷方式时,才会调用此 "callback":

private ShortcutAddedReceiver shortcutAddedReceiver;



private void registerShortcutAddedReceiver(){
    if(shortcutAddedReceiver == null){
        shortcutAddedReceiver = new ShortcutAddedReceiver();
    }
    IntentFilter shortcutAddedFilter = new IntentFilter(ShortcutHelper.ACTION_SHORTCUT_ADDED_CALLBACK);
    registerReceiver(shortcutAddedReceiver, shortcutAddedFilter);
}


private void unregisterShortcutAddedReceiver(){
    if(shortcutAddedReceiver != null){
        unregisterReceiver(shortcutAddedReceiver);
        shortcutAddedReceiver = null;
    }
}


@Override
public void onStart() {
    super.onStart();
    registerShortcutAddedReceiver();
}

@Override
public void onStop() {
    super.onStop();
   unregisterShortcutAddedReceiver();
}


private class ShortcutAddedReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Snackbar.make(view, "Shortcut added", Snackbar.LENGTH_LONG).show();
    }
}

希望对您有所帮助!