我的电台应用程序有一个持续通知,我已经设法从通知开始我的主要活动,但现在我试图添加一个按钮来停止流媒体服务。停止服务PendingIntent
这里面的服务我notifaction方法:
@SuppressWarnings("deprecation")
private void createNotification() {
int myIconId = R.drawable.ic_pause;
Intent mIntent = new Intent(context, StreamingService.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);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification();
notification.tickerText = station.getRadioName();
notification.icon = R.mipmap.ic_launcher;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(),
station.getRadioName(), null, pi);
} else {
notification = new NotificationCompat.Builder(context)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(station.getRadioName())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(
BitmapFactory.decodeResource(
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) {
task = new ProgressTask();
task.execute();
return START_NOT_STICKY;
}
我做的意图和的PendingIntent启动该服务,但我怎么能设置使用PendingIntent服务stopSelf()? 我被困在这个好几天了,在此先感谢您的帮助。
没有你的尝试,如果(intent.getAction()等于( “STOP”) )stopSelf();在你的onStartCommand – JRowan
谢谢你,那就是我错过的。我不能相信我被困在这个天xD –
它碰巧发生在我们最好的时候,当你走了它几天它刚刚发生,哈哈,我把它作为答案,所以你可以标记它 – JRowan