Я сделал медиаплеер. Теперь я хочу поместить один XML-файл в область уведомлений. Для этого я следовал за этим потоком, но я получил NullPointerException
при запуске уведомления.
1. Добавить службу в файл манифеста Android
service class=".MDService"
android:process=":remote"
android:name=".MDService"
2. Создать MDSInterface.aidl
следующим образом
interface MDSInterface {
void start();
void stop();
}
3. Сделан класс MDService
для удаленного просмотра, и в этом я добавляю файл XML и добавляю в область уведомлений. Ниже приведен код
package org.streamingmusic.main;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.RemoteViews;
public class MDService extends Service
{
public static NotificationManager nm;
private static final int NOTIFY_ID = 1;
@Override
public void onCreate() {
super.onCreate();
nm =(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onDestroy() {
nm.cancel(NOTIFY_ID);
}
/**
* Set notification on play song
* @param Trackname
* @param Artistname
*/
public void startnot() {
Notification notification = new Notification(R.drawable.icon, "SMS", 0);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.mindback);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(getApplicationContext(), StreamingPlayer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.contentIntent=contentIntent;
nm.notify(NOTIFY_ID, notification);
}
public IBinder getBinder() {
return mBinder;
}
public final MDSInterface.Stub mBinder = new MDSInterface.Stub() {
public void stop() throws RemoteException {
// TODO Auto-generated method stub
nm.cancel(NOTIFY_ID);
}
public void start() throws RemoteException {
// TODO Auto-generated method stub
startnot();
}
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
}
4. Объявление объекта класса MDService
в активности игрока
public static MDSInterface mpInterface;
5. Привязать услугу
getApplicationContext().bindService(new Intent(
getApplicationContext(), MDService.class),
mConnection, Context.BIND_AUTO_CREATE);
6. Добавить метод подключения службы
public static ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
mpInterface = MDSInterface.Stub.asInterface((IBinder)service);
}
public void onServiceDisconnected(ComponentName name) {
mpInterface = null;
}
};
Пожалуйста, кто-нибудь может сказать мне, что мне не хватает?
На самом деле, моя цель - предоставить функциональность в области уведомлений, чтобы пользователь мог воспроизвести следующую или предыдущую песню, приостановить песню, а также перейти в приложение.
Это означает добавление четырех кнопок в области уведомлений. Если у кого есть способ получше, то поделитесь пожалуйста. Я ценю это.
Спасибо.