如何从MainActivity类中的另一个类调用setText?

时间:2015-04-08 00:15:55

标签: android broadcastreceiver

我正在尝试在我的MainActivity中设置文本,但我不知道如何让它工作。我知道活动没有实例化,所以我不知道如何将字符串设置为MainActivity中的变量。

我感谢任何帮助



public class MainActivity extends ActionBarActivity {

	TextView textLat;
	TextView textLong;
	TextView textWifi;
	TextView textTime;

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

		textLat = (TextView) findViewById(R.id.textLat);
		textLong = (TextView) findViewById(R.id.textLong);
		textWifi = (TextView) findViewById(R.id.textWifi);
		textTime = (TextView) findViewById(R.id.textTime);



	}
  }






public class ConnectionBroadcast extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(
				android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
			//code
			
		} else if (intent.getAction().equals(
				android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
			//code
			    textWifi.setText(a.toString());
			}
	}


}




3 个答案:

答案 0 :(得分:0)

String tw = textWifi.toString(); //do this in MainActivity Class
textWifi.setText(tw); // in your subclass

答案 1 :(得分:0)

public class MainActivity extends ActionBarActivity {

TextView textLat;
    TextView textLong;
    TextView textWifi;
    TextView textTime;

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

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textWifi = (TextView) findViewById(R.id.textWifi);
        textTime = (TextView) findViewById(R.id.textTime);



    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equals(
                android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
            //code

        } else if (intent.getAction().equals(
                android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            //code
                textWifi.setText(a.toString());
            }
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }
}

答案 2 :(得分:0)

public class MainActivity extends ActionBarActivity implements ConnectionBroadcast.Listener {

    private TextView textLat;
    private TextView textLong;
    private TextView textWifi;
    private TextView textTime;

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

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textWifi = (TextView) findViewById(R.id.textWifi);
        textTime = (TextView) findViewById(R.id.textTime);

        ConnectionBroadcast.getInstance().register(this);
    }

    @Override
    public void onConnect() {

    }

    @Override
    public void onScan() {
        textWifi.setText(a.toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ConnectionBroadcast.getInstance().unRegister(this);
    }
}

将ConnectionBroadcast类定义为单例,并通过接口与其他类进行通信。

public class ConnectionBroadcast extends BroadcastReceiver {

    private List<Listener> listeners = new ArrayList<>();

    private ConnectionBroadcast() {

    }

    private static class SingleHolder {
        private static ConnectionBroadcast instance = new ConnectionBroadcast();
    }

    public static ConnectionBroadcast getInstance() {
        return SingleHolder.instance;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
            synchronized (listeners) {
                for (Listener l : listeners) {
                    l.onConnect();
                }
            }
        } else if (intent.getAction().equals(
            android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            synchronized (listeners) {
                for (Listener l : listeners) {
                    l.onScan();
                }
            }
        }
    }

    public void register(Listener listener) {
        synchronized (listeners) {
            if (listeners.contains(listener)) {
                return;
            }
            listeners.add(listener);
        }
    }

    public void unRegister(Listener listener) {
        synchronized (listeners) {
            if (listeners.contains(listener)) {
                listeners.remove(listener);
            }
        }
    }

    public interface Listener {
        public void onConnect();
        public void onScan();
    }
}