手机启动时状态栏通知打开 Activity

发布于 2024-12-21 23:11:55 字数 1494 浏览 0 评论 0原文

我创建了一项服务,该服务会在一定时间间隔后显示状态栏通知。 我还创建了广播接收器,它在手机重新启动或开机时启动服务。我面临的问题是,当手机重新启动时,我在栏中看到通知,但之后应用程序启动。我不希望应用程序自行启动,它应该仅在用户单击通知时启动。

我的广播接收器代码:

@Override
    public void onReceive(Context context, Intent intent) {



        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {


            try
            {
                Intent intent1 = new  Intent(context, NotificationService.class);
                context.startService(intent1);

            }
            catch(Exception e)
            {

            }


        }

    }

我的通知代码是:

public static void showNotification(Context context )
    {


    NotificationManager notificationManager = (NotificationManager)         context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher, "Pull Me Down!!", 1000);
    Intent intent = new Intent(context,X.class);
    PendingIntent  pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    notification.setLatestEventInfo(context, "I came!!", "My First Notifcation" , pendingIntent);
    notificationManager.notify(MY_ID, notification);

    }

我在我的服务的 onCreate 中调用上述方法。并在我的 X 活动类中调用它:

NotificationService.setActivity(StatusBarNotificationActivity.this);
                startService(new Intent(getApplicationContext(), NotificationService.class));

但不知道为什么当手机启动时会显示通知,但几秒钟后 X 活动也会启动。

I have created a service which displays a status bar notification after certain time interval.
I also have created broadcast receiver which starts the service when phone restarts or power on. The problem I am facing is that when phone restarts , I see the notification in bar, but after that the application launches. I don't want the application to launch itself, it should only launch when user clicks on the notification.

My code for Broad Cast Receiver:

@Override
    public void onReceive(Context context, Intent intent) {



        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {


            try
            {
                Intent intent1 = new  Intent(context, NotificationService.class);
                context.startService(intent1);

            }
            catch(Exception e)
            {

            }


        }

    }

My code for notification is :

public static void showNotification(Context context )
    {


    NotificationManager notificationManager = (NotificationManager)         context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher, "Pull Me Down!!", 1000);
    Intent intent = new Intent(context,X.class);
    PendingIntent  pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    notification.setLatestEventInfo(context, "I came!!", "My First Notifcation" , pendingIntent);
    notificationManager.notify(MY_ID, notification);

    }

I am calling above method in onCreate of my Service. and also calling it in my X activity class:

NotificationService.setActivity(StatusBarNotificationActivity.this);
                startService(new Intent(getApplicationContext(), NotificationService.class));

But don't know why when phone starts notification is show but after few seconds the X activity also launches.

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

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

发布评论

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

评论(2

顾挽 2024-12-28 23:11:55

解决了,只需在方法外部声明 NotificationManagerNotification 即可。

Solved it, just declared the NotificationManager and Notification outside the method, and it worked.

枕梦 2024-12-28 23:11:55

也许你可以尝试使用“前台服务”。要使用前台服务,您只需调用 startForeground 方法。

startForeground(NOTIFICATION_ID, notification);

但是,如果您不希望通知始终显示在状态栏上,则必须控制服务生命周期。希望这个答案对您有帮助。 :)

Maybe you can try to use "Foreground service". To use foreground service you just need to call startForeground method.

startForeground(NOTIFICATION_ID, notification);

But you have to control the service life-cycle if you don't want the notification always shows on the status bar. Hope this answer is helpful to you. :)

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