0

又是我,现在我正在为秒表、闹钟等构建应用程序。所以我在我的代码中添加了服务和通知,但问题是唯一能完美运行的是主视图,或者只是我称之为“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

1 回答 1

0

最后,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();
        }
于 2021-01-11T15:16:46.130 回答