【问题标题】:Android: Adding text from textView to status barAndroid:将文本从 textView 添加到状态栏
【发布时间】:2013-10-26 18:52:28
【问题描述】:

是否可以将从 textView 获取的字符串添加到状态栏。我想不断地显示文本,从应用程序的主要活动中动态更新为 textView。

TextView message = (TextView) findViewById(R.id.textView1);
message.setText("This I want to add to status bar");

【问题讨论】:

  • 我的意思是通知栏,它总是在屏幕的顶部
  • 所以你想使用通知?
  • 是的,但我只需要在时钟旁边显示变量。
  • 你找到如何在通知栏添加文字了吗?

标签: java android textview statusbar


【解决方案1】:

是的,你可以

private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_status;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name); // Here you can pass the value of your TextView
        Intent notificationIntent = new Intent(context, SplashScreen.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

【讨论】:

  • 我试过这种方式,一切都很好,但我还想在图标旁边添加变量的值,在那个常规的向上栏上。
  • 使用上述方法,获取textview的String并传递给上述方法。
  • 是的,但我想一直显示它,而不是一秒钟。它称为股票代码文本或类似的东西
【解决方案2】:

TextView 消息 = (TextView) findViewById(R.id.textView1); message.setText("这个我要添加到状态栏");

ActionBar actionBar = getSupportActionBar(); actionBar.setTittle(message.getText().toString());

这应该可以解决您的问题。

【讨论】:

    【解决方案3】:

    是的,可以将文本从 textview 显示到状态栏,您必须创建 notification 这是关于如何在运行时更新通知文本的 guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      相关资源
      最近更新 更多