7

ステータスバーにある同期アイコンを開始および停止したいだけです。NotificationManager を使用した簡単な呼び出しだと思っていましたが、ドキュメントやサンプル Q&A が Web や SO で見つかりません。

4

3 に答える 3

6

私は私の答えを見つけました...

http://libs-for-android.googlecode.com/svn-history/r46/trunk/src/com/google/android/accounts/AbstractSyncService.java

これは、stat_notify_syncアイコンを設定およびキャンセルする方法を示しています。

private void showNotification(String authority) {
    Object service = getSystemService(NOTIFICATION_SERVICE);
    NotificationManager notificationManager = (NotificationManager) service;
    int icon = android.R.drawable.stat_notify_sync;
    String tickerText = null;
    long when = 0;
    Notification notification = new Notification(icon, tickerText, when);
    Context context = this;
    CharSequence contentTitle = "mobi"; //createNotificationTitle();
    CharSequence contentText = "bob"; //createNotificationText();
    PendingIntent contentIntent = createNotificationIntent();
    notification.when = System.currentTimeMillis();
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notificationManager.notify(mNotificationId, notification);
}

private void cancelNotification() {
    Object service = getSystemService(NOTIFICATION_SERVICE);
    NotificationManager nm = (NotificationManager) service;
    nm.cancel(mNotificationId);
}
于 2011-03-23T08:28:04.440 に答える
6

アニメーション化された同期アイコンを取得するには、icon を使用できandroid.R.drawable.ic_popup_syncます。たとえば、最新の通知ビルダーを使用すると、次のようになります。

Notification notification = new NotificationCompat.Builder(mContext)
        .setContentTitle("my-title")
        .setContentText("Loading...")
        .setSmallIcon(android.R.drawable.ic_popup_sync)
        .setWhen(System.currentTimeMillis())
        .setOngoing(true)
.build();
于 2014-01-13T20:50:46.180 に答える
4

あなたの例をありがとう、それは私に時間を節約しました。アプリケーションで静的メソッドを作成したので、コード内のどこからでもアイコンのオン/オフを簡単に切り替えることができます。私はまだそれをアニメートさせることができません。

MyApplication.javaの場合:

private static Context context;
private static NotificationManager nm;

public void onCreate(){
        context = getApplicationContext();
        nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
...
}

public static void setNetworkIndicator(boolean state) {    
    if (state == false) {
        nm.cancel(NETWORK_ACTIVITY_ID);
        return;
    }

   PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    Notification n = new Notification(android.R.drawable.stat_notify_sync, null, System.currentTimeMillis());
    n.setLatestEventInfo(context, "SMR7", "Network Communication", contentIntent);
    n.flags |= Notification.FLAG_ONGOING_EVENT;
    n.flags |= Notification.FLAG_NO_CLEAR;
    nm.notify(NETWORK_ACTIVITY_ID, n);
}

そして、私のアプリケーションのどこからでも:

MyApplication.setNetworkIndicator(true);

MyApplication.setNetworkIndicator(false);
于 2011-04-03T09:50:11.193 に答える