在通知栏中显示应用程序图标

时间:2014-03-26 15:09:00

标签: android notifications

我正在开发这个应用程序。我想要做的是在点击按钮时在通知栏中显示我的活动图标。我希望它永久留在那里直到其他事件完成。目前我发布了这段代码,但它给出了很多错误,请告诉我一个简单的方法:

save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(icon.isChecked()) {
                    CharSequence text = getText(R.string.local_service_started);

                    // Set the icon, scrolling text and timestamp
                    Notification notification = new Notification(R.drawable.ic_launcher, "Track Your Life", System.currentTimeMillis());

                    // The PendingIntent to launch our activity if the user selects this notification
                    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                            new Intent(), 0);

                    // Set the info for the views that show in the notification panel.
                    notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);

                    // Send the notification.
                    mNM.notify(NOTIFICATION, notification);
                }

            }
        });

1 个答案:

答案 0 :(得分:0)

使用图标

创建通知的简单代码
Intent intentSetDefault = new Intent(mContext, ConversationListActivity.class);
        PendingIntent piSetDeafult = PendingIntent.getActivity(mContext, 0, intentSetDefault,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // create pending intent for action discard
        Intent intentdiscard = new Intent(mContext, NotificationReceiver.class).setType(CODE_DISCARD);
        PendingIntent piDiscard = PendingIntent.getBroadcast(mContext, 0, intentdiscard,
                PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder =
                    new Notification.Builder(mContext)
                            .setTicker(title)
                            .setSmallIcon(R.drawable.ic_stat_notify_incoming_msg)
                            .setContentTitle(title)
                            .setContentText(text)
                            .setLargeIcon(mAppBitmap)
                            .setContentInfo(Integer.toString(smsCount))
                            .setPriority(Notification.PRIORITY_MAX)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setAutoCancel(false);

            mBuilder.setContentIntent(piSetDeafult);

            Notification notification = mBuilder.build();
            notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

            mNotifyMgr.notify(NOTIFICATION_SET_DEFAULT, notification);