我的收音机应用程序上有正在进行的通知,我已经设法从通知中启动了我的主要活动,但现在我正在尝试添加按钮来停止流媒体服务。
这是我在服务中的通知方法:
@SuppressWarnings("deprecation")
private void createNotification() {
int myIconId = R.drawable.ic_pause;
Intent mIntent = new Intent(context, RadiophonyService.class);
Notification notification;
Bundle bundle = new Bundle();
bundle.putInt("list", list);
PendingIntent stopIntent = PendingIntent.getService(context, 0, mIntent, 0) ;
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(getApplicationContext(), MainActivity.class)
.putExtras(bundle), PendingIntent.FLAG_UPDATE_CURRENT);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
notification = new Notification();
notification.tickerText = station.getRadioName();
notification.icon = R.mipmap.ic_launcher;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
} else {
notification = new NotificationCompat.Builder(context)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(station.getRadioName())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(
BitmapFactory.decodeResource(
`enter code here` context.getResources(),
R.mipmap.ic_launcher))
.addAction(myIconId, "STOP", stopIntent)
.setOngoing(true).setContentIntent(pi).build();
}
startForeground(NOTIFICATION_ID, notification);
}
这是我的OnStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.getAction()!=null && intent.getAction().equals("STOP")){
stopSelf();
return START_NOT_STICKY;
}
else {
task = new ProgressTask();
task.execute();
return START_NOT_STICKY;
}
}
如何停止服务流?