我怎样才能从Android服务的返回值 [英] how can I get return value from android services

查看:130
本文介绍了我怎样才能从Android服务的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,我不知道我怎么可以获取来自服务的返回值的问题。为什么我要返回从服务的价值是我想表现出活动页面这个返回值。以下是我services文件,并返回值 retvalue

 公共类SyncService延伸服务{
    私人字符串forSync,lastrecordfromlocal,用户定义;
    DatabaseUtil dbUtil =新DatabaseUtil(本);    @覆盖
    公众的IBinder onBind(意向为arg0){
        // TODO自动生成方法存根
        返回null;
    }    @燮pressWarnings(德precation)
    @覆盖
    公共无效调用onStart(意向意图,诠释startId){
        // TODO自动生成方法存根
        super.onStart(意向,startId);        forSync = common.getInfoForSync(本);
        的String [] =的GetList forSync.split(:);
        lastrecordfromlocal =的GetList [0]的ToString();
        用户自定义=的GetList [1]的ToString();        如果(common.isNetAvailable(本)==真){
            SyncServiceTask InstallData =新SyncServiceTask(本);
            尝试{
                字符串(((retvalue)))= InstallData.execute(lastrecordfromlocal,用户自定义)获得();
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }赶上(为ExecutionException E){
                e.printStackTrace();
            }
        }
        this.stopSelf();
    }    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
    }
}


解决方案

您可以绑定服务的活动:的链接

您服务:

 公共类SyncService延伸服务{
  私人最终的IBinder mBinder =新MyBinder();
  私人字符串;  @覆盖
  公众诠释onStartCommand(意向意图,诠释标志诠释startId){     forSync = common.getInfoForSync(本);
    的String [] =的GetList forSync.split(:);
    lastrecordfromlocal =的GetList [0]的ToString();
    用户自定义=的GetList [1]的ToString();    如果(common.isNetAvailable(本)==真){
        SyncServiceTask InstallData =新SyncServiceTask(本);
        尝试{
            字符串(((retvalue)))= InstallData.execute(lastrecordfromlocal,用户自定义)获得();
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }赶上(为ExecutionException E){
            e.printStackTrace();
        }
    }
    返回Service.START_NOT_STICKY;
  }  @覆盖
  公众的IBinder onBind(意向为arg0){
    返回mBinder;
  }  公共类MyBinder扩展粘结剂{
    SyncService的getService(){
      返回SyncService。本;
    }
  }  公共字符串getRetValue(){
    返回retvalue;
  }}

而在活动检查值:

  SyncService MSERVICE;
    @覆盖
        保护无效调用onStart(){
            super.onStart();
            //绑定到本地服务
            意向意图=新意图(这一点,SyncService的.class);
            bindService(意向,mConnection,Context.BIND_AUTO_CREATE);
        }        @覆盖
        保护无效的onStop(){
            super.onStop();
            //取消绑定从服务
            如果(mBound){
                unbindService(mConnection);
                mBound = FALSE;
            }
        }        / **一个按钮被点击时(在布局文件中的按钮连接到被叫
          *这个方法用android:onclick属性)* /
        公共无效onButtonClick(视图v){
            如果(mBound){                Toast.makeText(这一点,数字+ NUM,Toast.LENGTH_SHORT).show();
            }
        }    / **定义服务绑定,传递给bindService()回调* /
        私人ServiceConnection mConnection =新ServiceConnection(){            @覆盖
            公共无效onServiceConnected(组件名类名,
                    的IBinder服务){
                LocalBinder粘结剂=(LocalBinder)服务;
                MSERVICE = binder.getService();
                mBound = TRUE;
            }            @覆盖
            公共无效onServiceDisconnected(组件名称为arg0){
                mBound = FALSE;
            }
        };

I got the problem that I don't know how can I fetch return value from services. Why I want return value from services is I want to show this return value in Activity page. Following is my services file and return value is 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();
    }    
}

解决方案

You can bind service to the Activity:link

Your Service:

  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;
  }

}

And in Activity check the value:

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服务的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆