java - Android:将文本从textView添加到状态栏

标签 java android textview statusbar

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

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

最佳答案

是的,可以

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);
    }

关于java - Android:将文本从textView添加到状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19610598/

相关文章:

android - 有什么特别的方法可以让系统卸载弹出窗口出现在android中的监听器

android - 在 TextView 中显示整数变量

java - Android - 存储或创建 3d 模型

java - 如何使用 TimePicker 获取今天的日期/时间?

javascript - 两次创建一个对象会产生不同的结果

android - 设置可扩展列表的样式

android - TextView 右侧的 SwitchCompat 被推到看不见的地方

一个TextView中的Android多色

java - 不确定将java代码放在哪里以防止列困惑

java.sql.SQLException : ResultSet may only be accessed in a forward direction