方法从另一个类调用

时间:2015-03-23 21:20:33

标签: android class methods

当我从另一个类调用方法SWITCHON()时,它不能按要求运行。 SWITCHON()更改设备的状态。我认为整个类应该运行SWITCHON()方法才能工作。

请告诉我如何让SWITCHON()运行整个类或者它的调用是否正常运行。

注意:此类使用UDP协议在LAN上搜索类似的设备

谢谢

----我想从另一个活动执行一个类----

/**

 * It displays the list of found WeMoDevices, which can be refreshed,
 * notify about additional and removal of a WeMoDevice
 * and provide ability to control the state of the WeMoDevices

 */
 public class WemoActivity extends Activity implements NotificationListener, 
                                                  OnClickListener, 
                                                  OnItemClickListener{
private ListView mListView;
private Button mRefreshButton;
private View mProgressBar;
public Button ONButton;
private WeMoSDKContext mWeMoSDKContext = null;
private String DeviceName= "uuid:Socket-1_0-221412K1100F3A";

public static String newState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wemo);

    mListView = (ListView)findViewById(R.id.list);
    mRefreshButton = (Button)findViewById(R.id.refresh_button);
    mProgressBar = findViewById(R.id.progress_bar);

    mListView.setOnItemClickListener(this);
    mRefreshButton.setOnClickListener(this);

    mWeMoSDKContext = new WeMoSDKContext(getApplicationContext());      
    mWeMoSDKContext.addNotificationListener(this);

    ONButton= (Button) findViewById(R.id.ON);






ONButton.setOnClickListener(new OnClickListener() {         
    @Override
    public void onClick(View v) {

     SWITCHON();

}


});}




/**
 * Refresh the list of found devices on first launch
 * and on coming into foreground
 */
@Override
protected void onStart() {      
    refresh();
    super.onStart();
}

/**
 * Stop WeMoSDKContext on application close
 */
@Override
protected void onDestroy() {
    mWeMoSDKContext.stop();
    super.onDestroy();
}

/**
 * Listens the WeMoDevice events
 * Updates ListView on REFRESH_LIST event, show notification on ADD_DEVICE, REMOVE_DEVICE
 * and change state of corresponding ListItem of ListView on CHANGE_STATE event 
 * 
 *event the event
 *udn the UDN of WeMoDevice provided the event
 */
@Override
public void onNotify(final String event, final String udn) {
    this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            WeMoDevice wemoDevice = mWeMoSDKContext.getWeMoDeviceByUDN(udn);

            if (event.equals(WeMoSDKContext.REFRESH_LIST)) {
                ArrayList<String> udns = mWeMoSDKContext.getListOfWeMoDevicesOnLAN();
                ArrayList<WeMoDevice> wemoDevices = new ArrayList<WeMoDevice>(); 

                for (String udn : udns) {
                    WeMoDevice listDevice = mWeMoSDKContext.getWeMoDeviceByUDN(udn);
                    if (listDevice != null && listDevice.isAvailable()) {
                        wemoDevices.add(listDevice);
                    }
                }
                mListView.setAdapter(new Adapter(getApplicationContext(), 0, wemoDevices));
                mProgressBar.setVisibility(View.GONE);                  
                mRefreshButton.setEnabled(true);
            } else if (wemoDevice == null) {
                //do nothing because of incorrect notification
            } else if (event.equals(WeMoSDKContext.ADD_DEVICE)) {
                Toast.makeText(getApplicationContext(), 
                               getString(R.string.notification_add) + wemoDevice.getFriendlyName(), 
                               Toast.LENGTH_SHORT).show();  
            } else if (event.equals(WeMoSDKContext.REMOVE_DEVICE)) {
                Toast.makeText(getApplicationContext(), 
                               getString(R.string.notification_remove) + wemoDevice.getFriendlyName(),
                               Toast.LENGTH_SHORT).show();
            } else if (event.equals(WeMoSDKContext.CHANGE_STATE) || event.equals(WeMoSDKContext.SET_STATE)) {
                for(int i = 0; i <= mListView.getLastVisiblePosition() - mListView.getFirstVisiblePosition(); i++) {
                    ListItem listItem = (ListItem)mListView.getChildAt(i);
                    if (listItem.getDevice().getUDN().equals(udn)) {
                        listItem.setState(wemoDevice.getState().split("\\|")[0]);
                        break;
                    }
                }
            }               
        }
    }); 
}

/**
 * The adapter to ListView
 * 

 */
private class Adapter extends ArrayAdapter<WeMoDevice> {
    private ArrayList<WeMoDevice> mDevices;

    /**
     * Constructor 
     * 
     * context application context
     * resource (not used)
     * devices the array of WeMoDevices to display
     */
    public Adapter(Context context, int resource, ArrayList<WeMoDevice> devices) {
        super(context, resource, devices);
        mDevices = devices;
    }

    /**
     * Creates ListItem corresponding to WeMoDevice
     *   
     *  position the position of WeMoDevice in entry array
     *  convertView the created view, corresponded to the WeMoDevice
     * @ parent (not used)
     * 
     * @return created ListItem view
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new ListItem(getApplicationContext());
        }
        ((ListItem)convertView).setDevice(mDevices.get(position));

        return convertView;
    }
}

/**
 * Handle the click on refresh button 
 * and update the device list
 */
@Override
public void onClick(View view) {
    if (view.getId() == R.id.refresh_button) {
        refresh();
    }       
}

/**
 * Updates the list of WeMoDevices in WeMoSDKContext
 */
private void refresh() {
    mProgressBar.setVisibility(View.VISIBLE);
    mRefreshButton.setEnabled(false);
    mWeMoSDKContext.refreshListOfWeMoDevicesOnLAN();
}

/**
 * Change the state of a WeMoDevice 
 * 
 * list ListView
 *  view ListItem
 * position the position of current ListItem in ListView
 *  id the id of the current view
 */
@Override
public void onItemClick(AdapterView<?> list, View view, int position, long id) {



    WeMoDevice device = ((ListItem)view).getDevice(); 



    // change the state of switches 

    String state = device.getState().split("\\|")[0];


         newState = WeMoDevice.WEMO_DEVICE_ON;

        if (state.equals(WeMoDevice.WEMO_DEVICE_ON) || state.equals(WeMoDevice.WEMO_DEVICE_STAND_BY)) {
            newState = WeMoDevice.WEMO_DEVICE_OFF;
        }

        mWeMoSDKContext.setDeviceState(newState, device.getUDN());
        ((ListItem)view).setState(WeMoDevice.WEMO_DEVICE_UNDEFINED);
}


public void SWITCHON(){



    mWeMoSDKContext.setDeviceState(WeMoDevice.WEMO_DEVICE_ON, DeviceName);
}

public void SWITCHOFF(){


    mWeMoSDKContext.setDeviceState(WeMoDevice.WEMO_DEVICE_OFF, DeviceName);
}



}

1 个答案:

答案 0 :(得分:0)

您应该将WeMoSDKContext和switchOn()方法放在可以从任何Activity(例如ManagerWeMo)访问的单独类中。然后,您可以调用ManagerWeMo.switchOn()。

更新:

您需要执行的代码应包含在ManagerWeMo中。如果您的Activity中有代码取决于ManagerWeMo的结果,那么将您的Activity设置为侦听器界面,将其传递给ManagerWeMo,并让ManagerWeMo报告回您的Activity。