【问题标题】:How to have a notification open a view within an activity?如何让通知在活动中打开视图?
【发布时间】:2012-06-04 19:46:02
【问题描述】:

现在我的主要活动有一个警报,它启动了一个在状态栏上启动通知的类。当我点击通知时,它会打开我的主要活动,现在我希望它在该活动中打开一个特定的视图。

这是我的通知代码。

       String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
    int icon = android.R.drawable.stat_notify_chat;        // icon from resources
    CharSequence tickerText = "TickerText";              // ticker-text
    long when = System.currentTimeMillis();         // notification time
    CharSequence contentTitle = "My notification";  // message title
    CharSequence contentText = "Hello World!";      // message text

    //This is the intent to open my main activity when the notification is clicked
    final Intent notificationIntent = new Intent(context, mainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    // the next two lines initialize the Notification, using the configurations above
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(notifID, notification);

这是我的主要活动使用的布局中的按钮,用于打开我希望通知打开的视图(图形视图)。

    <Button
    android:id="@+id/graphsButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_below="@id/reviewButton"
    android:layout_toRightOf="@id/medicineButton"
    android:onClick="openGraphs"
    android:text="Graphs" />

当您单击按钮时。它在主类中执行以下方法。

   public void openGraphs(View v) {
    canExit = false;
    setContentView(R.layout.graphs);
}

所以基本上,我收到了打开应用程序并启动主要活动的通知,但我希望它直接启动图形视图。

谁能帮帮我?

【问题讨论】:

    标签: java android android-layout notifications


    【解决方案1】:

    您可以使用意图的附加功能向通知中的待处理意图集添加一个标志。在活动开始时评估意图。如果您在起始意图中找到标志,请执行openGraphs() 中的代码。确保获得最新的意图(不是可能较早开始活动的意图,这里有一些建议:https://stackoverflow.com/a/6838082/1127492)。

    【讨论】:

      【解决方案2】:

      有什么阻止您直接在 Activity 中显示图表吗?

      在上面的Activity中没有按钮,点击时显示Graph,直接在onCreate()方法中设置视图为R.layout.graphs。

      如果您将规定的活动用于其他目的,则创建一个单独的活动以显示图表并从 notificationIntent 中指向它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 2023-03-13
        • 2015-09-07
        相关资源
        最近更新 更多