cri*_*.to 0 java notifications android android-layout
现在我对我的主要活动发出警报,启动了一个在状态栏上启动通知的类.当我点击通知时,它会打开我的主要活动,现在我希望它在该活动中打开一个特定的视图.
这是我的通知代码.
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);
Run Code Online (Sandbox Code Playgroud)
这是我的主要活动使用的布局中的按钮,它打开了我希望打开通知的视图(图表视图).
<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" />
Run Code Online (Sandbox Code Playgroud)
当您单击按钮时.它在主类中执行以下方法.
public void openGraphs(View v) {
canExit = false;
setContentView(R.layout.graphs);
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,我收到通知打开应用程序并启动主要活动,但我希望它直接启动图表视图.
有谁能够帮我?
您可以使用intent的附加项向通知中的待定意图集添加标记.在活动开始时评估意图.如果在起始意图中找到标志,则执行代码openGraphs().确保获得最新的意图(不是之前可能已启动该活动的意图,以下是一些建议:https://stackoverflow.com/a/6838082/1127492).