Andris
Andris

Reputation: 4193

Launch app from service (Flutter: Android)

I have created plugin in Flutter, to show chat heads, like Facebook Messenger does it. From Chat Head i must open specific screen. I don't know how to launch flutter app from my BroadcastReceiver. So far i have this code:

  private BroadcastReceiver createReceiver(final EventChannel.EventSink eventSink){
    return new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        if(b != null) {
          HashMap<String, Object> data = (HashMap<String, Object>) b.getSerializable(KEY_DATA);
          boolean showWhenLocked = data.get("SHOW_WHEN_LOCKED") == null ? false : (boolean) data.get("SHOW_WHEN_LOCKED");

          if(showWhenLocked) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
              mActivity.setShowWhenLocked(true);
              mActivity.setTurnScreenOn(true);
            } else {
              Window window = mActivity.getWindow();
              window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
              window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
              window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            }
          }

          Log.d("ChatheadsPlugin", "BroadcastReceiver onReceive intent for eventSink: " + data);
          eventSink.success(data);
        }
      }
    };
  }

Firstly i want to launch app main Activity here. And if it is possible, i want to launch it with 'SHOW_WHEN_LOCKED' possibility!

Upvotes: 1

Views: 570

Answers (1)

Andris
Andris

Reputation: 4193

I was able to launch flutter activity from Android this way:

  1. At first I in my plugin added MainActivity which is overriding FlutterActivity.

  2. Then i created new intent with flag to create new activity

    Intent intent = new Intent(MyService.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

  3. Then started activity as normally we do that

    startActivity(intent);

Upvotes: 2

Related Questions