Android InvalidateOptions菜单()不工作

Android InvalidateOptions菜单()不工作,android,notifications,onesignal,Android,Notifications,Onesignal,我想在每次收到来自一个信号通知类的新通知时更新android应用程序上的通知计数标记。流程是,无论何时收到新通知,我都会将计数保存到SharedReference。它已经完成,每次收到通知时,计数徽章都会更新,问题是更新工具栏中的图标时使用的是无效选项菜单。 所以我在MainActivity类中使用了这个方法 public void updateNotif(){ HashMap<String, Integer> notif = notif_count.getCount();

我想在每次收到来自一个信号通知类的新通知时更新android应用程序上的通知计数标记。流程是,无论何时收到新通知,我都会将计数保存到SharedReference。它已经完成,每次收到通知时,计数徽章都会更新,问题是更新工具栏中的图标时使用的是无效选项菜单。 所以我在MainActivity类中使用了这个方法

public void updateNotif(){
    HashMap<String, Integer> notif = notif_count.getCount();
    count = notif.get(NotifCountSession.KEY_COUNT);
    Log.e(MainActivity.class.getSimpleName(), "COUNT : "+count);
    invalidateOptionsMenu();
}
public void updateNotif(){
HashMap notif=notif_count.getCount();
count=notif.get(NotifCountSession.KEY\u count);
Log.e(MainActivity.class.getSimpleName(),“COUNT:+COUNT”);
无效操作菜单();
}
然后我在OneSignal通知类中调用它,如下所示

public class Notification implements OneSignal.NotificationReceivedHandler {

private NotifCountSession session;

private Application app;
private int count;
public Notification(Application app){
    this.app = app;
}

@Override
public void notificationReceived(OSNotification notification) {

    init();
    count++;
    session.saveCount(count);
//this code works
    Log.e(MainActivity.class.getSimpleName(), "COUNT NOTIF : "+count);
//but this one not
    new MainActivity().updateNotif();
}

private void init(){
    session = new NotifCountSession(app);
    HashMap<String, Integer> notif = session.getCount();
    count = notif.get(session.KEY_COUNT);
}
}
公共类通知实现OneSignal.NotificationReceivedHandler{
非公开通报会;
私人应用程序;
私人整数计数;
公共通知(应用程序应用程序){
this.app=app;
}
@凌驾
收到公共作废通知(OSNotification通知){
init();
计数++;
session.saveCount(count);
//此代码有效
Log.e(MainActivity.class.getSimpleName(),“COUNT NOTIF:+COUNT”);
//但这个不是
新建MainActivity().updateNotif();
}
私有void init(){
会话=新的NotifCountSession(应用程序);
HashMap notif=session.getCount();
count=notif.get(session.KEY\u count);
}
}
下面是我的通知计数徽章


如何解决此问题?

按照以下步骤从通知更新

1) 加入你的主要活动

private BroadcastReceiver mMyBroadcastReceiver;
那么

2) 在简历中

@Override
protected void onResume() {
    super.onResume();
    mMyBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateNotif();
        }
    };
    try {
        LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver, new IntentFilter("your_action"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
//还有你的其他代码

3) 然后在onPause中注销

@Override
protected void onPause() {
    super.onPause();


LocalBroadcastManager.getInstance(this).unregisterReceiver(mMyBroadcastReceiver);
}
4) 最后在您的
notificationReceived
方法中

Intent intent = new Intent("your_action");  
LocalBroadcastManager localBroadcastManager =LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);

此行
new main activity().updateNotif()
是一场灾难。永远不要创建活动的实例。它是一个由系统自身实例化的组件。那么如何让它工作呢?我建议您首先阅读
活动
。它是android的主干。为了使它工作,你可以广播一条消息或调用一个静态方法。欢迎兄弟。祝你好运。