2013-10-26 48 views
2

是否可以將從textView中獲取的字符串添加到狀態欄。我想不斷地顯示文本,動態更新爲來自應用程序主要活動的textView。Android:將textView中的文本添加到狀態欄

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

你的意思是操作欄? – Raghunandan

+0

我的意思是通知欄,它始終在屏幕上方 –

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

我試過這種方式,一切都很好,但我也想在圖標旁邊增加變量值,在那個常規欄上。 –

+0

使用上面的方法,並獲取textview的String並將其傳遞給上述方法。 –

+0

是的,但我想要一直顯示它,而不是一秒鐘。它被稱爲ticker文本或類似的東西 –

0

是的,它可以顯示從TextView的文本狀態欄,你必須創建notification這裏是關於如何在運行時更新通知文本的guide

1

TextView message =(TextView)findViewById(R.id.textView1); message.setText(「我想添加到狀態欄」);

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

這應該解決您的問題。

相關問題