2012-03-10 4 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이므로 Context에서 getString() 방법을 사용할 수 있습니다. 원하는 문자열의 리소스 ID를 전달하기 만하면 모든 설정이 완료됩니다. AsyncTask에서 문자열에 액세스하기 위해

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

, 나는 대개 다음 내가 원하는 무엇이든 액세스 할 수 있으며, AsyncTask에 ApplicationContext를 전달합니다. Contexts를 전달하는 것은 문제가 될 수 있으므로 실제 문자열을 AsyncTask에 전달하면 Context 참조가 필요하지 않을 수 있습니다.

+0

감사합니다. – user1212077

관련 문제