Android通知操作打开Listview DetailView

时间:2018-08-15 08:03:01

标签: java android listview notifications

在我的服务中,我通过mqtt接收数据,将该数据发送到我的mainactivity并发出呼叫通知。在我的mainactivity中,数据保存在绑定到适配器的Arraylist中。

我的问题是我想通过通知操作打开我的detailview活动,但是我只能访问最后收到的数据。我想我有某种方式可以访问我的arraylist,但不知道该怎么做。

如果需要mainactivity代码,我会发布它。

通过这种方法,我将数据发送到我的主要活动并调用通知:

private void  handleMessageArrived(String topic, MqttMessage message) {

    String messageBody = new String(message.getPayload());
    Datamodel datamodel = splitString(messageBody);

    Intent done = new Intent();
    done.setAction("mqttData");
    done.putExtra("newData",datamodel);

    runNotification(datamodel);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(done);
}

这是我的通知方法:

private void runNotification(Datamodel datamodel)
{
    Intent intent = new Intent(this,DetailActivity.class );
    intent.putExtra("Datamodel",datamodel);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(intent);


    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder builder = new Notification.Builder(getApplicationContext())
            .setContentTitle(datamodel.getline1())
            .setContentText(datamodel.getline2())
            .setSmallIcon(R.drawable.alarm)
            .setStyle(new Notification.BigTextStyle()
                    .bigText(Html.fromHtml(datamodel.toString())))
            .setGroup(MQTT_GROUP_NOTIFICATION)
            .setContentIntent(resultPendingIntent);

    Notification notification = builder.build();

    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_SOUND;

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID,notification);
    NOTIFICATION_ID++;
}

0 个答案:

没有答案