可以在手机屏幕因特定事件而锁定时,显示Flutter应用程序的特定页面吗?

huangapple go评论65阅读模式
英文:

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.

当屏幕被锁定时,我的问题是Android系统不允许在解锁屏幕之前运行该函数。

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?

有人有在不解锁屏幕的情况下,在特定时间运行应用程序或打开Flutter应用程序的特定页面的经验吗?

is this possible?

这是否可能?

(Note: I've provided translations for the non-code text as requested. If you need translations for the code comments or variable names, please let me know.)

英文:

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?

答案1

得分: 0

我已在AndroidManifest.xml中添加了这一行:

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

在Android 10及以上版本中,我们可以访问“在其他应用程序上方打开”,这对我有效。

英文:

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

    &lt;uses-permission android:name=&quot;android.permission.SYSTEM_ALERT_WINDOW&quot; /&gt;

huangapple
  • 本文由 发表于 2023年6月26日 21:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定