我正在开发一个应用程序,并且想播放背景音乐,因此我使用服务来播放和停止。它运作完美。另外,我想显示暂停,播放...的通知,但该通知未出现在Android 8.0上。 我使用andorid 3.0.1,compileSdkVersion = 26,targetSdkVersion = 26 ,minSdkVersion = 15
这是我当前的代码
public class BackgroundSoundService extends Service {
private static final String TAG = "BackgroundSoundService";
MediaPlayer player;
int length;
static int t;
private static final String LOG_TAG = "ForegroundService";
String channelId = "channel-01";
Notification notification;
NotificationChannel mChannel;
String name = "volume";
int id = 0;
int importance = NotificationManager.IMPORTANCE_HIGH;
public IBinder onBind(Intent arg0) {
Log.i(TAG, "onBind()");
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.jorgesys_song);
....
}
@RequiresApi(api = Build.VERSION_CODES.O)
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent previousIntent = new Intent(this, BackgroundSoundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);
Intent playIntent = new Intent(this, BackgroundSoundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
Intent nextIntent = new Intent(this, BackgroundSoundService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getService(this, 0,
nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.esp);
notification = new NotificationCompat.Builder(this)
.setContentTitle("Truiton Music Player")
.setTicker("Truiton Music Player")
.setContentText("My Music")
.setSmallIcon(R.drawable.esp)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX).setWhen(0)
.addAction(R.drawable.ic_per,
"", ppreviousIntent)
.addAction(R.drawable.ic_play, "",
pplayIntent)
.addAction(R.drawable.ic_next, "",
pnextIntent).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, name,
importance);
mChannel.setShowBadge(true);
NotificationManager mNotifyMgr =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.createNotificationChannel(mChannel);
// Builds the notification and issues it.
mNotifyMgr.notify(id, notification);
}
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i(LOG_TAG, "Clicked Previous");
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i(LOG_TAG, "Clicked Next");
} else if (intent.getAction().equals(
Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
player.start();
return Service.START_STICKY;
}
还有另一个问题: 在通知中,我有三个按钮,并为它们添加了3张图像,但是它们的背景没有改变。 预先感谢
答案 0 :(得分:0)
在有条件的情况下添加此行
notification.setChannelId(mChannel);
喜欢这个:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, name,
importance);
mChannel.setShowBadge(true);
notification.setChannelId(mChannel);//add this line
NotificationManager mNotifyMgr =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.createNotificationChannel(mChannel);
// Builds the notification and issues it.
mNotifyMgr.notify(id, notification);
}
答案 1 :(得分:0)
请尝试以下操作:
UIImage
还请在运行时添加 RECEIVE_SMS 权限。
答案 2 :(得分:0)
来自Android官方网站:
以Android 8.0(API级别26)为目标时,您必须实现 更多的通知渠道。如果您的targetSdkVersion设置为25或 较低,当您的应用在Android 8.0(API级别26)或更高版本上运行时, 行为与运行Android 7.1(API级别)的设备相同 25)或更低。
因为您的targetSdkVersion
是26,所以将代码更改为:
notification = new NotificationCompat.Builder(this, channelId) // TODO: Add channelId here
.setContentTitle("Truiton Music Player")
.setTicker("Truiton Music Player")
.setContentText("My Music")
.setSmallIcon(R.drawable.esp)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX).setWhen(0)
.addAction(R.drawable.ic_per,
"", ppreviousIntent)
.addAction(R.drawable.ic_play, "",
pplayIntent)
.addAction(R.drawable.ic_next, "",
pnextIntent).build();
// TODO: Move this statement out of if block
NotificationManager mNotifyMgr =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, name,
importance);
mChannel.setShowBadge(true);
mNotifyMgr.createNotificationChannel(mChannel);
}
// TODO: Move this statement out of if block too.
// Builds the notification and issues it.
mNotifyMgr.notify(id, notification);