2016-08-20 58 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的