我已经制作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, …Run Code Online (Sandbox Code Playgroud) android android-intent android-layout android-notifications android-fragments
我有一个带有两个按钮的应用程序.一个"关闭"应用程序的按钮和一个开始算法的按钮.当我点击"开始"时,它"隐藏"应用程序并在通知栏中显示通知.我需要能够在单击/按下通知时执行/调用方法.这类问题有几个答案,但它们非常模糊,只能指向BroadcastReceiver上文档的链接.
如果您要将一个URL留给BroadcastReceiver文档并说"阅读本页",请不要回复此问题.如果您要解释如何使用BroadcastReceiver来执行一个方法(从显示通知的同一个类中),请向我展示一些如何完成此操作的代码.
我的算法:按下按钮,显示通知,点击通知,调用方法(不显示活动).而已.
如果不可能,请告诉我.如果是,请告诉我你将做些什么来使它成为可能.这个简单的东西不应该被android sdk的开发人员忽视.
我在通知中添加了一个按钮

但我不知道如何在点击它时调用它.
我尝试了这样的方法https://code.google.com/p/languagepickerwidget/source/browse/trunk/trunk/src/org/gnvo/langpicker/LangPicker.java,因为它也使用了RemoteViews对象,但没有任何反应我点击按钮.
这就是我目前拥有的:
private void createNotification(){
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.notification_switch);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, SettingsActivity.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 …Run Code Online (Sandbox Code Playgroud)