我的電臺應用程序有一個持續通知,我已經設法從通知開始我的主要活動,但現在我試圖添加一個按鈕來停止流媒體服務。停止服務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