我有一个有趣的(至少对我而言)问题:
我有一个通知,我会根据需要在我的应用中弹出。一切都在我的Galaxy S II上很棒(史诗般的冲刺4g,因为那里有不同型号的S II,我觉得我需要具体),并且通知词包装没有问题 - 例如:< / p>
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 :(得分: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进行自动换行可能比缩小原始字符串或向“非包装”手机制造商发送愤怒的字母更复杂。