Search code examples
flutterlockscreenbackground-serviceandroid-lockscreenwidget

Is it possible to show a specific page of the Flutter application when the phone screen is locked due to a specific event?


I implemented a program that when a new call is made, a function is called and a special page of the application is opened.

When the screen is locked, the problem I have is that the Android system does not allow the function to run before the screen is unlocked.

Finally, my question leads here:

Does anyone have experience in running the app or opening a specific page of the Flutter application at a specific time without unlocking the screen?

is this possible?

@pragma('vm:entry-point')
Future<void> phoneStateBackgroundCallbackHandler(
  PhoneStateBackgroundEvent event,
  String number,
  int duration,
) async {
  switch (event) {
    case PhoneStateBackgroundEvent.incomingstart:
      print('Incoming call start, number: $number, duration: $duration s');
      openAppWithStartCall(number, duration);

      break;
    case PhoneStateBackgroundEvent.incomingmissed:
      break;
    case PhoneStateBackgroundEvent.incomingreceived:
      break;
    case PhoneStateBackgroundEvent.incomingend:
      openAppWithEndCall(number, duration);
      break;
    case PhoneStateBackgroundEvent.outgoingstart:
      print('Ougoing call start, number: $number, duration: $duration s');
      break;
    case PhoneStateBackgroundEvent.outgoingend:
      print('Ougoing call ended, number: $number, duration: $duration s');
      break;
  }
}

and openAppWithStartCall:


openAppWithStartCall(String phone, int duration) async {
  debugPrint('Action:android.intent.action.whenNewCall');
  AndroidIntent intent = AndroidIntent(
    action: 'android.intent.action.whenNewCall',
    package: 'com.example.flutter_application_1',
    data: Uri.encodeFull(
        'MyApplicationId://search?phone=$phone&duration=$duration'),
  );

  await intent.launch();
}

When the phone screen is not locked, the above functions are executed and in fact the desired intent is executed.

If it is not possible to execute the same intent when the screen is locked, how can the same content be shown without unlocking the screen?


Solution

  • I have added this line in AndroidManifes.xml, in androiod > 10 we can access "open over other apps" and this works for me.

        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />