如何从android服务获取返回值

前端之家收集整理的这篇文章主要介绍了如何从android服务获取返回值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我遇到的问题是我不知道如何从服务中获取返回值.为什么我想从服务返回值是我想在活动页面显示此返回值.以下是我的服务文件,返回值是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;
            }
        };

猜你在找的Android相关文章