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());
        }
    }
//..........................................................

}

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)