Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java Android中,无论发生什么情况,打开新窗口的通知_Java_Android_Android Activity_Notifications_Android Intent - Fatal编程技术网

在Java Android中,无论发生什么情况,打开新窗口的通知

在Java Android中,无论发生什么情况,打开新窗口的通知,java,android,android-activity,notifications,android-intent,Java,Android,Android Activity,Notifications,Android Intent,我想发出通知。当我点击它时,它会打开应用程序的一个新窗口。 这是我的密码: public class Noficitation extends Activity { NotificationManager nm; static final int uniqueID = 1394885; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub

我想发出通知。当我点击它时,它会打开应用程序的一个新窗口。 这是我的密码:

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

您是否正在尝试在当前活动中打开通知窗口?因为如果你是,我认为你不需要有目的地启动它。除非您已在通知框中创建了自定义视图和活动/服务,否则您通常仅使用意图在应用程序中启动新服务或活动。我看到您在自己的类中设置了它,这很好,但我认为您默认的方式将打开一个全新的视图


如果您需要在某个过程中启动通知或类似于按钮单击,那么您不需要在那里有意图……或者至少我从来没有这样做过:)您到底想通过通知实现什么。

您可能只想在通知栏中添加通知,当用户单击它时,它将启动实际活动。这样用户无论做什么都不会被打断

按如下方式创建状态栏通知:

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);

嗯,我正在运行一项后台服务,每24小时检查一次站点的变化。如果有更改,它将启动通知。我试着在普通班上做,但我有问题。。。如果应用程序在前台不工作,则可以,因为当我单击通知时,它会打开我的主要活动。但是,当应用程序在前台时,它会打开应用程序的一个新屏幕,然后我需要关闭这两个屏幕…如果不查看启动通知的其余代码,我很难给出我的想法。您是说在设备的实际通知栏中发布的通知吗?或者这是用户必须拥有的东西。哈哈,我不想让你看到android开发网站,但是。。。。。。如果我对整件事多了解一点,我可能会更好地帮助您:)您确定服务器端脚本返回了正确的变量吗?到目前为止,在处理客户机/服务器设置的任何应用程序中,我通常会仔细检查我的值是否被传递回客户机。也许你把一些长的,应该是int或者类似的东西传给手机……或者你可以发布一些其他的类代码来调用你的通知类,我们也许可以帮你弄明白:)是的,我想这就是他试图做的……可能只是他设置的一个简单的标志在他的帖子返回服务器或其他什么的时候触发