我正在尝试从我的播放器应用程序创建一个主屏幕快捷方式,它将触发特定直播流的播放,而无需打开活动。不幸的是,这不起作用......
ShortcutManager sM = c.getSystemService(ShortcutManager.class);
Intent intent2 = new Intent(c.getApplicationContext(), APPBroadcastReceiver.class);
intent2.setAction("toggle_intent");
intent2.putExtra("id", 1);
ShortcutInfo shortcut2 = new ShortcutInfo.Builder(c,MSG_SHORCUT_CUSTOM)
.setIntent(intent2)
.setShortLabel("ShortLabel")
.setLongLabel("LongLaber")
.setDisabledMessage("DisabledMessage")
.setIcon(Icon.createWithResource(c, R.mipmap.ic_add_outline_short))
.build();
listshortcut.add(shortcut2);
Intent pinnedShortcutCallbackIntent = mShortcutManager.createShortcutResultIntent(shortcut2);
PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, 0);
mShortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
创建快捷方式,但是当我按下它时我得到的是“应用程序未安装”只要我通过活动替换我的Receiver类,活动就会打开。
是否可以触发不会从主屏幕快捷方式打开活动的自定义意图?
不是直接的。据我所知,应用程序快捷方式只能指向Activities
。但你可以制作一个非常薄的Activity
,只是将Intents
转发到你的BroadcastReceiver
:
public class IntentForwardActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent broadcastIntent = new Intent(getIntent());
broadcastIntent.setClassName(getApplicationContext(), APPBroadcastReceiver.class);
sendBroadcast(broadcastIntent);
finish();
}
}
特别是,Activities
不需要布局或调用setContentView()
。