【问题标题】:Notification contenttext from a edittext field来自edittext字段的通知内容文本
【发布时间】:2013-11-25 08:38:17
【问题描述】:
 EditText message = (EditText)findViewById(R.id.mess);
 Notification mNotification = new Notification.Builder(this)
  .setContentTitle("Remainder!")
  .setContentText(message)
  .setSmallIcon(R.drawable.ic_launcher)
  .setSound(soundUri)
  .build();     

它给了我一个错误,说ContentText 应该是CharSequence。 我尝试了几个例子,但没有运气。

【问题讨论】:

    标签: android notifications android-edittext charsequence


    【解决方案1】:

    你必须改变这个:

    String name = message.getText().toString();
    

    并在此之后设置:

    .setContentText(name)
    

    【讨论】:

      【解决方案2】:

      试试这个方法

      setContentText(message.getText().toString())
      

      【讨论】:

        【解决方案3】:

        试试这个:

            @Override
             protected void onMessage(Context context, Intent intent) 
            {
            String message = intent.getExtras().getString("message");
            generateNotification(context, message);
            }
        
        
        
        
        private static void generateNotification(Context context, String message) {
        
            int icon = R.drawable.icon;
                long when = System.currentTimeMillis();
            String appname = context.getResources().getString(R.string.app_name);
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            Notification notification;
        
            Intent intent = new Intent(context, youractivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("message", message);
        
            int requestID = (int) System.currentTimeMillis();
            PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
                    intent, 0);
        
                if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
                    notification = new Notification(icon, message, when);
                    notification.setLatestEventInfo(context, appname, message,
                            contentIntent);
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    notificationManager.notify((int) when, notification);
        
                } else {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(
                            context);
                    notification = builder.setContentIntent(contentIntent)
                            .setSmallIcon(icon).setTicker(appname).setWhen(when)
                            .setAutoCancel(true).setContentTitle(appname)
                            .setContentText(message).build();
        
                    notificationManager.notify((int) when, notification);
        
                }
        }
        

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:

          这样做:

          .setContentText(message.getText().toString())

          检查我的问题here

          【讨论】:

          • 这两种方法都不起作用..第一种方法显示空白..没有消息..第二种方法应用程序崩溃..
          • @shree202.. 无法添加分号.. 分号必须在 .build () 之后
          猜你喜欢
          • 2011-11-04
          • 1970-01-01
          • 2016-09-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多