2014-11-03 2 views
0

Android의 다른 클래스 BroadcastReceiver에서 컨텍스트 인수를 사용하여 메서드를 호출하는 방법을 모르겠습니까?Android에서 다른 클래스 메소드에 컨텍스트 인수를 보내는 방법은 무엇입니까?

아래 코드는 Utils.resetTvProviderDB(this)입니다. TvSettingReceiver.java은 Utils.java를 호출 할 수 없습니다.

TvSettingReceiver.java

public class TvSettingReceiver extends BroadcastReceiver { 

public static final String ACTION_SETTING_CHANGED = 
    "com.lge.tvsettings.ACTION_SETTING_CHANGED"; 
public static final String KEY_SETTING_CHANGED_TYPE = 
    "setting_changed_type"; 

public static final int TYPE_DISPLAY_MODE = 1; 
public static final int TYPE_CLOSED_CAPTION = 2; 
public static final int TYPE_LANGUAGE = 3; 
public static final int TYPE_DVB = 4; 
public static enum CurrentDVB {DVBT, DVBC}; 
public static CurrentDVB currentDVB; 

public TvSettingReceiver() { 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO: This method is called when the BroadcastReceiver is receiving 
    // an Intent broadcast. 
    String intentAction = intent.getAction(); 
    if(intentAction.compareTo(ACTION_SETTING_CHANGED)==0){ 
     handleTvSettingChanged(context, intent); 
    } 
    else if(intentAction.compareTo(Intent.ACTION_LOCALE_CHANGED)==0){ 
     handleLocaleChanged(context, intent); 
    } 
} 

. . .

private void deleteAllChannelDB(CurrentDVB newDVB) { 

    if (currentDVB != newDVB) { 

     Utils.needToResetDtvDB = true; 

     /* reset Dvbt Channels Objects */ 
     DVBTInputService.resetDvbtChannelsObjectsList(); 

     /* remove tv provider all dbs */ 
     try { 
      Utils.resetTvProviderDB(this); // It's impossible 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

Utils.java

class Utils { 

public static void resetTvProviderDB(Context context) throws Exception { 
    Uri channelUri = TvContract.buildChannelsUriForInput(
      "com.lge.tvinput/.DVBTInputService", false); 

    if (DEBUG) 
     Log.d(TAG, " resetTvProviderDBforDvbt uri " + channelUri); 

    String[] projection = { TvContract.Channels._ID }; 

    Cursor cursor = null; // , cursor2 = null; 

. . .

답변

1

onReceive() 메서드에서 전달 된 Context에 대한 참조를 가져 와서 인스턴스 변수에 저장할 수 있습니다. 일단 그렇게하면 resetTvProviderDB()에 전달할 수 있습니다. 코드는 다음과 같습니다.

관련 문제