2013-04-09 13 views
6

何か手助けが必要です。 アンドロイドのステータスバーに通知を表示しようとしています。Android拡張可能な表示なしでステータスバーに通知を表示します。

これは、サーバーとの同期が進行中であることをユーザーに通知します。

は今、私はこのコードを使用し、それが正常に動作します:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(android.R.drawable.stat_notify_sync).setWhen(System.currentTimeMillis()) 
      .setAutoCancel(false).setOngoing(true); 

    NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notif = builder.getNotification(); 
    nm.notify(1, notif); 

唯一の問題は、ユーザがステータスバーを展開するとビューが作成されていることです。私がしたいのは、コンテンツビューなしでステータスバーの上部に小さなアイコンを表示するだけです。

誰でも知っていますか? ありがとうございます!

+0

私が言うCommonsWareからいくつかの答えを見てそれは不可能だが、私もsで見たアプリ(例: Yahoo Mail) –

答えて

3

ステータスバーに通知アイコンを表示するには、このメソッドを使用することができます。

これは、ユーザーがステータスバーを見てアイコンを見ると、そのアイコンの意味を理解するための方法があるはずです。したがって、各アイコンに対応する通知パネルに行がなければなりません。

私は、あなたの質問で行ったように、同期が進行中であることを説明する通知を作成することをお勧めします。これは、どのアプリが現在同期しているかを示す素晴らしい機会です(何らかの問題がある場合、または同期が永遠に続く場合、ユーザーは注意が必要なアプリを知っています)。

+0

あなたの答えをありがとう。 私は、Yahoo Mailが消耗しないビューなしで通知をするのを見たので、それは奇妙です... –

+0

本当ですか?通知シェードに行がないアイコンを作成することは本当に不可能です。あなたはスクリーンショットや何かに私を向けることができますか? – dsandler

+0

私はYahoo Mailに間違っています.Yahooアプリのアイコン表示はOSによって表示される同期アイコンです。 アプリがサーバー接続を実行しているときに別の方法でユーザーに通知します。 ありがとうございます! –

1

あなたはこれが不可能な

private void showNotification() { 
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // In this sample, we'll use the same text for the ticker and the 
    // expanded notification 

    // Set the icon, scrolling text and timestamp 
    Notification notification = new Notification(R.drawable.ic_launcher, 
      "Service Started", System.currentTimeMillis()); 
    // The PendingIntent to launch our activity if the user selects this 
    // notification 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, MainActivity.class), 0); 
    // Set the info for the views that show in the notification panel. 
    notification.setLatestEventInfo(this, getText(R.string.service_label), 
      "Service Started", contentIntent); 
    // Send the notification. 
    // We use a layout id because it is a unique number. We use it later to 
    // cancel. 
    nm.notify(R.string.service_started, notification); 

} 
+0

あなたの答えをありがとうが、それは私が欲しいものではありません。 あなたのコードは私のものと同じことを行います(この方法で通知をインスタンス化することは推奨されていません)。通知パネルには表示したくないのですが、ステータスバーの小さなアイコン 通知パネルのビューを配置しない場合、この例外は発生します。 'java.lang.IllegalArgumentException:contentViewが必要です:pkg = com.test.app.notification id = 1 notification =通知(pri = 0 contentView = null振動=ヌルサウンド=ヌルデフォルト= 0x0フラグ= 0x0 kind = [null]) ' –

0

これはInputMethodServiceで可能です。単にshowStatusIcon(resource_name)を使用します。 しかし、これは入力サービス以外の何ものに対しても良い考えではないと思います。

0
Context context = TutListDownloaderService.this 
.getApplicationContext(); 
NotificationManager notificationManager = (NotificationManager) context 
.getSystemService(NOTIFICATION_SERVICE); 

通知を作成します。

Notification updateComplete = new Notification(); 
updateComplete.icon = android.R.drawable.stat_notify_sync; 
updateComplete.tickerText = context.getText(R.string.notification_title); 
updateComplete.when = System.currentTimeMillis(); 

は保留中のオブジェクトを作成します。

Intent notificationIntent = new Intent(context,TutListActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
notificationIntent, 0); 

String contentTitle = context.getText(R.string.notification_title).toString(); 
String contentText; 
if (!result) { 
Log.w(DEBUG_TAG, "XML download and parse had errors"); 
contentText = context.getText(R.string.notification_info_fail).toString(); 
} else { 
contentText = context.getText(
    R.string.notification_info_success).toString(); 
} 
updateComplete.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

は、最後にユーザーへの通知を示しています。

notificationManager.notify(LIST_UPDATE_NOTIFICATION, updateComplete); 
関連する問題