2012-05-09 27 views
13

、私はそれに2つのボタンで通知をしようとしています:Androidの通知は、

  • 1は、活動に戻って私を取る
  • 他はそれ

が持つ閉じ誰でもボタンのクリックイベントをキャッチする方法を知っています(アクティビティは一時停止しています)。 ICSについては

+0

正直言って、私はあなたのeffordの背後にあるアイデアが好きではない、または私はそれを理解していません。アンドロイドの通常の通知では、右側にXが表示されます。クリックすると、アクティビティにリダイレクトされます。 – user61664

+0

私がしたいことは、2つのボタンが付いた通知(既にレイアウトがある)ですが、私はそれらを教えて、彼らにAndroidデベロッパーガイドラインを示してください;) –

答えて

4

必要な動作がデフォルトの通知を反映しているため、質問はお答えするのは簡単です:あなたは右にスワイプ通知近くをことができ、あなたはときに彼にユーザーを送信するためにどの活動定義することができますhttp://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

あなたの答えをありがとう、 onClickイベントを捕まえることに問題があります。そのうちの1人が** ACTIVITY **を終了し、もう1人が通知のデフォルトアクションを実行するようにします。 – doronsl

3

から取ら

// The PendingIntent to launch our activity if the user selects this 
// notification. Note the use of FLAG_CANCEL_CURRENT so that, if there 
// is already an active matching pending intent, cancel it and replace 
// it with the new array of Intents. 
PendingIntent contentIntent = PendingIntent.getActivities(this, 0, 
     makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT); 

コードを使用すると、ボタンに特定の意図を割り当てる場合::それは単にPendingIntentを使用して押す

views.setOnClickPendingIntent(R.id.your_button_id, pendingIntent); 

あなたがメインの通知意図

notification.contentIntent = yourPendingIntent; 

を設定しないように持っているので、あなたが「notification.contentIntent =を設定している場合、私は(そうでなければ、あなたはボタンがクリックされたときに送信される唯一の意図を必要とすると仮定pendingIntent; "いつものように)両方のインテントが呼び出されますが、これはユーザが望むものではないかもしれません。

通知の他の部分を押しても、一般的な意図(またはその他のもの)が呼び出されるようにするには、上記と同じインテント/ビュー割り当ての方法を使用できます。

android:clickable="true" 

あなたがonClick()を追跡するすべてのビューに設定することを忘れないでください。

これらのインテントは、自分が呼び出しているアクティビティのエクストラで追跡できます。あなたがあなたを呼んている場合(それは、このメソッドのJavadocから来ているよう)あなたがここでそれらを追跡しますよりも、活動ランチャー/メイン:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    Bundle data = intent.getExtras(); 

    if (data != null && data.containsKey(YOUR_INTENT_KEY_SOURCE_CONSTANT)) { 
     // process your notification intent 
    } 

    // go on with smth else 
} 
41

は、私はそれを投稿して嬉しいです!一晩中働いた後、私は何かを見つけた。だからここに行く!

1.通知用のxmlレイアウトファイルを作成します。

2. Notification.Builderを使用して通知を作成します。あなたが望むすべてのものを追加した後(アイコン、サウンド、など)これを行う:

 //R.layout.notification_layout is from step 1 RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout); setListeners(contentView);//look at step 3 notification.contentView = contentView; 

3.

は、メソッドsetListenersを作成します。あなたがこれを記述する必要があり、このメソッドの内部で:私の要件については

//HelperActivity will be shown at step 4 

    Intent radio=new Intent(ctx, packagename.youractivity.class); 
    radio.putExtra("AN_ACTION", "do");//if necessary 

    PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0); 
    //R.id.radio is a button from the layout which is created at step 2 view.setOnClickPendingIntent(R.id.radio, pRadio); 

    //Follows exactly my code! 
    Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class); 
    volume.putExtra("DO", "volume");</p> 

    //HERE is the whole trick. Look at pVolume. I used 1 instead of 0. 
    PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0); 
    view.setOnClickPendingIntent(R.id.volume, pVolume); 

4.私は意図に応答HelperActivityを使用しました。しかし、あなたにとって私は必要とは思わない。

完全なソースコードが必要な場合は、参照したり、git repoからダウンロードしたりできます。コードは個人的な用法ですので、たくさんのコメントで豪華なコードを読むことを期待しないでください。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

上記のすべては、さまざまなボタンからのキャッチイベントに関する質問に答えています。通知をキャンセルについて

私はあなたが単純にアクションを追加することができます

+7

これはICS 4.0.4でうまく動作します!あなたは、他のユーザーがあなたの答えをよく見ることができるように、あなたの答えを適切にフォーマットすることを検討するべきです。 –

+7

あなたは正しいです。私はそれをやり遂げるべきだった。私はそれが今より良いことを願っています。あなたのコメントのために、私はソースコードをダウンロードするためのリンクを追加しました。今からすべての読者は、私ではなく、あなたに感謝すべきです。ハッピーエンディングの年と幸せな新年 – LiTTle

+1

それはあなたのいいです!明けましておめでとうございます!! –

3

こぶし時間の通知と呼ばれたときにちょうどあなたが通知方法で解析されたIDを使用することを忘れないでください

​​

あなたをここにリダイレクトNotificationのアクションをNotification.Builderに設定し、アクションごとにPendingIntentを定義して、

をサンプルコードにします:

NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.notification_icon) 
       .setContentTitle("My notification") 
       .setContentText("Hello World!") 
     .addAction(R.drawable.action_posetive,"posetive",PendingIntent.getActivity(0,intent,0)) 
.addAction(R.drawable.action_clear,"clear",PendingIntent.getActivity(0,intent,0)); 
     NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(0, mBuilder.build()); 
+0

ありがとうございました! – DmitryKanunnikoff

2

はここにあなたのための

//Add this code to onCreate or some onclick Buttton 
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); 
    long when = System.currentTimeMillis(); 
    builder.setSmallIcon(R.drawable.ic_notification); 
    Intent notificationIntent = new Intent(getApplicationContext(), notificationActivity.class).putExtra("notification", "1"); 
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 
    Notification notification = builder.getNotification(); 
    notification.when = when; 

    RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_view); 
    remoteViews.setTextViewText(R.id.tvName, "New Name"); 
    listener(remoteViews,getApplicationContext()); 


    notification.contentView = remoteViews; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    manager.notify(1, notification); 

完全な例があり、その後、あなたはリスナーメソッドを定義することができます

public void listener(RemoteViews remoteViews, Context context) { 
    // you have to make intetns for each action (your Buttons) 
    Intent intent = new Intent("Accept"); 
    Intent intent2 = new Intent("Reject"); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1,intent,0); 
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,1,intent2,0); 

    // add actions here ! 
    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction("Accept"); 
    intentFilter.addAction("Reject"); 


    BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(intent.getAction().equals("Accept")){ 
       Toast.makeText(context, "Accepted !!", Toast.LENGTH_SHORT).show(); 
      } else if(intent.getAction().equals("Reject")) { 
       Toast.makeText(context, "Rejected !!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }; 

    context.registerReceiver(receiver,intentFilter); 
    remoteViews.setOnClickPendingIntent(R.id.ivRequest,pendingIntent); 
    remoteViews.setOnClickPendingIntent(R.id.ivReject,pendingIntent2); 

} 

を、ここであなたの通知をcostumizeするnotification_viewのレイアウトです。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="16dp"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:text="Request from " 
    /> 

<TextView 
    android:id="@+id/tvName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginStart="15dp" 
    android:layout_toRightOf="@id/textView" 
    android:text="Amin" 
    /> 

<ImageView 
    android:id="@+id/ivRequest" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:layout_alignParentEnd="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/notification" 
    /> 

<ImageView 
    android:id="@+id/ivReject" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:layout_marginEnd="10dp" 
    android:layout_toLeftOf="@id/ivRequest" 
    android:layout_centerVertical="true" 
    android:src="@drawable/trash" 
    /> 

    </RelativeLayout> 
+1

この例では、コピー&ペースト(ドロアブルを追加した後)を主張します。しかし、 'builder.setCustomContentView(remoteViews);'は 'notification.contentView = remoteViews;より優れており、' builder.build() 'は非推奨のために' builder.getNotification() 'よりも優れています。 (これらの変更のためにコードを少し並べ替える必要があります)。 – Gary99

+0

はい、自分のSimpleProject からコードをコピーしてありがとうございました。 – Amin