添加按钮动作在自定义通知 [英] Adding button action in custom notification

查看:73
本文介绍了添加按钮动作在自定义通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了自定义通知键,存在这样一个按钮,我想在通知和按钮,点击功能C $ C>。我看很多的联系,但找不到添加按钮侦听的方式。

I have made custom notification and there is a button in that, I want to perform two different functionalities on notification and button click. I look at many links but couldn't find the way to add button listener.

谁能帮助。这是我的code。非常感谢。

Can anyone help. Here is my code. Thanks a lot.

 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 不知道为什么它不工作。

I have passed the button id (closeOnFlash) in setOnClickPendingIntent don't know why its not working.

和这里是我的 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" />

推荐答案

这为我工作:

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:

In manefest under Application tag:

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

这篇关于添加按钮动作在自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆