0

Android の通知サービスのデモ例を調べるために、次のリンクを参照しました: Sai Geetha ブログVogella チュートリアル

どちらも機能しましたが、部分的には、両方のプロジェクトをそのままダウンロードして実行しました。どちらにも通知を開始するボタンがあります。On Button Click 通知が上部のステータス バーに表示されます。

ここで問題が発生します。その通知をクリックすると、メッセージが表示されたり、新しいアクティビティに移動する意図が発せられたりすることはありません。

私はこの概念に慣れていないので、助けていただければ幸いです...

編集

CreateNotification .class

public class CreateNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void createNotification(View view) {
        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int UNIQUE_ID = 123458;
        Intent navigationIntent = new Intent();
        navigationIntent.setClass(CreateNotification.this,
                NotificationReceiver.class);

        PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
                0);
        String body = "New Notification added!!!";
        String title = "Title";
        Notification n = new Notification(R.drawable.ic_launcher, body,
                System.currentTimeMillis());
        n.number = 2;
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
        nm.notify(UNIQUE_ID, n);
    }
}

NotificationReceiver.class

public class NotificationReceiver extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        Log.i("Receiver", "NotificationReceiver");
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="createNotification"
        android:text="Create Notification" >
    </Button>

</LinearLayout>

結果.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>

</LinearLayout>
4

1 に答える 1

2

これをチェックしてください

   public static NotificationManager nm;
public static final int UNIQUE_ID = 123458;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newone);
    createNotification();

}
public void createNotification() {
    NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    final int UNIQUE_ID = 123458;
    Intent navigationIntent = new Intent();
    navigationIntent.setClass(NEWAct.this,
            TestActivity.class);
    navigationIntent.putExtra("selected_tab", "more");

    PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
            0);
    String body = "New Notification added!!!";
    String title = "Title";
    Notification n = new Notification(R.drawable.ic_launcher, body,
            System.currentTimeMillis());
    n.number = 2;
    n.setLatestEventInfo(this, title, body, pi);
    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(UNIQUE_ID, n);
}

アクティビティの開始後、id(UNIQUE_ID)を使用してその通知をキャンセルする必要があります

              try {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(UNIQUE_ID);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-04-25T06:44:33.737 に答える