6

バックグラウンド

これは、いくつかの API に慣れるために作成したアプリであり、Android の機能を示すこと以外に実際の目的はありません。Serviceフォアグラウンド ( startForeground) で実行されている があり、タップするとアプリケーションに戻る進行中のNotificationがあります。はServiceブロードキャストをリッスンし、DB に記録します。

サービスの開始、通知の作成

私はの使用NotificationNotificationCompat.Builder作成します:onCreateService

@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 で と ティッカー テキストの両方が表示されます。

Android 2.3.3 でのサービス通知の起動

次にアップデートのお知らせ

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)

アンドロイド 2.2

アンドロイド 4.0.3

Android 2.3.3 での異なる動作

Android 2.3.3 (API 10) エミュレーターまたはデバイスで実行すると、最初に を起動したときにティッカー テキストが最初に表示されます (最初Serviceのスクリーンショット: 「監視サービスの開始」に表示されます)。Notificationただし、 を更新すると、コンテンツ テキストは更新されますが、ティッカー テキストは表示されません。

アンドロイド 2.3.3

質問

  • NotificationCompat.BuilderAndroid 2.3.3 でティッカー テキストが表示されないように設定していますか?
  • または、進行中の更新中にティッカー テキストを表示する場合、Android 2.3.3 は他のバージョンとは異なる動作をしますNotificationか?
  • NotificationCompat.Builderそれとも、 Android 2.3.3 で正常に動作しない不具合ですか?
4

0 に答える 0