少量通知操作的一项服务

时间:2016-04-05 14:30:27

标签: android notifications

我用动作创建了通知。并通过通知处理我正在使用服务。对于每个动作我创建了一些额外的待定意图。但是在服务中,当我试图获得额外的意图时,我得到了错误的价值。这是我的代码:

类NotificationCreator

public Notification createConnectedNotification(){
    Context context = TeamlabApplication.getInstance().getApplicationContext();
    CharSequence comeToWork = context.getString(R.string.toWork);
    CharSequence goFromDinner = context.getString(R.string.fromDinner);


    PendingIntent actionComeIntent = createActionSendPendingIntent(context, comeToWork.toString());
    Notification.Action.Builder sendMessageToWork =
            new Notification.Action.Builder(R.mipmap.icon_notification_action_send, comeToWork, actionComeIntent);

    PendingIntent actionDinnerIntent = createActionSendPendingIntent(context, goFromDinner.toString());
    Notification.Action.Builder sendMessageFromDinner =
            new Notification.Action.Builder(R.mipmap.icon_notification_action_send, goFromDinner, actionDinnerIntent);

    PendingIntent activityIntent = createActivityPendingIntent(context);

    Notification.Builder builder = new Notification.Builder(context)
            .setSmallIcon(R.mipmap.icon_notification_main)
            .setContentTitle("Connected to WI-FI")
            .setContentText("You connected to work WI-FI. Do you want to send message in teamLab")
            .setContentIntent(activityIntent)
            .setLights(Color.WHITE, 1000, 1000)
            .setAutoCancel(true)
            .addAction(sendMessageToWork.build())
            .addAction(sendMessageFromDinner.build());

    return builder.build();
}



private PendingIntent createActionSendPendingIntent(Context context, String extraMessage){
    Intent intent = new Intent(context, NotificationService.class);
    intent.putExtra(NotificationService.MESSAGE_EXTRA, extraMessage);
    return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}

class NotficationService

public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            AppLog.d("Extra is: " + intent.getStringExtra(MESSAGE_EXTRA));
        }

String toWork = "To work" , goFromDinner = "From dinner"。在通知中,我使用字符串goFromDinner按下了acton,但在Extra i中有结果To work。如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。您需要为不同的操作设置不同的requestCode。所以现在我的代码是:

#content .blog ul li a + a { display: block; }