java—即使在应用程序关闭时也可以监听时区的变化

83qze16e  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(492)

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

我尝试的是:

我找到了一个意图行动。现在是时区改变了。然而,它是一个受保护的意图,只能由系统发送,正如文档所说,而且它可能不允许隐式广播。
我尝试了alarmmanager,但找不到确切的时区更改事件。
我曾经 schedululeAtFixedRate 在应用程序服务的线程中。它工作得很好。但我不想让它听每一个小时的变化,我只想时区的变化,就像我上面提到的。
编辑:
主要活动

  1. public static final String CHANNEL_ID = "49";
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. createNotificationChannel();
  7. startService(new Intent(this,AppService.class));
  8. }
  9. private void createNotificationChannel() {
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  11. CharSequence c_name = getString(R.string.notification_channel_name);
  12. String c_desc = getString(R.string.notification_channel_description);
  13. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  14. NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,c_name,importance);
  15. notificationChannel.setDescription(c_desc);
  16. NotificationManager manager = getSystemService(NotificationManager.class);
  17. manager.createNotificationChannel(notificationChannel);
  18. }
  19. }

显示

  1. <?xml version="1.0" encoding="utf-8"?>
  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  3. <application
  4. android:allowBackup="true"
  5. android:icon="@mipmap/ic_launcher"
  6. android:label="@string/app_name"
  7. android:roundIcon="@mipmap/ic_launcher_round"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme"
  10. android:usesCleartextTraffic="true">
  11. <receiver
  12. android:name=".TimeChangedReceiver"
  13. android:enabled="true"
  14. android:exported="true">
  15. <intent-filter >
  16. <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
  17. </intent-filter>
  18. </receiver>
  19. <service
  20. android:name=".AppService"
  21. android:enabled="true"
  22. android:exported="true" />
  23. <activity android:name=".MainActivity">
  24. <intent-filter>
  25. <action android:name="android.intent.action.MAIN" />
  26. <category android:name="android.intent.category.LAUNCHER" />
  27. </intent-filter>
  28. </activity>
  29. </application>

接收器类别

  1. package com.example.gridview;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import androidx.core.app.NotificationCompat;
  6. import androidx.core.app.NotificationManagerCompat;
  7. public class TimeChangedReceiver extends BroadcastReceiver {
  8. private static int NOTIFICATION_ID = 1;
  9. @Override
  10. public void onReceive(Context context, Intent intent) {
  11. // TODO: This method is called when the BroadcastReceiver is receiving
  12. // an Intent broadcast.
  13. StringBuilder sb = new StringBuilder("Timezone is changed
  14. YAY!,executed for "+NOTIFICATION_ID+" times.");
  15. if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
  16. sb.append("\n Status: Ok.");
  17. }
  18. NotificationCompat.Builder builder = new
  19. NotificationCompat.Builder(context,MainActivity.CHANNEL_ID)
  20. .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  21. .setContentText(sb.toString())
  22. .setSmallIcon(R.drawable.ic_launcher_background)
  23. .setContentTitle("Hey!");
  24. NotificationManagerCompat notificationManager =
  25. NotificationManagerCompat.from(context);
  26. notificationManager.notify(NOTIFICATION_ID,builder.build());
  27. NOTIFICATION_ID++;
  28. }

}

m3eecexj

m3eecexj1#

您应该为注册清单接收者 "android.intent.action.TIMEZONE_CHANGED" (又名 Intent.ACTION_TIMEZONE_CHANGED ). 这是一个隐含的广播例外,不受android8.0+中注册清单接收者的限制。
这样的清单接收器将是在应用程序未运行时检测此类事件的唯一方法。

相关问题