如何在广播接收器中启动片段

Adn*_*aki 1 android broadcastreceiver android-fragments

我怎样才能使用广播接收器来启动/午餐片段:例如:如果我需要启动/关闭活动,我可以使用意图:

public void onReceive(final Context context, Intent intent) {

    this.context = context;
    this.intent = intent;

    try {
        Bundle bundle = intent.getExtras();

        int messageID = bundle.getInt("id");

        intent = new Intent(context, GetAlarm.class);

        intent.putExtra("id",messageID);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    } catch (Exception e) {
        Toast.makeText(context, "There was an error ", Toast.LENGTH_SHORT)
                .show();
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不知道片段怎么样

小智 5

你必须保持意图和putexterasendactivity.然后你可以从活动中启动片段.