开始启动一个Android的通知 [英] Starting an Android notification at startup

查看:191
本文介绍了开始启动一个Android的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题不基于我的这个previous问题: Android的 - 设置通知永远不会​​消失

This question is based off of this previous question of mine: Android - Set notification to never go away

我现在意识到我需要的东西在启动时被触发,触发通知。

I now realize that I need something to be triggered at startup to trigger the notification.

从我的previous问题,它看起来像我可以采取两种途径。无论是启动一个服务,或者使用一个BroadcastReceiver。我认为,启动的服务是有点矫枉过正(但我知道什么)。同时与广播接收器的路线去我被困。它的工作原理一般,但是当我把我的code的通知我收到了一堆错误。

From my previous question, it looks like I can take two routes. Either start a service, or use a BroadcastReceiver. I think that starting a service is a little overkill (but what do I know). I am stuck while going with the BroadcastReceiver route. It works in general, but when I take my code for a notification I get a bunch of errors.

这是我的code:

NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(this, ResultActivity.class);
            //Intent.putExtra("My Notification");
            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(ResultActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(i, mBuilder.build());

我的错误:

My errors:

在这个任何想法?

摘要:
是一个服务或广播接收器(在这种情况下)更好?
如何在我的code解决这些错误(当你徘徊,他们说,他们是不确定的)?

Summary: Is a service or broadcast receiver better (in this situation)? How do I solve these errors in my code (when you hover they say they are undefined)?

推荐答案

替换的所有实例这种情况下错误行

BroadcastReciever没有实现不同的活动和服务环境。在其中有错误的所有方法都需要上下文的实例

BroadcastReciever does not implement a context unlike Activity and Service. All methods in which you have error require an instance of context

这篇关于开始启动一个Android的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆