无法更改textview(broadcastreceiver)

时间:2014-03-17 08:15:22

标签: android broadcastreceiver

我试图更改我的文本视图,但它无法正常工作。

MainActivity:

public void changeTxt(String txt){
    TextView t = (TextView) findViewById(R.id.wifiName);
    t.setText(txt);
}

广播接收器:

MainActivity ma;
@Override
public void onReceive(Context context, Intent intent) {
    ma= new MainActivity();
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;

    int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    String txt = acCharge+"";
    ma.changeTxt(txt);
}

2 个答案:

答案 0 :(得分:0)

像这样调用registerBroadCastReciever:

//BroadCastReciever variable in base activity or activity
        private BroadcastReceiver serviceBroadcastReceiver;         
//Register reciever method
        void registerBroadcastReceiver(){
        if (serviceBroadcastReceiver != null)
        {
         LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.serviceBroadcastReceiver);
            LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(this.serviceBroadcastReceiver, new IntentFilter("...BROADCAST_STR"));
            return;
        }
        this.serviceBroadcastReceiver = new BroadcastReceiver()
        {
            public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
            {
                //Recieve STH and call method like SetText() that initialize your textView text
            }
        };
        LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(this.serviceBroadcastReceiver, new IntentFilter("...BROADCAST_STR"));
    }       //Then onResume & onPause methods in activity   
    @Override
    public void onResume() {
        super.onResume();
        registerBroadcastReceiver();
    }

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(this.serviceBroadcastReceiver);
    }

答案 1 :(得分:0)

试试这个:

<强> MyActivity:

public class MyActivity extends Activity {

    private TextView wifi;
    private BateryReceiver bateryReceiver;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        wifi = (TextView) findViewById(R.id.wifiName);
        wifi.setText("nothing");

        bateryReceiver = new BateryReceiver(wifi);

    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(bateryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(bateryReceiver);
    }
}

BateryReceiver:(如果您没有,请创建新课程)

public class BateryReceiver extends BroadcastReceiver {

    private final TextView textView;

    public BateryReceiver(TextView textView)
    {
        this.textView = textView;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean ac = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
        boolean usb = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);

        String plugSource = (ac == true)? "AC" : "USB";

        if(isCharging)
            textView.setText("charging: " + isCharging + " by " + plugSource);
        else
            textView.setText("not charging");
    }
}

应该有效。