android为通知添加按钮,在自定义通知中添加按钮操作

开始通知为:

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:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="horizontal"

android:weightSum="100" >

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

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

android:id="@+id/closeOnFlash"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="20"

android:text="@string/close" />

在清单中的Application标签下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值