2012-06-03 27 views
0

現在我的主要活動有一個鬧鐘,它啓動了一個在狀態欄上啓動通知的類。當我點擊通知時,它會打開我的主要活動,現在我希望它在該活動中打開一個特定的視圖。如何在活動中打開通知視圖?

這是我的通知代碼。

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

所以基本上,我得到通知打開應用程序並啓動主要活動,但我希望它直接啓動圖形視圖。

任何人都可以幫助我嗎?

回答

5

您可以使用意圖的附加內容向通知中設置的未決意圖添加標誌。在活動開始時評估意圖。如果您在起始意向中找到該標誌,請執行openGraphs()中的代碼。確保獲得最近的意圖(不是早些時候開始活動的意圖,這裏有一些建議:https://stackoverflow.com/a/6838082/1127492)。

0

有沒有什麼能阻止你直接在活動中顯示圖形?

在上面的Activity中,如果沒有按鈕,點擊時顯示Graph,直接在onCreate()方法中將視圖設置爲R.layout.graphs。

如果您爲其他目的而規定的活動,則創建一個單獨的活動只是爲了顯示圖形並從notificationIntent指向它。