所有这些都是我在通知课程中的代码,我在完成第一项活动的任务后正在调用。
但是我遇到了获取当前申请通知的问题。
我想将Notification显示为对话框。
"R.layout.main"
包含带OK按钮的对话框。
public class Notif extends Activity implements View.OnClickListener {
private Button Button01;
private NotificationManager mManager;
private static final int APP_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.Button01 = (Button) this.findViewById( R.id.Button1);
this.Button01.setOnClickListener(this);
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this,Notif.class);
Notification notification = new Notification(R.drawable.icon,
"Notify", System.currentTimeMillis());
notification.setLatestEventInfo(Notif.this,"App Name","Description of the notification",PendingIntent.getActivity(this.getBaseContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
mManager.notify(APP_ID, notification);
}
}
答案 0 :(得分:2)
1)为什么要使用View.OnClickListener的实现处理你的按钮监听器?
到目前为止我看到的标准方法是:
Button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Your code
}
});
2)通知是通过屏幕顶部的Android状态面板通知用户的方法。我不明白你想用通知和对话框做什么 - 下定决心你想要哪一个?
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
如果您确实想要使用通知,那么这就是我在onStop()
方法中所拥有的内容(它基本上就是您从Android指南中获得的内容):
Notification notification = new Notification(R.drawable.icon, "App Name", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, ClassToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "App Name", "Press here to resume", contentIntent);
mNotificationManager.notify(1, notification);
这已经在mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
onCreate()
真的不确定你要做的是什么。