gpt4 book ai didi

android - NotificationManager.notify 锁定手机和Emulator

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:38:38 26 4
gpt4 key购买 nike

问题:

手机/模拟器锁定重复的通知更新。使模拟器在锁定后恢复响应的唯一方法是按给定顺序按 Home => Menu => Lock => Home => Menu Button。

代码:

通知推送代码:

        // Set up notifcation views
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Get notification manager
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.service_notification);
contentView.setViewVisibility(R.id.current_error_text, View.GONE);
contentView.setViewVisibility(R.id.error_text, View.GONE);
contentView.setViewVisibility(R.id.info_error_text, View.GONE);
contentView.setViewVisibility(R.id.info_text, View.GONE);
contentView.setViewVisibility(R.id.next_check_in_text, View.VISIBLE);
contentView.setViewVisibility(R.id.current_profile_text, View.VISIBLE);
contentView.setViewVisibility(R.id.profile_name_text, View.VISIBLE);
contentView.setTextViewText(R.id.next_check_in_text, mainText);
// Set profile text now
contentView.setTextViewText(R.id.profile_name_text, miniText);
// Set up a new notification
Notification notif = new Notification(R.drawable.service_logo_small, "Service is running", System.currentTimeMillis());
notif.contentView = contentView; // Set content view
// Create and plug in the PendingIntent
Intent notifIntent = new Intent(this, EntryPointActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifIntent, 0); // Set up the Pending Intent
notif.contentIntent = pIntent;
// Now set up notification flags
notif.flags |= Notification.FLAG_NO_CLEAR;
notif.flags |= Notification.FLAG_ONGOING_EVENT;
notif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
if(sp.getBoolean("UpdateLights", true)) notif.flags |= Notification.DEFAULT_LIGHTS;
if(sp.getBoolean("UpdateVibrate", true)) notif.flags |= Notification.DEFAULT_VIBRATE;
if(sp.getBoolean("UpdateSound", true)) notif.flags |= Notification.DEFAULT_SOUND;

notificationManager.notify(R.string.app_name, notif);

所有对象都存在并且项目编译完美。我没有遇到 NullPointerExceptions!

调用通知创建函数的代码:

final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
if(( nextUpdateIn - System.currentTimeMillis() ) > 0) {
long milliseconds = (nextUpdateIn - System.currentTimeMillis());
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
String toShow = "Next Check In: " + minutes + " minute" + ((minutes != 1) ? "s" : "") + " " + seconds + " second" + ((seconds != 1) ? "s" : "");
pushNotification(STATE_LOADEDSUCCESSFULLY, currentProfile.getProfileName(), toShow);
} else {
currentState = STATE_RELOADING;
pushNotification(STATE_RELOADING, null, "Refreshing..");
timer.cancel();
}
}
}, 1, 999);

同样,所有对象都存在!上述过程中的通知已更新,但如上所述它会锁定模拟器和手机!

目标:

更新状态栏中的通知以基本上显示下一次刷新前的倒计时。

编辑:

我正在尝试通过SERVICE 运行这段代码,我已经尝试过运行 Android 2.2、2.3.3、4.1 的模拟器,但它们都给我同样的问题!

最佳答案

您用于显示和更新通知的代码工作正常。

在我看来,造成这种情况的唯一原因可能是同时构建了许多计时器。由于初始延迟为 1 毫秒,如果 timer.scheduleAtFixedRate() 是通过某个意外循环调用的,您可能会用数百个通知更新锁定系统。我建议在该行上放置一个断点并找出发生这种情况的原因。

我还有两个建议:

  1. 请记住,Notification 构造函数现已弃用,您可能应该考虑使用通知生成器,例如 Jake Wharton 的 NotificationCompat2 .
  2. 推荐使用ScheduledThreadPoolExecutor而不是 Timer

关于android - NotificationManager.notify 锁定手机和Emulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323892/

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