2012-05-09 35 views
13

我試圖做一個通知,2個按鈕就可以了:上面有按鈕Android的通知

  • 一個把我帶回到活動
  • 其他關閉它

有任何人都知道如何捕捉按鈕點擊事件(請記住活動已暫停)?

+0

說實話,我不喜歡背後的想法,或者我不明白。 Android中的正常通知在右側有X,如果你點擊它們,你重定向到了活動。 – user61664

+0

我們教他們並向他們展示Android開發人員指南;) –

回答

4

至於ICS,問題是簡單的回覆,因爲所需的行爲反映了默認的通知:您可以接近通知其滑動到右側,你可以定義活動用戶發送到時,他按下它簡單地使用PendingIntent:從http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

感謝您的回答,雖然這不是我所要求的。 我想要做的是帶有2個按鈕的通知(已經有佈局),但是我在遇到onClick事件時遇到問題,我希望其中一個關閉** ACTIVITY **,另一個關閉通知的默認操作。 – 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); 

代碼如果你想具體意圖分配到一個按鈕:

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倉庫下載它。該代碼是供個人使用的,所以不要指望用很多評論來閱讀華麗的代碼。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

以上所有內容都回答了從不同按鈕捕捉事件的問題。

關於取消通知我在這裏將您重定向

​​

只要記得用你的通知方法解析的ID,當你要求拳頭時間

+7

這在ICS 4.0.4上非常出色!您應該考慮正確地設置您的答案,以便其他用戶可以仔細查看您的答案。 –

+7

你說得對。我應該從開始做起。我希望現在好一點。由於您的評論,我添加了鏈接以下載源代碼。從現在起所有的讀者都應該感謝你,而不是我。快樂的結局和幸福的新年 – LiTTle

+1

這真是太棒了!新年快樂!! –

3

您可以簡單地添加行爲的通知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); 

} 

這裏是notification_view佈局costumize您的通知。

<?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