Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让通知在活动中打开视图?_Java_Android_Android Layout_Notifications - Fatal编程技术网

Java 如何让通知在活动中打开视图?

Java 如何让通知在活动中打开视图?,java,android,android-layout,notifications,Java,Android,Android Layout,Notifications,现在我的主活动上有一个警报,它启动一个类,在状态栏上启动一个通知。当我单击通知时,它会打开我的主要活动,现在我希望它在该活动中打开一个特定于n的视图 这是我的通知代码 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); int icon = and

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

这是我的通知代码

       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);
这是我的主要活动使用的布局中的按钮,用于打开我希望通知打开的视图(图形视图)

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


有人能帮我吗?

您可以使用意图的附加项向通知中设置的待定意图添加一个标志。在活动开始时评估意图。如果在起始意图中找到该标志,请执行
openGraphs()
中的代码。确保获取最新的意图(而不是之前可能已开始活动的意图,这里有一些建议:)。

您可以使用意图的附加项向通知中的挂起意图集添加标志。在活动开始时评估意图。如果在起始意图中找到该标志,请执行
openGraphs()
中的代码。确保获得最新的意图(而不是之前可能已经开始活动的意图,这里有一些建议:)。

是否有任何东西阻止您直接在活动中显示图表

在上述活动中,要在单击时显示图形,请在onCreate()方法中将视图直接设置为R.layout.graphs,而不是使用按钮


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

是否有任何东西阻止您直接在活动中显示图形

在上述活动中,要在单击时显示图形,请在onCreate()方法中将视图直接设置为R.layout.graphs,而不是使用按钮

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

    <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);
}