每天更改android个状态栏通知
Change android status bar notification every day
我想在状态栏上创建一个持续的通知(比如当前电池百分比 - 它总是在那里),但图标将在 00:00 每天更改。
此外,我希望此通知会在设备重启后自动提醒,而不是仅在用户打开应用程序后提醒。
我在MainActivity中创建了通知,但我不知道如何让它在重启后提醒,也不知道如何在每天半夜更改它。
我尝试创建 AlarmService 和广播接收器,但它 运行 每 5 秒一次,太多了,所以我删除了它。
我当前的代码是:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.notificationTitle))
.setContentText(....)
.setLargeIcon(largeIconBitmap)
.setSmallIcon(getResources().getIdentifier("icon_status_" + myHelper.getCurrentImageNumber(),"drawable", getPackageName())); // change the icon at mid-night. 'getCurrentImageNumber' should get me the right icon number
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mylHelper.myRequestCode, notification);
现在我看到了通知,但图标没有变化,重启后也没有报警。我在 Whosebug 中看到了一些答案,但没有任何帮助。
我不会给你所有的代码,你需要边做边学。
这是你应该做的:
1 使用服务来启动您的通知并维护更新它的逻辑。如果通知已存在,您可以更新现有通知或以编程方式将其关闭并替换为新通知。
2 在 AndroidManifest.xml 中使用多个 Intent 过滤器注册广播接收器以启动您的服务。
<receiver android:name=".myReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3 创建扩展广播接收器 class 的 class 并使用和启动上述服务。
我想在状态栏上创建一个持续的通知(比如当前电池百分比 - 它总是在那里),但图标将在 00:00 每天更改。
此外,我希望此通知会在设备重启后自动提醒,而不是仅在用户打开应用程序后提醒。
我在MainActivity中创建了通知,但我不知道如何让它在重启后提醒,也不知道如何在每天半夜更改它。
我尝试创建 AlarmService 和广播接收器,但它 运行 每 5 秒一次,太多了,所以我删除了它。
我当前的代码是:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.notificationTitle))
.setContentText(....)
.setLargeIcon(largeIconBitmap)
.setSmallIcon(getResources().getIdentifier("icon_status_" + myHelper.getCurrentImageNumber(),"drawable", getPackageName())); // change the icon at mid-night. 'getCurrentImageNumber' should get me the right icon number
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mylHelper.myRequestCode, notification);
现在我看到了通知,但图标没有变化,重启后也没有报警。我在 Whosebug 中看到了一些答案,但没有任何帮助。
我不会给你所有的代码,你需要边做边学。
这是你应该做的:
1 使用服务来启动您的通知并维护更新它的逻辑。如果通知已存在,您可以更新现有通知或以编程方式将其关闭并替换为新通知。
2 在 AndroidManifest.xml 中使用多个 Intent 过滤器注册广播接收器以启动您的服务。
<receiver android:name=".myReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3 创建扩展广播接收器 class 的 class 并使用和启动上述服务。