我参考了下面的链接来研究Android中通知服务的示例:赛格萨博客和Vogella教程。
两者都工作,但部分,即我已经下载了这两个项目的现状,并执行他们。两者都有按钮来启动通知。在按钮单击通知出现在顶部状态栏。
问题来了,只要单击通知,就不会显示任何消息,也不会激发任何消息来导航到新的活动。
我对这个概念很陌生,所以任何帮助都要感谢.
编辑
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>result.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>发布于 2012-04-25 06:44:33
看看这个
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();
}https://stackoverflow.com/questions/10310425
复制相似问题