gpt4 book ai didi

java - 即使在应用程序关闭时也能监听时区变化

转载 作者:行者123 更新时间:2023-12-04 15:12:37 24 4
gpt4 key购买 nike

即使我的 Android 应用关闭,我也在尝试监听时区变化

我尝试了什么:

  1. 我找到了它的 Intent 操作。现在是 TIME_ZONE_CHANGED。然而它是一个 protected Intent ,只能由系统作为文件说,而且它可能不允许这样做隐式广播。

  2. 我试过 AlarmManager,但找不到确切的时区更改事件。

  3. 我在应用服务的线程中使用了 schedululeAtFixedRate。它工作得很好。但我不希望它听到每小时的变化,我只想要时区时区变化,正如我上面提到的。


编辑:

主 Activity

public static final String CHANNEL_ID = "49";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
startService(new Intent(this,AppService.class));
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence c_name = getString(R.string.notification_channel_name);
String c_desc = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,c_name,importance);
notificationChannel.setDescription(c_desc);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}

list

<?xml version="1.0" encoding="utf-8"?>

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<receiver
android:name=".TimeChangedReceiver"
android:enabled="true"
android:exported="true">

<intent-filter >
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
</intent-filter>

</receiver>

<service
android:name=".AppService"
android:enabled="true"
android:exported="true" />

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

接收类

package com.example.gridview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class TimeChangedReceiver extends BroadcastReceiver {

private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
StringBuilder sb = new StringBuilder("Timezone is changed
YAY!,executed for "+NOTIFICATION_ID+" times.");
if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
sb.append("\n Status: Ok.");
}

NotificationCompat.Builder builder = new
NotificationCompat.Builder(context,MainActivity.CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentText(sb.toString())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Hey!");
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID,builder.build());
NOTIFICATION_ID++;
}

最佳答案

您应该为 "android.intent.action.TIMEZONE_CHANGED"(又称 Intent.ACTION_TIMEZONE_CHANGED)注册一个 list 接收器。它是 implicit broadcast exceptions 之一不受在 Android 8.0+ 中注册 list 接收器的限制。

当您的应用未运行时,像这样的 list 接收器将是检测此类事件的唯一方法。

关于java - 即使在应用程序关闭时也能监听时区变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64942722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com