3

这是我的服务类,它根据时间i增加值......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 {

@Override
public 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...

先感谢您..

4

2 回答 2

2

用这个

public class MainActivity extends Activity {
     TextView tx;
     RefreshBroadcastReciver mBroadCastReciver;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    startService(new Intent(this, BackService.class));
    tx =(TextView)findViewById(R.id.textView1);

}
@Override
protected 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)+"");

}
}
@Override
protected void onStop() {
    super.onStop();
    if(mBroadCastReciver!=null)
    unregisterReceiver(mBroadCastReciver);
}
}

你的服务就在这里

public class BackService extends Service {
int i=0;
Intent intent1;
@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);
     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()
{

}

@Override
public void onDestroy() {
    super.onDestroy();
}
protected Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        intent1.putExtra("i", msg.arg1);
        sendBroadcast(intent1);
    }
};
}
于 2012-10-29T09:11:34.483 回答