Android 通知未显示用于秒表的 Java

问题描述

又是我,现在我正在为秒表、闹钟等构建应用程序。 所以我在我的代码添加了服务和通知,但问题是唯一一个完美运行的是主视图,或者我简单地称之为“TimePlayer.java”,这是我的秒表活动。通知没有出现,我已经多次检查我的代码,但不知道我错过了哪一部分。我正在使用目标 API OREO

这是我的服务类:

public class MyServices extends Service implements Propertychangelistener {
    //..........................................................

    private static final int NOTIFICATION_ID = 0;

    private notificationmanager mnotificationmanager;
    private boolean isNotificationShowing;
    private broadcastReceiver receiverStateChanged;
    private broadcastReceiver receiverResets;
    private broadcastReceiver receiverExit;
    private Timer t;
    private Builder mBuilder;

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        isNotificationShowing = false;
        mnotificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
        IntentFilter filterNext = new IntentFilter(ACTION_PLAY);
        receiverStateChanged = new broadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
                    TimeContainer.getInstance().pause();
                } else {
                    TimeContainer.getInstance().start();
                }
                updateNotification();
            }
        };
        registerReceiver(receiverStateChanged,filterNext);
        receiverResets = new broadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                TimeContainer.getInstance().reset();
                updateNotification();
            }
        };
        IntentFilter filterPause = new IntentFilter(ACTION_RESET);
        registerReceiver(receiverResets,filterPause);
        IntentFilter filterPrev = new IntentFilter(ACTION_STOP);
        receiverExit = new broadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                TimeContainer.getInstance().reset();
                stopForeground(true);
                isNotificationShowing = false;
                stopSelf();
            }
        };
        registerReceiver(receiverExit,filterPrev);
        startUpdateTimer();
        TimeContainer.getInstance().isServiceRunning.set(true);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        mBuilder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.timer)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setongoing(true)
                .setonlyAlertOnce(true);
        super.onCreate();
    }


    //..........................................................

    @SuppressWarnings("deprecation")
    private synchronized void updateNotification() {
        RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.custom_navigation);
        if(TimeContainer.getInstance().getCurrentState() == TimeContainer.STATE_RUNNING) {
            contentView.setimageViewResource(R.id.btnPlay,R.drawable.ic_launcher_blackpause_foreground);
        } else {
            contentView.setimageViewResource(R.id.btnPlay,R.drawable.ic_launcher_blackplay_foreground);
        }

        contentView.setTextViewText(R.id.textNotifTime,msToHourMinSec(TimeContainer.getInstance().getelapsedtime()) );

        //..........................................................
        mBuilder.setContent(contentView);

        Intent notificationIntent = new Intent(this,TimePlayer.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this,PendingIntent.FLAG_UPDATE_CURRENT,notificationIntent,0);
        mBuilder.setContentIntent(intent);
        if(isNotificationShowing) {
            mnotificationmanager.notify(NOTIFICATION_ID,mBuilder.getNotification());
        } else {
            isNotificationShowing = true;
            startForeground(NOTIFICATION_ID,mBuilder.getNotification());
        }
    }
//..........................................................

}

如果您有任何解决方案,请帮助我。无论如何,提前致谢

解决方法

终于,4个小时后,我解决了自己的问题。事情就是这样,我的通知已被完全弃用(XD 抱歉)。我什至没有注意到这一点。我的应用程序在 targetSdk 26 和 minSdk 23 上运行,这意味着我上面的代码将不起作用,因为大多数功能都已弃用,这就是为什么我必须为 minSdk 添加一些抑制。最后但并非最不重要的是,最后我必须添加 Notification Compat 和 Notification Channel 来调用通知。这是由 Builder 本身引起的。我使用了带有 Builder 的自变量,它应该改为调用 Notification Compat。

所以这是我到目前为止所做的更改:

    private NotificationCompat.Builder mBuilder;
            //..................
    NotificationChannel mChannel = new NotificationChannel(notifyID,"name",importance);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mNotificationManager.createNotificationChannel(mChannel);
        }
            //..................

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //..................
            mBuilder.setSmallIcon(R.drawable.timer);
            mBuilder.setContentIntent(intent);
            mBuilder.setChannelId(notifyID);
            mBuilder.build();
        }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...