2012-03-10 9 views
1

私のアプリケーションにはサービスがあり、必要なものが含まれているstrings.xmlファイルにアクセスするにはどうすればいいですか?サービスでstrings.xmlファイルを使用する方法は?

strings.xmlにある文字列を取得したいのですが、私のサービスでこのファイルにアクセスできません。

私はこの問題が文脈を扱うと思っていますが、解決できません。あなたの助けのための

package com.receiver; 

protected Void doInBackground() { 

    // GPS timeout 
    final int maxSeconds = 60; //30 
    int count = 0; 

    // If we've finished finding location or exceeded timeout, 
    // break the loop. 
    while (findingLocation && count < maxSeconds) { 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // Continue if interrupted 
     } 
     // Increment the timer by one second 
     count++; 
    } 

    // If we're still finding location, switch to network locations. 
    if (findingLocation) { 
     locationHandler.sendMessage(new Message()); 
     locationManager.removeUpdates(SMSReceiver.this); 
     lat = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLatitude(); 
     lng = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLongitude(); 
     msg = "";// here I whant to get my string from the strings.xml but I can't access to this file 
    } else { 
     lat = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude(); 
     lng = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude(); 
     msg = " "; // here I whant to get other string from the strings.xml but I can't access to this file 
} 

ありがとう:

私のサービスの抽出物があります。

私の悪い英語のために申し訳ありません。

答えて

3

あなたはAndroidサービスを利用していますか?あなたのコードスニペットから、AsyncTaskから文字列にアクセスするように見えます。

通常のAndroidサービスではがContextになるので、getString()の方法をContextから使用できます。必要な文字列のリソースIDを渡すだけで、すべての設定が完了しているはずです。

http://developer.android.com/reference/android/content/Context.html#getString(int

AsyncTaskから文字列にアクセスするためには、私は通常ちょうど、私は私が欲しいものにアクセスすることができ、AsyncTaskへのApplicationContextを渡します。 Contextsを渡すことは問題になる可能性があるので、AsyncTaskに実際の文字列を渡して、Contextの参照が不要であることを覚えておいてください。

+0

ありがとうございました。 – user1212077

関連する問題