Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通知栏-如何在那里写入文本_Android - Fatal编程技术网

Android 通知栏-如何在那里写入文本

Android 通知栏-如何在那里写入文本,android,Android,我想知道如何只在通知栏上写通知文本,在通知布局上写不同的文本 int icon = R.drawable.notification_icon; // icon from resources CharSequence tickerText = "Hello"; // ticker-text long when = System.currentTimeMillis(); // notification time

我想知道如何只在通知栏上写通知文本,在通知布局上写不同的文本

   int icon = R.drawable.notification_icon;        // icon from resources  
   CharSequence tickerText = "Hello";              // ticker-text  
   long when = System.currentTimeMillis();         // notification time  
   Context context = getApplicationContext();      // application Context  
   CharSequence contentTitle = "My notification";  // expanded message title  
   CharSequence contentText = "Hello World!";      // expanded message text  

   Intent notificationIntent = new Intent(this, MyClass.class);  
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);  

    // the next two lines initialize the Notification, using the configurations above  
    Notification notification = new Notification(icon, tickerText, when);  
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  

你应该看看这个链接

NotificationCompat.Builder类有方法
setTicker()
允许您这样做。

接受的答案包含
不推荐的
类和方法

下面是一个使用
NotificationCompat.Builder
生成
通知的示例:

        Intent notificationIntent = new Intent(this, MyClass.class);  
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.my_drawable_resource)
            .setContentTitle("my title")
            .setContentText("my content")
            .setContentIntent(pendingIntent);
        Notification notification = mBuilder.build();
            // default phone settings for notifications
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;

            // cancel notification after click
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
            // show scrolling text on status bar when notification arrives
        notification.tickerText = title + "\n" + content;

            // notifiy the notification using NotificationManager
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

可能会帮助新来者

通知栏中出现的第一个文本由方法定义

  • 标题的文本由定义
  • 内容的文本由定义

这是一个例子:

 private void showCustomNotification(){
        final int NOTIFICATION_ID = 1;
        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.mipmap.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, getString(R.string.app_name), when);
        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Java (programming language)")
                        .setTicker("Java (Open to see the info).")
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setContentText("Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,[14] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.");
       // NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }

请尝试2次搜索,然后自己在此处发布4个问题。检查此答案:这是旧的折旧方法检查此答案不推荐的方法,检查此答案: