【问题标题】:notification opening a new window no matter what in Java Android通知在Java Android中打开一个新窗口
【发布时间】:2011-10-01 21:46:26
【问题描述】:

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

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

【问题讨论】:

    标签: java android android-activity notifications android-intent


    【解决方案1】:

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

    【讨论】:

    • 是的,我认为这就是他想要做的......这可能只是一个简单的标志,他设置为在他的帖子返回服务器或其他东西时触发
    【解决方案2】:

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

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

    【讨论】:

    • 嗯,我正在运行一个后台服务,它每 24 小时检查一次站点中的更改。如果有更改,它会启动通知。我尝试在常规课程中进行此操作,但遇到了问题...如果该应用程序无法在前台运行,那没关系,因为当我单击通知时它会打开我的主要活动。但是,当应用程序处于前台时,它会打开一个应用程序的新屏幕,然后我需要关闭它们...
    • 好吧,如果不查看启动通知的其余代码,就很难给你我的想法。您是在谈论在设备上的实际通知栏中发布的通知吗?或者这是用户必须拥有的东西。
    • 大声笑我不想把你指向 android 开发网站但是......developer.android.com/guide/topics/ui/notifiers/… 如果我对整个事情有更多的了解,我可能会更好地帮助你:)
    • 您确定您的服务器端脚本返回了正确的变量吗?在我迄今为止在处理客户端/服务器设置时所做的任何应用程序中,我通常会仔细检查我的值是否传递回客户端。也许您将一些很长的东西传回手机,应该是一个 int 或类似的愚蠢的东西........或者您可以发布一些其他类代码来调用您的通知类,我们可能会能够为你弄清楚:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多