2016-11-07 53 views
0

我正在尝试制作一个简单的秒表应用程序,该应用程序将在通知中显示时间,并为您提供一些可让您启动和停止秒表的按钮。如何在通知中做按钮'做点什么'

如何将按钮添加到通知?我该如何将该按钮指向某个功能?

下面有什么,我在想一个画面: http://image.prntscr.com/image/a4113f39cbaf46c597dc32f07bcc531c.png

actionIntent = new Intent(this, MainActivity.class); 
    actionPendingIntent = PendingIntent.getService(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    timerNotification.addAction(android.R.drawable.ic_media_pause, "Start", actionPendingIntent); 

这是我现在有。在意图的地方,我会把我想要执行的功能放在哪里?

回答

0

添加操作的通知,并分配一个pendingintent

+0

我已更新我的问题。我在哪里等待意向函数? –

0

如果您想自定义您的通知布局,可以使用setContent()函数与您的自定义布局的RemoteViews。

Remote View mRemoteView = new RemoteViews(getPackageName(), R.layout.notification_general); 
Notification.Builder mBuilder = new Notification.Builder(context); 
mBuilder.setSmallIcon(R.mipmap.ic_battery) 
     .setContent(mRemoteView) 
     .setContentIntent(notificationPendingIntent); 
mNotificationManager.notify(1, mBuilder.build()); 

要处理的通知按钮onClick事件,你需要使用独立的PendingIntents(从differecnt行动意图制造),每个按钮。稍后在onReceive()中,您只需检查传入Intent的操作&根据该操作执行不同的代码。记得在清单上分配您的侦听器。

Intent generalIntent = new Intent(context, GeneralReceiver.class); 
generalIntent.putExtra(REQUEST_CODE, ACTION_GENERAL); 
PendingIntent generalPendingIntent = 
     PendingIntent.getBroadcast(context, 1, generalIntent, 0); 
mRemoteView.setOnClickPendingIntent(R.id.btnNotificationGeneral, generalPendingIntent); 
public static class GeneralReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      //Your code here 
     } 
    } 
相关问题