все!Я работаю над своим проектом - 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());
}
Служба My Music Foreground:
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);
}
}
Кто-нибудь может мне помочь в моем случае?Я не знаю, почему я получил ошибку.Сначала я попытался запустить службу переднего плана, а затем я хотел добавить текст (обновление).