即使在应用程序关闭时也能监听时区变化
Posted
技术标签:
【中文标题】即使在应用程序关闭时也能监听时区变化【英文标题】:Listen for Time Zone Changes Even When the App is Closed 【发布时间】:2021-03-04 15:38:10 【问题描述】:即使我的 android 应用关闭,我也会尝试监听时区变化。
我尝试了什么:
我为它找到了一个意图操作。现在是 TIME_ZONE_CHANGED。然而它 是一个受保护的意图,只能由系统作为 文档说,而且它可能不允许这样做 隐式广播。
我尝试了 AlarmManager,但找不到确切的时区更改 事件。
我在应用服务的线程中使用了schedululeAtFixedRate
。它工作得很好。
但我不想让它听每小时的变化,我只想要时区时区的变化,就像我上面提到的那样。
编辑:
MainActivity
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);
清单
<?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++;
【问题讨论】:
【参考方案1】:您应该为"android.intent.action.TIMEZONE_CHANGED"
(又名Intent.ACTION_TIMEZONE_CHANGED
)注册一个清单接收器。是implicit broadcast exceptions之一,不受Android 8.0+注册清单接收器的限制。
当您的应用未运行时,这样的清单接收器将是检测此类事件的唯一方法。
【讨论】:
嗨,我试过你的解决方案。我的接收器类没有改变。我正在编辑问题。以上是关于即使在应用程序关闭时也能监听时区变化的主要内容,如果未能解决你的问题,请参考以下文章
为啥在 IntentService 中进行的 BLE 扫描即使在服务被破坏时也能继续工作?