手机启动时状态栏通知打开 Activity
我创建了一项服务,该服务会在一定时间间隔后显示状态栏通知。 我还创建了广播接收器,它在手机重新启动或开机时启动服务。我面临的问题是,当手机重新启动时,我在栏中看到通知,但之后应用程序启动。我不希望应用程序自行启动,它应该仅在用户单击通知时启动。
我的广播接收器代码:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了,只需在方法外部声明
NotificationManager
和Notification
即可。Solved it, just declared the
NotificationManager
andNotification
outside the method, and it worked.也许你可以尝试使用“前台服务”。要使用前台服务,您只需调用 startForeground 方法。
但是,如果您不希望通知始终显示在状态栏上,则必须控制服务生命周期。希望这个答案对您有帮助。 :)
Maybe you can try to use "Foreground service". To use foreground service you just need to call startForeground method.
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. :)