我正在android中编写一个本地绑定的服务。我的应用程序有两个类,即。活动和服务
在我的活动中,我有一个 TextView 和一个按钮。我在活动的 onCreate() 方法中启动和绑定服务。单击按钮时,我希望使用从 Service 类中编写的函数返回的 String 值更新我的 TextView
但是我总是将 null 作为返回值。可能是什么问题?
我在这里给出我的整个代码
这是活动课
package com.tcs.cto.healthcare;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.tcs.cto.healthcare.GlucoseDataService.LocalUSBSerialBinder;
public class HealthcareCTOActivity extends Activity {
Button getDataButton;
TextView dataView;
String TAG="HealthCareCTO";
String glucoseData;
GlucoseDataService myService;
boolean isBound = false;
private ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalUSBSerialBinder binder = (LocalUSBSerialBinder) service;
myService = binder.getService();
isBound = true;
}
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_healthcare_cto);
getDataButton=(Button)findViewById(R.id.button1);
dataView=(TextView)findViewById(R.id.glucoseDataView);
Intent intent = new Intent(this, GlucoseDataService.class);
startService(intent);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG,"Glucose Service Started");
//getDataButton=(Button)findViewById(R.id.button1);
//dataView=(TextView)findViewById(R.id.glucoseDataView);
getDataButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Log.i(TAG,"Button clicked");
try{
//glucoseData = myService.getGlucoseValueOverSerialPort();
String currentTime="XXX";
currentTime = myService.getCurrentTime();
//glucoseData = myService.getGlucoseValueOverSerialPort();
//Log.i(TAG,"glucoseData --> "+glucoseData);
Log.i(TAG,"currentTume --> "+currentTime);
dataView.setText(currentTime);
}catch(Exception ex){
Log.i(TAG,"Button click exception");
ex.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_healthcare_cto, menu);
return true;
}
}
我的服务类如下
package com.tcs.cto.healthcare;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class GlucoseDataService extends Service {
private final IBinder myBinder = new LocalUSBSerialBinder();
String glucoseData="0000";
String TAG="GlucoseService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public String getGlucoseValueOverSerialPort(){
int glucoseValue=120;
glucoseData=String.valueOf(glucoseValue);
return glucoseData;
}
public String getCurrentTime() {
Log.i(TAG,"getCurrentTime");
SimpleDateFormat dateformat =
new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
return (dateformat.format(new Date()));
}
public class LocalUSBSerialBinder extends Binder {
GlucoseDataService getService() {
return GlucoseDataService.this;
}
}; }