用于在Android中打开片段的动态快捷方式

Edg*_*xid 2 shortcuts android dynamic android-intent android-fragments

嗨,我正在开发一个有两种类型用户的应用程序:管理员和普通用户; 管理员显然比普通用户有更多的操作,因此我需要使用动态快捷方式,具体取决于创建快捷方式的用户.

在这一点上,一切都已经编纂成了工作.但是,在分配时Intent,即使MainActivity我收到错误.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.motusk.Monit", "com.motusk.Monit.activity.MainActivity"));

shortcut.add(new ShortcutInfo.Builder(this, "ub")
   .setShortLabel("Location")
   .setLongLabel("Location")
   .setIcon(Icon.createWithResource(this, R.drawable.bg))
   .setIntent(intent)
   .build());
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

Intent intent = new Intent(this, MainActivity.class);
Run Code Online (Sandbox Code Playgroud)

但是,两者都得到错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.motusk.Monit/com.motusk.Monit.activity.MainActivity}:java.lang.NullPointerException:必须设置intent的操作

我需要帮助的是:

1)如何创建一个正确IntentMainActivity

2)如何知道按下了哪个快捷方式?(取决于按下哪个快捷键打开某个快捷键的过程Fragment将在中执行MainActivity,这就是为什么我需要知道按下了哪个快捷键).

ian*_*ake 7

根据错误消息,您的Intent必须使用setAction设置操作:

intent.setAction("LOCATION_SHORTCUT");
Run Code Online (Sandbox Code Playgroud)

然后,您可以检查"活动"中的操作:

String action = getIntent() != null ? getIntent().getAction() : null;
if ("LOCATION_SHORTCUT".equals(action)) {
  // Show the correct fragment
}
Run Code Online (Sandbox Code Playgroud)