web-dev-qa-db-ja.com

カスタム通知にボタンアクションを追加する

custom notificationとその中にボタンがあり、2つの異なる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" />
30
Syed Raza Mehdi

通知を開始:

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" />

Applicationタグの下のマニフェスト:

<receiver Android:name="FlashLight$switchButtonListener" />
50
Syed Raza Mehdi