无法从BroadcastReceiver更改TextView内容

时间:2014-10-11 09:16:41

标签: android string broadcastreceiver getter-setter

我正在尝试从BroadcastReceiver获取字符串,该字符串将在我的活动中设置TextView的内容。

我有两个Java类

  1. MainActivity:它有TextView我想要更改的文本。

  2. batteryinfo:它扩展了BroadcastReceiver。这个班有 返回自定义String(getString())

  3. 的方法

    以下是MainActivity的代码

    package com.example.batteryhealth;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.BatteryManager;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        BatteryManager bm;
        TextView health;
        String string;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            health=(TextView)findViewById(R.id.textView1);
            //status=(TextView)findViewById(R.id.textView2);
            batteryinfo bmc= new batteryinfo(bm,string);
            this.registerReceiver(bmc, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    
            health.setText(bmc.gethealth());
    
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        } 
    

    以下是batteryinfo的代码:

    package com.example.batteryhealth;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.BatteryManager;
    
    public class batteryinfo extends BroadcastReceiver{
    
        BatteryManager bm;
        String health;
    
        public batteryinfo(BatteryManager b,String hea) {
            // TODO Auto-generated constructor stub
    
            this.bm=b;
            this.health=hea;
    
        }
    
    
    
    
        @Override
        public void onReceive(Context arg0, Intent intent) {
            // TODO Auto-generated method stub
        int i= intent.getIntExtra(bm.EXTRA_HEALTH, 0);
    
            boolean s=true;
    
    
            if(s=i==bm.BATTERY_HEALTH_COLD)
            {
               health="cold";
    
            }
            else if(s=i==bm.BATTERY_HEALTH_GOOD)
            {
                health="good";
            }
            else if (s=i==bm.BATTERY_HEALTH_OVERHEAT)
    
            {
                health="overheat"       ;
            }
            else if (s=i==bm.BATTERY_HEALTH_OVER_VOLTAGE)
            {
                health="overvolteage";
            }
    
        }
    
        public String gethealth()
        {
            return health;
        }
    
    }
    

    如果我在MainActivity中创建一个嵌套类(也扩展了BroadcastReciver),代码工作正常,但在创建一个单独的类后它不起作用,我的TextView为空。

1 个答案:

答案 0 :(得分:0)

在MainActivity中注册新的广播接收器,然后从您的batteryinfo类向MainActivity类发送广播,并更改MainActivity接收器中的文本