0

メインアクティビティでは、次のようになります。

((TextView)findViewById(R.id.TextView02)).setText(getSharedPreferences("FearAlert", 1).getString("contactName", "Tap to select an Emergency Contact."));
    ((TextView)findViewById(R.id.TextView03)).setText(getSharedPreferences("FearAlert", 1).getString("contactNumber", ""));

今、私は別のアクティビティでcontactNumber値を使用したいのですが、Activty2のactivity2は次のようになります。

 SmsManager.getDefault().sendTextMessage(??, null, "message",null, null, null);
return null;

代わりに何を書くべきですか?上記..助けてください..

4

4 に答える 4

1

Javaには...気にしないでください。プログラミング言語には、変数に値を割り当てることができる変数があります。x=1のように; 次に、x + x=?;を使用できます。そしてあなたに2を与えるでしょう。

ここであなたはすることができます

SharedPreferences sPrefs=getSharedPreferences("FearAlert", 1);
TextView tv=(TextView)findViewById(R.id.TextView02);
String YOUR_INTRESTING_STRING=sPrefs.getString("contactName", "Tap to select an Emergency Contact.");

そして、それを別のアクティビティに渡します。あなたはそれをバンドルに入れることができます

Intent i = new Intent(getApplicationContext(), YOUR_ANOTHER_ACTIVITY.class);
i.putExtra("name_of_value",YOUR_INTRESTING_STRING);
startActivity(i);


そして、AnotherActivityでそれを取得します

Bundle extras = getIntent().getExtras();
String value = extras.getString("name_of_value");
于 2013-01-29T15:30:29.060 に答える
0

SharedPreferencesを正しく作成したと仮定します。

SharedPreferences settings = getSharedPreferences("MY_APP", MODE_PRIVATE);

次の方法で、メソッドの設定を取得できます。

SmsManager.getDefault().sendTextMessage(settings.getString("contactNumber"), null, "message", null, null, null);

「MY_APP」共有設定タグで設定したと仮定します。

settings.edit().putString("contactNumber", "XXX-XXX-XXXX").commit();
于 2013-01-29T15:35:06.407 に答える
0

プリファレンスは通常、名前と値のペアです。これらは、アプリケーションのさまざまなアクティビティ間で「共有設定」として保存できます(現在、プロセス間で共有することはできません)。または、アクティビティに固有に保存する必要があるものにすることもできます(ここでは説明しません)。

コンテキストオブジェクトを使用すると、メソッドを介してSharedPreferencesを取得できます

Context.getSharedPreferences(). 


getSharedPreferences("FearAlert", 1).getString("contactNumber", "");

OR 

 SharedPreferences myPrefs = this.getSharedPreferences("FearAlert", 1);
        String contactNumber = myPrefs.getString(contactNumber, "nothing");
于 2013-01-29T15:30:53.230 に答える
0

次の方法など、アプリのどこからでも設定にアクセスできます。

/**
 * Get a string preference by its key from the apps preferences file
 * 
 * @param key of preference
 * @return value of preference
 */
public String getStringPreference(String key) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    return pref.getString(key, "");
}

キーを設定するだけです。たとえば、「contactName」をパラメータとして設定します。設定マネージャーには、getApplicationContext()を介したアプリケーションコンテキストが必要です。

于 2013-01-29T15:32:52.660 に答える