是否可以将从textView中获取的字符串添加到状态栏。我想不断地显示文本,动态更新为来自应用程序主要活动的textView。
TextView message = (TextView) findViewById(R.id.textView1);
message.setText("This I want to add to status bar");
答案 0 :(得分:2)
是的,你可以
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);
}
答案 1 :(得分:1)
TextView message =(TextView)findViewById(R.id.textView1); message.setText(“我想添加到状态栏”);
ActionBar actionBar = getSupportActionBar(); actionBar.setTittle(message.getText()的toString());
那应该解决你的问题。
答案 2 :(得分:0)
是的,可以显示从textview到状态栏的文本,你必须创建notification这里是关于如何在运行时更新通知文本的guide。