バックグラウンド
これは、いくつかの API に慣れるために作成したアプリであり、Android の機能を示すこと以外に実際の目的はありません。Service
フォアグラウンド ( startForeground
) で実行されている があり、タップするとアプリケーションに戻る進行中のNotification
があります。はService
ブロードキャストをリッスンし、DB に記録します。
サービスの開始、通知の作成
私はの使用Notification
をNotificationCompat.Builder
作成します:onCreate
Service
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
// Get the notification manager to update notifications
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent openAppIntent = new Intent(this, MainActivity.class);
openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent selectNotifPendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, 0);
// Build the notification to show
mNotificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Monitor Service")
.setContentText("Logging system events")
.setTicker("Starting monitor service")
.setSmallIcon(R.drawable.ic_stat_cloud)
.setContentIntent(selectNotifPendingIntent)
.setOnlyAlertOnce(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setUsesChronometer(true);
// Start service in foreground
startForeground(MonitorService.NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build());
// more code....
}
がService
起動するNotification
と、すべてのバージョンの Android で と ティッカー テキストの両方が表示されます。
次にアップデートのお知らせ
interface
また、コールバックを myに追加しました。これは、ティッカー テキストとコンテンツ テキストを変更しService
て を更新します。Notification
@Override
public void updateNotificationTicker(String newTickerText) {
Log.d(TAG, "updateNotificationTicker");
if (mNotificationBuilder != null && mNotificationManager != null) {
mNotificationBuilder.setTicker(newTickerText);
mNotificationBuilder.setContentText(newTickerText);
mNotificationManager.notify(NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build());
}
}
の更新をテストするためNotification
に、BroadcastReceiver
タイム ティック ブロードキャストをリッスンupdateNotificationTicker
し、現在の時刻で呼び出しました。
else if (action.equals(android.content.Intent.ACTION_TIME_TICK)) {
SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa", Locale.US);
String message = "The time is now " + sdf.format(new Date());
newLogEntry.setMessage(message);
// Update the ticker notification with the time
if (mNotificationCallback != null) {
mNotificationCallback.updateNotificationTicker(message);
}
}
ティッカー テキストが点滅し、コンテンツ テキストが次のように更新されるため、これは Android の (ほぼ) すべてのバージョンで完全に機能しますNotification
。
- Android 2.2 (API 8)
- アンドロイド 4.0.3 (API 15)
- アンドロイド 4.1.2 (API 16)
- Android 4.2.2 (API 17)
Android 2.3.3 での異なる動作
Android 2.3.3 (API 10) エミュレーターまたはデバイスで実行すると、最初に を起動したときにティッカー テキストが最初に表示されます (最初Service
のスクリーンショット: 「監視サービスの開始」に表示されます)。Notification
ただし、 を更新すると、コンテンツ テキストは更新されますが、ティッカー テキストは表示されません。
質問
NotificationCompat.Builder
Android 2.3.3 でティッカー テキストが表示されないように設定していますか?- または、進行中の更新中にティッカー テキストを表示する場合、Android 2.3.3 は他のバージョンとは異なる動作をします
Notification
か? NotificationCompat.Builder
それとも、 Android 2.3.3 で正常に動作しない不具合ですか?