android– 如何显示从服务类更新的Activity的文本字段中的值?

android– 如何显示从服务类更新的Activity的文本字段中的值?,第1张

概述这是我的服务类,我根据时间增加i值…publicclassBackServiceextendsService{inti=0;@OverridepublicIBinderonBind(Intentarg0){returnnull;}@OverridepublicvoidonCreate(){pollForUpdates();super

这是我的服务类,我根据时间增加i值…

    public class BackService extends Service {    int i=0;    @OverrIDe    public IBinder onBind(Intent arg0) {        return null;    }    @OverrIDe    public voID onCreate() {        pollForUpdates();        super.onCreate();    }    @OverrIDe    public voID onStart(Intent intent, int startID) {        super.onStart(intent, startID);    }    private voID pollForUpdates() {        Timer timer=new Timer();        timer.scheduleAtFixedrate(new TimerTask() {            @OverrIDe            public voID run() {                    Log.v("Service class called", "service class called "+i);                    getRunningApps();                    i++;            }        },0,1000);    }    private voID getRunningApps()    {    }    @OverrIDe    public voID onDestroy() {        super.onDestroy();    }}

我想将i值附加到TextVIEw.即TextVIEw值根据i值动态变化…

public class MainActivity extends Activity {@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    startService(new Intent(this, BackService.class));    TextVIEw tx=(TextVIEw)findVIEwByID(R.ID.textVIEw1);}}

如何将i值附加到tx …

先感谢您..

解决方法:

用这个

public class MainActivity extends Activity {     TextVIEw tx;     RefreshbroadcastReciver mbroadCastReciver;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    startService(new Intent(this, BackService.class));    tx =(TextVIEw)findVIEwByID(R.ID.textVIEw1);}@OverrIDeprotected voID onResume() {    super.onResume();    mbroadCastReciver = new RefreshbroadcastReciver();    registerReceiver(mbroadCastReciver, new IntentFilter("sendData"));}private class RefreshbroadcastReciver extends broadcastReceiver {    @OverrIDe    public voID onReceive(Context context, Intent intent) {        tx.setText(intent.getIntExtra("i", 0)+"");}}@OverrIDeprotected voID onStop() {    super.onStop();    if(mbroadCastReciver!=null)    unregisterReceiver(mbroadCastReciver);}}

而且你的服务就在这里

public class BackService extends Service {int i=0;Intent intent1;@OverrIDepublic IBinder onBind(Intent arg0) {    return null;}@OverrIDepublic voID onCreate() {    pollForUpdates();    super.onCreate();}@OverrIDepublic voID onStart(Intent intent, int startID) {    super.onStart(intent, startID);     intent1=new Intent("sendData");}private voID pollForUpdates() {    Timer timer=new Timer();    timer.scheduleAtFixedrate(new TimerTask() {        @OverrIDe        public voID run() {                Log.v("Service class called", "service class called "+i);                getRunningApps();                i++;                Message msg=new Message();                msg.arg1=i;                handler.sendMessage(msg);        }    },0,1000);}private voID getRunningApps(){}@OverrIDepublic voID onDestroy() {    super.onDestroy();}protected Handler handler = new Handler() {    public voID handleMessage(androID.os.Message msg) {        intent1.putExtra("i", msg.arg1);        sendbroadcast(intent1);    }};}
总结

以上是内存溢出为你收集整理的android – 如何显示从服务类更新的Activity的文本字段中的值?全部内容,希望文章能够帮你解决android – 如何显示从服务类更新的Activity的文本字段中的值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106228.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存