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

基本的には、アプリを開いてメインのアクティビティを起動するようにという通知を受け取りましたが、グラフ ビューを直接起動する必要があります。

誰でも私を助けることができますか?

4

2 に答える 2

5

インテントのエクストラを使用して、通知に設定された保留中のインテントにフラグを追加できます。活動の開始時に意図を評価します。開始インテントでフラグが見つかった場合は、 のコードを実行しますopenGraphs()。必ず最新のインテントを取得してください (以前にアクティビティを開始したインテントではありません。これに関するアドバイスはhttps://stackoverflow.com/a/6838082/1127492です)。

于 2012-06-03T17:08:59.493 に答える
0

アクティビティでグラフを直接表示するのを妨げているものはありますか?

上記のActivityでボタンを持たず、クリックしたときにグラフを表示するには、onCreate()メソッドでビューをR.layout.graphsに直接設定します。

他の目的のために所定のアクティビティがある場合は、グラフを表示し、notificationIntent からそれを指すためだけに別のアクティビティを作成します。

于 2012-06-04T11:46:02.047 に答える