2016-06-27 8 views
0

私のラジオストリーミングアプリでは、プレイしないようにしたいです。プレイヤーを停止または殺すための通知ボタン

通知にボタンを追加してプレーヤーを停止または強制終了するにはどうすればよいですか?

私はこれまでのところ、これを持っている:

private void initNotification() { 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    PendingIntent intent = PendingIntent.getActivity(StreamService.this, 0, new Intent(this, Radio.class), 0); 
    builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_ricky) 
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)) 
      .setContentTitle("Radio") 
    builder.setContentIntent(intent); 
    builder.setOngoing(true); 
    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

答えて

0

は通知レイアウトに閉じるボタンを追加します。このようないくつかの意図を作成します。

final ComponentName srvComp = new ComponentName(getContext(), service.getClass()); 
Intent close = new Intent(YourRadioService.ACTION_PLAYER_CLOSE); 
close.setComponent(srvComp); 
mPiClose = PendingIntent.getService(getContext(), 4, close, PendingIntent.FLAG_UPDATE_CURRENT); 

例えば、何とか自分のRemoteViewを膨らま:その後

notification.contentView = smallNotification; 

通知上のコンテンツビューを設定し

RemoteViews smallNotification = new RemoteViews(getContext().getPackageName(), R.layout.notification_small); 
smallNotification.setOnClickPendingIntent(R.id.close, mPiClose); 

に通知マネージャを使用通知を提供する

notificationManager.notify(NOTIFICATION_ID, notification); 

インテントを処理することを忘れないでください。オープンソースの音楽プレーヤーがどうやってそれをやっているかをもっと調べる必要がある場合は、こちらをご覧ください。

関連する問題