在Activity中调用Service的非静态方法

先上代码:

public class MyService extends Service {
    public MyService() {
    }

    private long mServiceCreatTime;


    @Override
    public void onCreate() {
        super.onCreate();
        mServiceCreatTime = System.currentTimeMillis();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public class MyBinder extends Binder{
        public String getRunningTime(){
            return MyService.this.getRunningTime();
        }
    }

    public String getRunningTime(){
        long runningTime = System.currentTimeMillis()-mServiceCreatTime;
        return String.format("服务已运行: %s",dateFormat(runningTime));
    }

    public String dateFormat(long timeMillions){
        Date date = new Date(timeMillions);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

}
public class MainActivity extends AppCompatActivity {

    private TextView mTvRunningTime;
    private MyService.MyBinder mMyBinder;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTvRunningTime = (TextView) findViewById(R.id.tv_running_time);

        //绑定服务
        Intent intent = new Intent(this,MyService.class);
        MyServiceConnection myServiceConnection = new MyServiceConnection();
        bindService(intent,myServiceConnection, Context.BIND_AUTO_CREATE);

        refreshRunningTime();
    }

    private void refreshRunningTime(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //通过MyBinder调用MyService中的getRunningTime()方法
                        if (mMyBinder!=null) {
                            mTvRunningTime.setText(mMyBinder.getRunningTime());
                        }
                    }
                });
            }
        },0,1000);
    }

    private class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mMyBinder = (MyService.MyBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }

}

实现步骤:
1.在Service中创建一个Binder的子类MyBinder, 在MyBinder中调用MyService需要提供给外部调用的getRuuningTime()方法;
2.在MyService的onBind()方法中返回MyBinder的实例;
3.在Activity中创建一个ServiceConnection的实现类MyServiceConnection;
4.通过bindService()启动MyService;
5.在MyServiceConnection的onServiceConnected()方法中将iBinder强转为MyBinder, 这样就可以通过MyBinder调用MyService中的getRunningTime()方法了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值