2012-07-22 39 views
1

我有一個有趣的(嗯,至少對我來說)問題:確保通知自動換

我有需要,我在我的應用程序彈出一個通知。我的Galaxy S II(sprint touch 4g來自sprint,因爲S II有不同的型號,我覺得我需要具體說明),並且通知字包裝沒有問題 - 例如:

this is a notification, but it's too long, 
so it'll show this second line on it's own. 

我的恐懼,我意識到,一些Android手機沒有自動換行,而實際上只是把我的通知,在其屏幕的結束,因此,上述消息將顯示爲:

this is a notification, but it's too long, 

顯然,這是不行的。所以,我能做些什麼來確保整個信息總是顯示出來?

回答

0

重寫

正如我們討論過要強制Notifcation的tickerText爲自動換行之前。一種方法是將文本打入合適長度的線,併發出通知爲每一個:

public class Example extends Activity { 
    public class AlarmReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.v("Example", "Alarm received"); 
      sendNotification(); 
     } 
    } 

    AlarmManager alarmManager; 
    NotificationManager notificationManager; 

    AlarmReceiver alarmReceiver = new AlarmReceiver(); 
    Notification notification = new Notification(); 
    Intent notificationIntent; 
    PendingIntent pendingIntent; 
    int tickerIndex = 0; 
    List<String> tickerTexts = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     // Determine the screen width and density, split tickerText appropriately 
     String tickerText = "A tickerText that is long enough to cause a word wrap in portrait."; 
     tickerTexts.add(tickerText.substring(0, 35)); 
     tickerTexts.add(tickerText.substring(35)); 

     notificationIntent = new Intent(this, Example.class); 
     pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     notification.flags = Notification.FLAG_ONLY_ALERT_ONCE; 
     notification.icon = android.R.drawable.star_on; 

     sendNotification(); 
     registerReceiver(alarmReceiver, new IntentFilter("Generic")); 
    } 

    @Override 
    protected void onDestroy() { 
     unregisterReceiver(alarmReceiver); 
     super.onDestroy(); 
    } 

    public void sendNotification() { 
     Log.v("Example", "Send Notification " + tickerIndex); 

     notification.tickerText = tickerTexts.get(tickerIndex); 
     notification.setLatestEventInfo(this, "Title", "Text " + tickerIndex, pendingIntent); 
     notificationManager.cancel(0); 
     notificationManager.notify(0, notification); 

     tickerIndex++; 
     if(tickerIndex < tickerTexts.size()) { 
      Log.v("Example", "Setting up alarm"); 
      PendingIntent alarmPending = PendingIntent.getBroadcast(this, 0, new Intent("Generic"), 0); 
      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000, alarmPending); 
     } 
    } 
} 

但是你可以看到我跳過的建立不可或缺的一部分:我假設的長度那些適合的文本,我沒有計算出來。爲了確定合適的中斷點,您需要計算屏幕的寬度,字體的縮放大小和密度。你也需要考慮「!!!」可能比「www」更短,具體取決於字體,並且用戶可以在橫向和縱向之間切換......您可以查看源代碼以查看通用Android如何分割tickerText。

無論如何,強制使用tickerText來自動換行可能比壓縮原始字符串或將生氣信件發送給「非包裝」手機制造商更爲複雜。

+0

感謝山姆,這看起來像一個很好的答案,但它實際上並沒有幫助我需要什麼 - 你看,我不會離開我的通知,我只在事件發生時閃爍,所以這贏得了不爲我工作。我同意不希望通知太長 - 我最多缺少1-2個字... – raingod 2012-07-22 18:39:32

+0

通過「閃爍它」,你的意思是在狀態欄中的「tickerText」(我已經「纏繞一次或兩次,如果我能得到它「在圖像中)? – Sam 2012-07-22 18:48:32

+0

是的,確切地說。這就是被切斷.. – raingod 2012-07-23 15:55:47