Android 状态栏通知在手机启动时打开活动

Android 状态栏通知在手机启动时打开活动,android,service,android-activity,notifications,broadcastreceiver,Android,Service,Android Activity,Notifications,Broadcastreceiver,我已经创建了一个服务,在特定时间间隔后显示状态栏通知。 我还创建了广播接收器,当手机重新启动或通电时启动服务。我面临的问题是,当手机重新启动时,我会在工具栏中看到通知,但之后应用程序就会启动。我不希望应用程序自行启动,它应该只在用户单击通知时启动 我的宽频广播接收机代码: @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BOOT_COMPLETED

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

我的宽频广播接收机代码:

@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活动也会启动。

解决了这个问题,只是在方法之外声明了
通知管理器
通知,它就工作了。

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

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