2012-05-09 29 views
13

Tôi đang cố gắng để thực hiện một thông báo với 2 nút trên nó:thông báo Android với các nút trên đó

  • một đưa tôi trở lại hoạt động
  • kia đóng nó

Có bất cứ ai có một ý tưởng về làm thế nào để nắm bắt sự kiện bấm nút (hãy nhớ rằng các hoạt động bị tạm dừng)?

+0

Thành thật mà nói tôi không thích ý tưởng đằng sau ảnh hưởng của bạn, hoặc tôi không hiểu nó. Thông báo bình thường trong Android có X ở bên phải và nếu bạn nhấp vào chúng, bạn sẽ được chuyển hướng đến hoạt động. – user61664

+0

Chúng tôi dạy cho họ và cho họ biết Hướng dẫn dành cho nhà phát triển Android;) –

Trả lời

4

Đối với ICS, câu hỏi rất đơn giản để trả lời vì hành vi cần phản ánh thông báo mặc định: bạn có thể gần thông báo vuốt sang bên phải, và bạn có thể xác định đó hoạt động để gửi người dùng đến khi ông nhấn nó chỉ đơn giản sử dụng PendingIntent:

// 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); 

mã lấy từ http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

cảm ơn câu trả lời của bạn, mặc dù nó không phải là những gì tôi yêu cầu .. Điều tôi muốn làm là thông báo với 2 nút trên đó (đã có bố cục), nhưng tôi đang gặp sự cố khi bắt sự kiện onClick, tôi muốn một trong số họ đóng ** HOẠT ĐỘNG ** và người kia để thực hiện hành động mặc định của thông báo. – doronsl

3

Nếu bạn muốn gán ý định cụ thể cho một nút:

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

Tôi giả sử rằng bạn chỉ cần một ý định sẽ được gửi khi nút được nhấn vào, vì vậy bạn phải TRÁNH thiết lập mục đích thông báo chính

notification.contentIntent = yourPendingIntent; 

Nếu không (nếu bạn thiết lập "notification.contentIntent = pendingIntent; "như thường lệ) cả hai ý định sẽ được gọi mà có thể không phải là những gì bạn muốn/người dùng mong đợi.

Nếu bạn vẫn muốn nhấn các phần khác của thông báo, hãy gọi ý định chung đó (hoặc bất kỳ mục đích nào khác), bạn có thể sử dụng cùng một phương pháp chỉ định ý định cho mỗi lần xem như trên. Đừng quên đặt

android:clickable="true" 

cho bất kỳ chế độ xem nào bạn muốn theo dõi onClick() cho.

Bạn có thể theo dõi các ý định này bằng các tính năng bổ sung của chúng trong hoạt động mà chúng đang gọi. Nếu bạn đang gọi bạn chính/hoạt động phóng hơn bạn sẽ theo dõi chúng ở đây (vì nó xuất phát từ javadoc cho phương pháp này):

@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

Tôi vui mừng khi gửi nó! Sau khi làm việc cả đêm, tôi tìm thấy một cái gì đó. Vì vậy, ở đây chúng tôi đi!

1. Tạo tệp bố cục xml cho thông báo của bạn.

2. Tạo thông báo bằng Notification.Builder. Sau khi thêm tất cả mọi thứ bạn muốn (các biểu tượng, âm thanh, vv) làm điều này:

 //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. Tạo một phương pháp setListeners. Bên trong phương pháp này bạn phải viết này:

//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. Đối với yêu cầu của tôi, tôi sử dụng một HelperActivity mà đáp ứng các intents.Nhưng đối với bạn tôi không nghĩ rằng nó là cần thiết.

Nếu bạn muốn mã nguồn đầy đủ, bạn có thể duyệt qua hoặc tải xuống từ kho lưu trữ git của tôi. Mã này dành cho mục đích sử dụng cá nhân, vì vậy đừng mong đợi đọc một mã tuyệt đẹp với nhiều nhận xét. https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

TẤT CẢ CÁC TRÊN ĐÂY, TRẢ LỜI CÂU HỎI DANH MỤC TÌM HIỂU SỰ KIỆN TỪ BUTTONS KHÁC.

thiệu về cách hủy thông báo tôi chuyển hướng bạn ở đây

How to clear a notification in Android

Chỉ cần nhớ để sử dụng id bạn phân tích tại thông báo phương pháp khi bạn gọi thông báo cho thời gian nắm tay

+7

Điều này hoạt động xuất sắc trên ICS 4.0.4! Bạn nên xem xét định dạng câu trả lời của mình một cách chính xác để người dùng khác có thể có cái nhìn tốt về câu trả lời của bạn. –

+7

Bạn nói đúng. Tôi nên làm điều đó từ việc ăn xin. Tôi hy vọng rằng nó là tốt hơn bây giờ. Do nhận xét của bạn, tôi đã thêm liên kết để tải xuống mã nguồn. Tất cả các độc giả từ bây giờ nên cảm ơn bạn, không phải tôi. Chúc mừng năm kết thúc và năm mới hạnh phúc nhất – LiTTle

+1

Thật tuyệt vời! Chúc mừng năm mới!! –

3

Bạn chỉ có thể thêm hành động Các nút trong số Notification bằng cách đặt hành động thành Notification.Builder và xác định PendingIntent cho mỗi hành động

dưới đây là mã mẫu:

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

Cảm ơn bạn rất nhiều! – DmitryKanunnikoff

2

Có một ví dụ đầy đủ cho các bạn ở đây

//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); 

và sau đó bạn có thể xác định phương pháp nghe:

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); 

} 

và đây là cách bố trí notification_view để costumize thông báo của bạn.

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

Ví dụ này làm những gì nó yêu cầu với một bản sao & dán (sau khi bạn thêm các khoản tiền của bạn). Tuy nhiên, 'builder.setCustomContentView (remoteViews);' sẽ tốt hơn 'notification.contentView = remoteViews;' và 'builder.build()' tốt hơn 'builder.getNotification()' do không dùng nữa. (Bạn phải sắp xếp lại mã một chút cho những thay đổi này). – Gary99

+0

Có, tôi đã sao chép mã từ SimpleProject của riêng mình và Cảm ơn bạn đã cập nhật :) – Amin

Các vấn đề liên quan