我想发布一个通知。当我点击它时,它会打开应用程序的一个新窗口。 这是我的代码:
public class Noficitation extends Activity {
NotificationManager nm;
static final int uniqueID = 1394885;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent= new Intent (Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), SchoolBlichActivity.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0);
String body = " body";
String title = "title!";
Notification n =new Notification(R.drawable.table, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(uniqueID,n);
finish();
}
顺便说一下,如果我在 finish() 之前添加 nm.cancel(uniqueID),它会创建通知并立即将其删除...
谢谢你的帮助:D
最佳答案
您可能只想在通知栏中添加一个通知,当用户单击它时,它将启动实际的 Activity。这样,无论用户在做什么,都不会被打扰。
像这样创建状态栏通知:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, myclass.class);
notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello world!", notificationIntent, PendingIntent.getActivity(this, 0, notificationIntent, 0));
mNotificationManager.notify(1, notification);
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
关于java - 通知打开一个新窗口,无论在 Java Android 中是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623172/