我昨晚将操作系统版本更新为 android 10,从那时起,广播接收器中的 startActivity 函数就什么也不做。这就是我尝试根据 CommonsWare 的回答开始活动的方式:
Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
Log.d("Debug", "This is android 10");
// Start the alert via full-screen intent.
PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "my_channel_02";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.notification_channel_name_second),
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(startAlarmPendingIntent, true);
Log.d("Debug", "Try to load …
Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-pendingintent start-activity android-10.0
我尝试使用下一个代码为广播接收器启动活动
Intent i = new Intent(context, AlarmNotification.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
"Whatever", NotificationManager.IMPORTANCE_HIGH));
}
mgr.notify(NOTIFY_ID, buildNormal(context, i).build());
}
private NotificationCompat.Builder buildNormal(Context context, Intent intent) {
NotificationCompat.Builder b=
new NotificationCompat.Builder(context, CHANNEL_WHATEVER);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(TEXT)
.setContentText(TEXT)
.setFullScreenIntent(buildPendingIntent(context, intent), true);
return(b);
}
private PendingIntent buildPendingIntent(Context context, Intent intent) {
return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
Run Code Online (Sandbox Code Playgroud)
一开始,一切正常。但是如果我进入应用程序设置,关闭CHANNEL_WHATEVER的通知通道,然后再打开它。稍后当我调用 NotificationManager.notify 时,它会在通知抽屉中显示通知,但不会启动活动。如果我删除该应用程序并重新安装,它又可以正常工作了。这是我应该报告的 android 10 …
android broadcastreceiver android-intent android-pendingintent android-10.0
我构建了一个待办事项列表应用程序,该应用程序应该显示通知以提醒任务。为了能够将通知安排在截止日期的准确时间,我将通知数据从 flutter 传递到 kotlin,并显示来自广播接收器的通知。
这里我将通知数据发送到 kotlin:
await platform.invokeMethod('setNextNotification', {
'tasksNames': tasksNames,
'notificationsTimeInMillis': notificationsTimeInMillis
});
Run Code Online (Sandbox Code Playgroud)
这就是我在 FlutterActivity 中获取数据的方式:
private const val CHANNEL = "flutter.native/helper"
class MainActivity : FlutterActivity() {
companion object {
const val TASKS_NAMES_EXTRA = "tasksNames"
const val NOTIFICATIONS_TIME_IN_MILLIS_EXTRA = "notificationsTimeInMillis"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
// Init the AlarmManager.
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
// We got here from the setNotifications() method in flutter...
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "setNextNotification") { …
Run Code Online (Sandbox Code Playgroud) 到目前为止,我使用的是动态字符串,如本文的解决方案所示: Flutter 国际化 - 动态字符串
下面是一个例子:
AppLocalizations.of(context).userAge(18)
Run Code Online (Sandbox Code Playgroud)
在 AppLocalizations.dart 上:
userAge(age) => Intl.message(
"My age is $age",
name: "userAge",
args: [age]);
// Return "My age is 18"
Run Code Online (Sandbox Code Playgroud)
但后来我读了这篇关于颤振国际化的文章:https : //medium.com/flutter-community/flutter-internationalization-the-easy-way-using-provider-and-json-c47caa4212b2 其中展示了如何使用 json 文件进行本地化字符串的资源文件。它看起来更方便,所以我更喜欢使用这种方法,但不知道如何从具有动态值的 json 文件中获取字符串。
有什么解决办法吗?
每当用户单击设备的后退按钮时,我需要更改子小部件内的变量值并调用setState()
. 问题是,当我单击后退按钮时,将调用父窗口小部件的 onWillPopScope() ,而不是子窗口小部件的 onWillPopScope() 。因此,我需要从父级更改子小部件状态的变量值并setstate()
在子小部件内部调用。或者,如果有一种方法可以调用子窗口小部件的 onWillPopScope,它也可以解决我的问题。有什么我可以做的吗?
我正在 kotlinlang.org 上阅读有关流程的文章:https ://kotlinlang.org/docs/flow.html
他们展示了下一个例子:
fun simple(): Flow<Int> = flow {
println("Flow started")
for (i in 1..3) {
delay(100)
emit(I)
}
}
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) }
println("Calling collect again...")
flow.collect { value -> println(value) }
}
Run Code Online (Sandbox Code Playgroud)
他们说输出是:
Calling simple function...
Calling collect...
Flow started
1
2
3
Calling collect again...
Flow started
1
2
3
Run Code Online (Sandbox Code Playgroud)
因此,UI 线程似乎正在等待第一个flow.collect
函数完成,然后再继续打印“再次调用收集...”
我希望当第一个流程构建器运行时,系统将打印“再次调用收集...”,因此输出将是:
Calling simple function... …
Run Code Online (Sandbox Code Playgroud) android ×3
flutter ×3
android-10.0 ×2
dart ×1
flutter-intl ×1
json ×1
kotlin ×1
kotlin-flow ×1