2016-08-20 132 views
0

这里是我的代码通知: -如何将操作添加到我的通知按钮?

public void showNotification(String Name, String Rate, int Image_Source, int PandP, int Repeat) { 

    RemoteViews remoteview = new RemoteViews(getPackageName(), R.layout.notification_layout); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SongsListActivity.class), 0); 
    notification = new NotificationCompat.Builder(this) 
      .setContent(remoteview) 
      .setPriority(2) 
      .setTicker(NameD.getText()) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(NameD.getText()) 
      .setContentText(RateD.getText()) 
      .setContentIntent(pi) 
      .setAutoCancel(false) 
      .setCustomBigContentView(remoteview) 
      .build(); 

    notification.bigContentView = remoteview; 
    remoteview.setImageViewResource(R.id.Repeat_N, Repeat); 
    remoteview.setImageViewResource(R.id.P_and_P_N, PandP); 
    remoteview.setTextViewText(R.id.Name_N, Name); 
    remoteview.setTextViewText(R.id.Rate_N, Rate); 
    remoteview.setImageViewResource(R.id.Image_N, Image_Source); 
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(111111, notification); 
} 

这里是我的通知XML: - 我通知

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#2196F3"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="#2196F3" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/Image_N" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     tools:ignore="ContentDescription" /> 

    <LinearLayout 
     android:id="@+id/text_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/Name_N" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="bottom" 
      android:textAppearance="?android:textAppearanceMedium" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" 
      android:maxLength="25"/> 

     <TextView 
      android:id="@+id/Rate_N" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="top" 
      android:textAppearance="?android:textAppearanceMedium" 
      android:textColor="#F44336" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <ImageButton 
       android:id="@+id/Repeat_N" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_repeat_white_48dp" 
       android:background="#2196F3"/> 

      <ImageButton 
       android:id="@+id/Previous_N" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_skip_previous_white_48dp" 
       android:background="#2196F3"/> 

      <ImageButton 
       android:id="@+id/P_and_P_N" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_play_arrow_white_48dp" 
       android:background="#2196F3"/> 

      <ImageButton 
       android:id="@+id/Next_N" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_skip_next_white_48dp" 
       android:background="#2196F3"/> 


     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

布局

现在如何添加动作到通知中的4个按钮,以便: -

a)Pause图像按钮暂停MediaPlayer mediaPlayer,然后将其更改为播放图像。

b)中的下一个和前一个Imagebuttons调用一个方法媒体播放器(INT I)

c)该Repet ImageButton的调用方法changeRepeat()

+0

http://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification也许这个帮助你 – Tony

+0

看到这个:http://stackoverflow.com/questions/15350998/determine-addaction-click - 用于-Android的通知 – KrishnaJ

回答

0

对于添加的侦听到远程视图

remoteViews.setOnClickPendingIntent(R.id.someView, pendingIntent); 
+0

好吧,假设我加你到我的按钮,暂停song.Now又有什么未决意向做的说。 –

+0

待定意图通过点击暂停按钮触发。 待定意图可能开始接收器或服务或活动取决于你如何让待定的意图实例。 更多信息: https://developer.android.com/reference/android/app/PendingIntent.html –

0

您可以使用remoteViews.setOnClickPendingIntent(R.id.someView, pendingIntent);与BroadCastReceiver结合使用以及建立如下通知:

private void showNotification(){private void showNotification(){ 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 

    PendingIntent pendIntent = PendingIntent.getActivity 
      (this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // notification's layout 
    RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small); 
    mRemoteViews.setImageViewResource(R.id.notif_icon, R.mipmap.ic_launcher); 
    mRemoteViews.setTextViewText(R.id.notif_title, "playMusic"); 
    mRemoteViews.setTextViewText(R.id.notif_content, "pauseMusic"); 

    Intent intentPlay = new Intent(this, MyReceiver.class); 
    intentPlay.setAction(Constants.PLAY_ACTION); 
    PendingIntent pIntentPlay = PendingIntent.getBroadcast 
      (this, Constants.PENDING_PLAY_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent intentPause = new Intent(this, MyReceiver.class); 
    intentPause.setAction(Constants.PAUSE_ACTION); 
    PendingIntent pIntentPause = PendingIntent.getBroadcast 
      (this, Constants.PENDING_PAUSE_CODE, intentPause, PendingIntent.FLAG_UPDATE_CURRENT); 

    mRemoteViews.setOnClickPendingIntent(R.id.notif_title, pIntentPlay); 
    mRemoteViews.setOnClickPendingIntent(R.id.notif_content, pIntentPause); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    mBuilder.setSmallIcon(R.drawable.icon_play) 
      .setAutoCancel(false) 
      .setOngoing(true) 
      .setContentIntent(pendIntent) 
      .setContent(mRemoteViews) 
      .setTicker("musicStarted"); 

    startForeground(NOTIFY_ID, mBuilder.build()); 
} 
private void showNotification(){private void showNotification(){ 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 

    PendingIntent pendIntent = PendingIntent.getActivity 
      (this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // notification's layout 
    RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small); 
    mRemoteViews.setImageViewResource(R.id.notif_icon, R.mipmap.ic_launcher); 
    mRemoteViews.setTextViewText(R.id.notif_title, "playMusic"); 
    mRemoteViews.setTextViewText(R.id.notif_content, "pauseMusic"); 

    Intent intentPlay = new Intent(this, MyReceiver.class); 
    intentPlay.setAction(Constants.PLAY_ACTION); 
    PendingIntent pIntentPlay = PendingIntent.getBroadcast 
      (this, Constants.PENDING_PLAY_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent intentPause = new Intent(this, MyReceiver.class); 
    intentPause.setAction(Constants.PAUSE_ACTION); 
    PendingIntent pIntentPause = PendingIntent.getBroadcast 
      (this, Constants.PENDING_PAUSE_CODE, intentPause, PendingIntent.FLAG_UPDATE_CURRENT); 

    mRemoteViews.setOnClickPendingIntent(R.id.notif_title, pIntentPlay); 
    mRemoteViews.setOnClickPendingIntent(R.id.notif_content, pIntentPause); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    mBuilder.setSmallIcon(R.drawable.icon_play) 
      .setAutoCancel(false) 
      .setOngoing(true) 
      .setContentIntent(pendIntent) 
      .setContent(mRemoteViews) 
      .setTicker("musicStarted"); 

    startForeground(NOTIFY_ID, mBuilder.build()); 
} 
在广播接收器触发方法

你需要管理的媒体播放器,在我来说,我有一个绑定到MainActivity服务,所以我通过接口连接主要和广播接收器,当MainActivity的方法触发其管理服务,服务管理是MediaPlayer的