gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:08 25 4
gpt4 key购买 nike

是否可以将从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com