即使应用程序关闭,也要监听时区变化



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

我尝试过的:

  1. 我为它找到了一个意向动作。它是TIME_ZONE_CHANGED。然而是受保护的意图,只能由系统作为文件上说,而且可能不允许隐含广播。

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

  3. 我在一个应用程序服务的线程中使用了schedululeAtFixedRate。它运行得很好。但我不想让它听每一个小时的变化,我只想听时区的变化,就像我上面提到的那样。


编辑:

主要活动

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++;
}

}

您应该为"android.intent.action.TIMEZONE_CHANGED"(又名Intent.ACTION_TIMEZONE_CHANGED(注册一个清单接收器。这是一个隐含的广播例外,不受安卓8.0+中注册清单接收器的限制。

当你的应用程序没有运行时,像这样的清单接收器将是检测这种类型事件的唯一方法。