2016-08-20 10 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)MediaPlayerメディアプレーヤーを一時停止し、再生するように画像を変更します。

B)次及び前Imagebuttonsメソッドのメディアプレーヤを呼び出し(I)

C)REPET IMAGEBUTTON、リモートビューにリスナーを追加するための方法changeRepeat()

+0

http://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification多分このヘルプu – Tony

+0

を参照してください:http://stackoverflow.com/questions/15350998/determine-addaction-click -for-android-notifications – KrishnaJ

答えて

0

を呼び出すint型

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

私があなたの言ったことを私のボタンの上に追加したとしましょう。私のボタンの曲は一時停止しています。その保留中のインテントは何をしていますか? –

+0

一時停止ボタンをクリックすると、保留中のインテントがトリガーされます。 保留中のインテントは受信者またはサービスを開始する可能性があり、アクティビティは保留中のインテントのインスタンスを取得する方法によって異なります。 詳細情報: https://developer.android.com/reference/android/app/PendingIntent.html –

0

remoteViews.setOnClickPendingIntent(R.id.someView, pendingIntent);をBroadCastReceiverと組み合わせて使用​​して、次のような通知を行うことができます。

ブロードキャストレシーバーのトリガー方式では、メディアプレーヤーを管理する必要があります。私の場合MainActivityにバインドされたサービスがありますので、MainActivityのメソッドがサービスを管理するメソッドを起動したときにMainActivityとBroadcast Receiverをインターフェイスで接続しました。サービスはMediaPlayerを管理します。

関連する問題