我遇到的问题是我不知道如何从服务中获取返回值.为什么我想从服务返回值是我想在活动页面中显示此返回值.以下是我的服务文件,返回值是retvalue.
public class SyncService extends Service{
private String forSync,lastrecordfromlocal,userdefined;
DatabaseUtil dbUtil = new DatabaseUtil(this);
@Override
public IBinder onBind(Intent arg0) {
// Todo Auto-generated method stub
return null;
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent,int startId) {
// Todo Auto-generated method stub
super.onStart(intent,startId);
forSync = common.getInfoForSync(this);
String[] getlist = forSync.split(":");
lastrecordfromlocal = getlist[0].toString();
userdefined = getlist[1].toString();
if (common.isNetAvailable(this) == true) {
SyncServiceTask InstallData = new SyncServiceTask(this);
try {
String (((retvalue))) = InstallData.execute(lastrecordfromlocal,userdefined).get();
} catch (InterruptedException e) {
e.printstacktrace();
} catch (ExecutionException e) {
e.printstacktrace();
}
}
this.stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
最佳答案
您可以将服务绑定到Activity:link
您的服务:
public class SyncService extends Service {
private final IBinder mBinder = new mybinder();
private String;
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
forSync = common.getInfoForSync(this);
String[] getlist = forSync.split(":");
lastrecordfromlocal = getlist[0].toString();
userdefined = getlist[1].toString();
if (common.isNetAvailable(this) == true) {
SyncServiceTask InstallData = new SyncServiceTask(this);
try {
String (((retvalue))) = InstallData.execute(lastrecordfromlocal,userdefined).get();
} catch (InterruptedException e) {
e.printstacktrace();
} catch (ExecutionException e) {
e.printstacktrace();
}
}
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class mybinder extends Binder {
SyncService getService() {
return SyncService .this;
}
}
public String getRetValue() {
return retvalue;
}
}
并在Activity中检查值:
SyncService mService;
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this,SyncService .class);
bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
public void onButtonClick(View v) {
if (mBound) {
Toast.makeText(this,"number: " + num,Toast.LENGTH_SHORT).show();
}
}
/** Defines callbacks for service binding,passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServicedisconnected(ComponentName arg0) {
mBound = false;
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。