23

我製作了custom notification,並且有一個按鈕,我想執行兩個不同的functionalities on notification and button click。我看了很多鏈接,但無法找到添加按鈕偵聽器的方式。在自定義通知中添加按鈕動作

任何人都可以幫忙。這是我的代碼。非常感謝。

private void startNotification() { 
    Intent intent; 
    PendingIntent pIntent; 
    RemoteViews remoteViews = new RemoteViews(getPackageName(), 
      R.layout.mynotification); 

    Context context = getApplicationContext(); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      this).setSmallIcon(R.drawable.ic_launcher).setContent(
      remoteViews); 

    if (hasFlash) { 
     intent = new Intent(context, FlashLight.class); 
     pIntent = PendingIntent.getActivity(context, 1, intent, 0); 
    } else { 
     intent = new Intent(context, BlankWhiteActivity.class); 
     pIntent = PendingIntent.getActivity(context, 1, intent, 0); 
    } 
    builder.setContentIntent(pIntent); 
    NotificationManager mNotificationManager = (NotificationManager)  getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notif = builder.setContentTitle("Flashlight") 
      .setContentText("Lighten your world!!!").build(); 
    mNotificationManager.notify(1, notif); 

    remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent); 

} 

我已經通過了按鈕的ID(closeOnFlash)在setOnClickPendingIntent不知道爲什麼它不工作。

這裏是我的xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="horizontal" 
android:weightSum="100" > 

<ImageView 
    android:id="@+id/notifiation_image" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="30" 
    android:contentDescription="@string/appImage" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/appName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="50" 
    android:gravity="center" 
    android:text="@string/flashLightOn" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/closeOnFlash" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="20" 
    android:text="@string/close" /> 

+2

可能是這個線程幫助? http://stackoverflow.com/questions/5479165/event-onclick-for-a-button-in-a-custom-notification – user1406716

+1

這個線程應該回答你正在做的事情:http://stackoverflow.com/questions/12438209 /處理按鈕 - 裏面的安卓通知 – user1406716

+0

好的非常感謝,我會給它一看。 –

回答

40

這爲我工作:

private void startNotification(){ 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(ns); 

    Notification notification = new Notification(R.drawable.ic_launcher, null, 
      System.currentTimeMillis()); 

    RemoteViews notificationView = new RemoteViews(getPackageName(), 
      R.layout.mynotification); 

    //the intent that is started when the notification is clicked (works) 
    Intent notificationIntent = new Intent(this, FlashLight.class); 
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

    notification.contentView = notificationView; 
    notification.contentIntent = pendingNotificationIntent; 
    notification.flags |= Notification.FLAG_NO_CLEAR; 

    //this is the intent that is supposed to be called when the 
    //button is clicked 
    Intent switchIntent = new Intent(this, switchButtonListener.class); 
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, 
      switchIntent, 0); 

    notificationView.setOnClickPendingIntent(R.id.closeOnFlash, 
      pendingSwitchIntent); 

    notificationManager.notify(1, notification); 
} 


public static class switchButtonListener extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("Here", "I am here"); 
     FlashOnOff flashLight; 
     flashLight = new FlashOnOff(); 
     flashLight.flashLightOff(); 
     flashLight.releaseCamera();   
    } 
} 

和我的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="horizontal" 
android:weightSum="100" > 

<ImageView 
    android:id="@+id/notifiation_image" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="30" 
    android:contentDescription="@string/appImage" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/appName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="50" 
    android:gravity="center" 
    android:text="@string/flashLightOn" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/closeOnFlash" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="20" 
    android:text="@string/close" /> 

在根據應用標籤manefest:

<receiver android:name="FlashLight$switchButtonListener" /> 
+0

@PoojaShah樂意幫忙:) –

+1

Manifest中的聲明如何工作? FlashLight是包含swiftButtonListener的類嗎?它是應用程序名稱嗎? –

+1

其活動名稱 –