Android 如何创建不包含';在安卓系统中单击鼠标时不会消失?
这对我不起作用Android 如何创建不包含';在安卓系统中单击鼠标时不会消失?,android,notifications,Android,Notifications,这对我不起作用 我怎样才能创建一个可点击并进入我的应用程序,但点击后不会消失的通知?你应该阅读全部内容,而不仅仅是一部分,伙计。请一步一步地仔细阅读 int icon = R.drawable.icon4; CharSequence tickerText = "Hello"; // ticker-text long when = System.currentTimeMillis(); Context context = getApplicationContext
我怎样才能创建一个可点击并进入我的应用程序,但点击后不会消失的通知?你应该阅读全部内容,而不仅仅是一部分,伙计。请一步一步地仔细阅读
int icon = R.drawable.icon4;
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Hello";
CharSequence contentText = "Hello";
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int-icon=R.drawable.ic_启动器;
长时间=System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_服务);
意向意向=新意向(上下文,MainActivity.class);
PendingEvent pending=PendingEvent.getActivity(上下文,0,意图,0);
通知;
if(Build.VERSION.SDK_INT<11){
通知=新通知(图标,“标题”,何时);
notification.setLatestEventInfo(
上下文
“头衔”,
“文本”,
待定);
}否则{
通知=新建通知.Builder(上下文)
.setContentTitle(“标题”)
.setContentText(
“Text”).setSmallIcon(R.drawable.ic_启动器)
.setContentIntent(挂起).setWhen(何时).setAutoCancel(真)
.build();
}
notification.flags |=notification.FLAG_AUTO_CANCEL;
notification.defaults |=notification.DEFAULT_声音;
nm.notify(0,notification);
或者您可以从这里下载直接教程:如果您使用的是Android 5.0>,它变得容易多了,功能有所改变,但您可以使用相同的代码
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification;
if (Build.VERSION.SDK_INT < 11) {
notification = new Notification(icon, "Title", when);
notification.setLatestEventInfo(
context,
"Title",
"Text",
pending);
} else {
notification = new Notification.Builder(context)
.setContentTitle("Title")
.setContentText(
"Text").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pending).setWhen(when).setAutoCancel(true)
.build();
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
nm.notify(0, notification);
确保您处于应用程序上下文中,否则可能需要传递上下文并按如下所示更改源代码
//Some Vars
public static final int NOTIFICATION_ID = 1; //this can be any int
//Building the Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentTitle("BasicNotifications Sample");
builder.setContentText("Time to learn about notifications!");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
您可以在以下位置查看完整的源代码:
要在android.os.Build.VERSION.SDK_INT中工作,下面是代码
if (android.os.Build.VERSION.SDK_INT>16)
{
notificationManager.notify(5, notification.build());
}else
{
notificationManager.notify(5, notification.getNotification());
}
如果你想要在Android中点击时不会消失的通知,请点击这里?
这么定
setLateStevenInfo()
在API级别11中被弃用。必须使用。我使用相同的代码通知。。现在,如果我想在通知中添加按钮,那么如何实现。。。?请帮助meI遇到问题,我将图标设置为绿色,但当通知出现时,它会自动变为白色。这是否正确?您创建了一个pendingent
,但当时不使用它。至少在<11时,它应该是setLatestEventInfo
的最后一个参数,还是我错了?我有一个问题,我将图标设置为绿色,但当通知出现时,它会自动变为白色。类似:我有一个问题,我将图标设置为绿色,但当通知出现时,它会自动变为白色。
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
...
..
.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT>16)
{
notificationManager.notify(5, notification.build());
}else
{
notificationManager.notify(5, notification.getNotification());
}
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
setAutoCancel(false);