在 Java Android 中无论什么情况都会通知打开一个新窗口

发布于 2024-12-07 14:07:27 字数 1006 浏览 1 评论 0原文

我想启动一个通知。当我单击它时,它会打开应用程序的一个新窗口。 这是我的代码:

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

I want to launch a notification. When I click on it, it opens a NEW window of the app.
Here's my code:

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

by the way, if i add nm.cancel(uniqueID) before the finish(), it creates the notification and immediately deletes it...

Thanks for the help :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄟ。诗瑗 2024-12-14 14:07:27

您可能只想在通知栏中添加一个通知,当用户单击它时,它将启动实际的 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

You might want to just add a notification in the notification bar, and when the user clicks it, it will launch the actual Activity. This way the user won't be interrupted in whatever he's doing.

Create the status bar notification like this:

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

冷了相思 2024-12-14 14:07:27

您是否只是想在当前活动中打开通知窗口?因为如果你是,我认为你不需要有意图地启动它。您通常只使用意图在应用程序中启动新服务或活动,除非您构建了将在通知框中发生的自定义视图和活动/服务。我看到你把它设置在自己的类中,这很好,但我认为你默认的做法会打开一个全新的视图。

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

Are you just trying to open a notification window in a current activity? Because if you are I dont think you need to launch it with an intent. You normally only use intents to launch new services or activities in your app unless youve built a custom view and activity/service which is to take place within the notification box. I see you have it set up in its own class which is fine but I think the way your doing it by default would open an entire new view.

If you need to launch a notification during a process or something like a button click you dont need to have the intent there.....or at least I never did :) What exactly are you trying to achieve with the notification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文