2014-11-03 14 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()。所以,代碼看起來像這樣:

private Context mContext; 

public void onReceive(Context context, Intent intent) { 
    ... 

    mContext = context; 

    ... 
} 

... 

private void deleteAllChannelDB(CurrentDVB newDVB) { 

    if (currentDVB != newDVB) { 

    ... 

     try { 
      Utils.resetTvProviderDB(mContext); // <- This is possible 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 
相關問題