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>