如何更新通知前台服务?

时间:2019-04-13 18:28:56

标签: java android foregroundnotification

大家!我正在我的项目-MusicApp上,我想知道如何更新前台服务?我阅读了帖子,但出现错误:

2019-04-13 21:11:31.351 19279-19279/com.example.musicapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.musicapp, PID: 19279
java.lang.NullPointerException: class name is null
    at android.content.ComponentName.<init>(ComponentName.java:116)
    at android.app.Service.startForeground(Service.java:695)
    at com.example.musicapp.MusicForegroundService.startForeground(MusicForegroundService.java:119)
    at com.example.musicapp.activities.MainActivity$14.playSong(MainActivity.java:586)
    at com.example.musicapp.MusicService.playSong(MusicService.java:93)
    at com.example.musicapp.activities.MainActivity$3.onItemClickListener(MainActivity.java:315)
    at com.example.musicapp.adapters.SongsRecyclerViewAdapter$SongsViewHolder$1.onClick(SongsRecyclerViewAdapter.java:83)
    at android.view.View.performClick(View.java:7339)
    at android.view.View.performClickInternal(View.java:7305)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27788)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7073)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

我的MainActivity类中有下一个代码:

 mForegroundService = new MusicForegroundService(MainActivity.this);
 if (!isForegroundServiceRunning(MusicForegroundService.class)) {
                // first time, create the foreground service
                mForegroundService.startForeground();
                mForegroundService.updateForegroundNotification(mCurrentSong.getTitle(), mCurrentSong.getArtist());
            } else {
                mForegroundService.updateForegroundNotification(
                        mCurrentSong.getTitle(),
                        mCurrentSong.getArtist());
            }

我的音乐前景服务:

public class MusicForegroundService extends Service {
private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "channel_id_01";
private Context mContext;
private NotificationManager mNotificationManager;
private Notification mNotification;
private RemoteViews mRemoteViews;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public MusicForegroundService(Context context) {
    mContext = context;
}

public void startForeground() {
    mNotificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    // create the notification channel
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(
                CHANNEL_ID,
                "Music foreground service",
                NotificationManager.IMPORTANCE_LOW);
        notificationChannel.enableVibration(false);
        notificationChannel.enableLights(false);

        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
    }

    Intent notificationIntent = new Intent();
    PendingIntent notificationPendingIntent = PendingIntent.getActivity(
            mContext,
            MUSIC_SERVICE_REQUEST_CODE,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mRemoteViews =
            new RemoteViews(mContext.getPackageName(), R.layout.partial_music_notification);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            mContext,
            CHANNEL_ID);
    builder.setAutoCancel(false)
            .setCustomContentView(mRemoteViews)
            .setTicker(mContext.getString(R.string.app_name))
            .setContentIntent(notificationPendingIntent)
            .setSmallIcon(R.drawable.ic_stat_notify_music);
    mNotification = builder.build();
    startForeground(NOTIFICATION_ID, mNotification);
}

// update text
public void updateForegroundNotification(String title, String artist) {
    mRemoteViews.setTextViewText(R.id.text_title_notification, title);
    mRemoteViews.setTextViewText(R.id.text_info_notification, artist);

    mNotificationManager.notify(NOTIFICATION_ID, mNotification);
  }
}

有人可以帮助我吗?我不知道为什么会出错。最初,我尝试启动前台服务,然后想添加文本(更新)。

0 个答案:

没有答案