0

私が知っているように、Javaは常に「値渡し」によってメソッドを呼び出します。しかし、AndroidのNotificationManager.notify(String、int、Notification)のリファレンスが表示されます。

戻り値

 the id of the notification that is associated with the string

通知をキャンセルするために使用できる識別子

リファレンスを参照してください:http: //developer.android.com/reference/android/app/NotificationManager.html

どうしてこれが起こったのでしょうか?誤解していることはありますか?

BR、ヘンリー

4

4 に答える 4

3

この声明について:

「Java はプリミティブを値で渡しますが、オブジェクトは参照で渡します。」

これは正確ではありません。Java はすべてを値で渡し、オブジェクトをまったく渡しません。

  • プリミティブの場合:コピーはメソッドに送信されます(Stringはプリミティブではないことを忘れないでください)-あなたの言ったことは正しいです
  • 参照変数の場合: 値によっても送信されます:参照変数のコピーがメソッドに送信されます。したがって、オブジェクト自体は送信されません。オブジェクトはメソッド内で (メソッドのいくつかを呼び出すことによって) 変更することができ、メソッドから戻った後に変更が表示されます (たとえば、Person オブジェクトから "name" メンバーを変更します)。変更はメソッドの外では見えません:

参照の変更は、「new」演算子または param = some_other_reference (some_other_referece がヒープ上の他のオブジェクトを指す) のような代入によって行われます。参照を変更しても、「元の」参照には影響しませんが、「コピー参照」(メソッド内で使用される参照) のみに影響します。

于 2010-11-12T09:07:05.897 に答える
0

以前のポスターに同意します。ドキュメントが正しくないようです。このメソッドには3つのパラメーターが必要ですが、javadocではそのうちの2つだけが言及されています。@returnを@paramに置き換えるだけで、idパラメーターの説明が得られます。

通知をキャンセルするために使用できる文字列識別子に関連付けられている通知のID

編集:このIDを自分で定義し、後で通知をキャンセルするために使用できます。

于 2010-11-12T10:05:50.113 に答える
0

NotificationManagerのAPI リファレンスが少し混乱しているようです。

NotificationManager および Android で Google Code Search を介して見つかったコードは次のとおりです。

/**
 * Persistent notification on the status bar,
 *
 * @param tag An string identifier for this notification unique within your
 *        application.
 * @param notification A {@link Notification} object describing how to
 *        notify the user, other than the view you're providing. Must not be null.
 * @return the id of the notification that is associated with the string identifier that
 * can be used to cancel the notification
 */
public void notify(String tag, int id, Notification notification)
{
    int[] idOut = new int[1];
    INotificationManager service = getService();
    String pkg = mContext.getPackageName();
    if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
    try {
        service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
        if (id != idOut[0]) {
            Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
        }
    } catch (RemoteException e) {
    }
}

明らかに、パラメーターは値を返しません。彼らは同様の JavaDoc を持つつもりでしたが、おそらく間違いを犯しました。

の他のバリアントのコードを見てくださいnotify

/**
 * Persistent notification on the status bar,
 *
 * @param id An identifier for this notification unique within your
 *        application.
 * @param notification A {@link Notification} object describing how to
 *        notify the user, other than the view you're providing. Must not be null.
 */
public void notify(int id, Notification notification)
{
    notify(null, id, notification);
}

ご覧のとおり、このオーバーロードされたバージョンは、デフォルトのtagString 値であるプライマリ実装を呼び出すだけですnull


値渡しと参照渡しの一般的な質問に関して、単純/下品な説明は次のとおりです。

  • Java はプリミティブを値で渡します。
  • ただし、参照によってオブジェクトを渡します。

明確にするために、arnivan と Patrick によるコメントを参照してください。

于 2010-11-12T08:28:24.313 に答える
0

ドキュメントは私には間違っているようです。宣言は次のように述べています。

public void notify (String tag, int id, Notification notification)

しかし同時に、それは何かを返すと言います。

私はこれを次のように解釈します。idは問題の通知に一意にマップされ、通知をキャンセルするときに使用できます。

于 2010-11-12T08:28:52.737 に答える