Android之Service复习深入

本文介绍了如何在Android应用中使用`bindService`方法启动Service,并通过`ServiceConnection`实现Service与Activity之间的连接。重点讨论了`onBind`方法的作用以及如何在Service生命周期中管理连接状态。
摘要由CSDN通过智能技术生成
  • 这个方法是必须执行的,当用bind方法启动Service的时候,是有用的。

*/

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return mBinder;

}

/**

  • Class used for the client Binder. Because we know this service always

  • runs in the same process as its clients, we don’t need to deal with IPC.

*/

public class HelloBinder extends Binder {

HelloService getService() {

return HelloService.this;

}

}

public void showNumber(){

new Thread(new Runnable() {

int i=1;

@Override

public void run() {

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(i++);

}

}

}).start();

}

}

HelloServiceActivity.java

package hb.android.service;

import hb.android.service.HelloService.HelloBinder;

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.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class HelloServiceActivity extends Activity {

Intent intent;

boolean flag = true;

HelloService mService;;

boolean mBound = false;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

intent = new Intent();

initMyWidget();

setMyWidgetListener();

}

public void setMyWidgetListener() {

MyWidget.btn_bind.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_start.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_stop.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_unbind.setOnClickListener(new WidgetOnClickListener());

MyWidget.btn_kill.setOnClickListener(new WidgetOnClickListener());

}

class WidgetOnClickListener implements OnClickListener {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_start:

intent.setClass(getApplicationContext(), HelloService.class);

intent.putExtra(“test”, “start”);

startService(intent);

flag = true;

MyWidget.btn_stop.setEnabled(flag);

break;

case R.id.btn_stop:

intent.putExtra(“test”, “stop”);

stopService(intent);

flag = false;

MyWidget.btn_stop.setEnabled(flag);

break;

case R.id.btn_kill:

android.os.Process.killProcess(android.os.Process.myPid());

break;

case R.id.btn_bind:

Intent intent = new Intent(getApplicationContext(), HelloService.class);

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//自动调用Service里的onCreate方法

System.out.println(“bind:”+mService);

opeService();

break;

case R.id.btn_unbind:

if (mBound) {

unbindService(mConnection);//自动调用Service的onDestory方法

mBound = false;

}

break;

default:

break;

}

}

public void opeService() {

System.out.println(“opeService:”+mService);

// mService.showNumber();

}

}

public void initMyWidget() {

MyWidget.btn_bind = (Button) findViewById(R.id.btn_bind);

MyWidget.btn_start = (Button) findViewById(R.id.btn_start);

MyWidget.btn_stop = (Button) findViewById(R.id.btn_stop);

MyWidget.btn_unbind = (Button) findViewById(R.id.btn_unbind);

MyWidget.tv_test = (TextView) findViewById(R.id.tv_test);

MyWidget.btn_kill = (Button) findViewById(R.id.btn_kill);

}

private static class MyWidget {

static Button btn_start;

static Button btn_stop;

static Button btn_bind;

static Button btn_unbind;

static Button btn_kill;

@SuppressWarnings(“unused”)

static TextView tv_test;

}

@Override

protected void onDestroy() {

super.onDestroy();

stopService(intent);

}

/** Defines callbacks for service binding, passed to bindService() */

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,

IBinder service) {

// We’ve bound to LocalService, cast the IBinder and get LocalService instance

HelloBinder binder = (HelloBinder) service;

mService = binder.getService();

System.out.println(“ServiceConnection:”+mService);

mBound = true;

mService.showNumber();

}

@Override

public void onServiceDisconnected(ComponentName arg0) {

mBound = false;

}

};

@Override

protected void onStop() {

super.onStop();

if (mBound) {

unbindService(mConnection);

mBound = false;

}

};

}

注意:当服务创建之后由,onServiceConnected 这个方法负责建立与Servicer的连接

意思当Activit用BindService启动服务之后,由:

public ServiceConnection mConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

System.out.println(“onServiceConnected”);

mBound = false;

}

/**

  • 服务创建之后收些方法建立Serivce与Activity的连接

*/

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

System.out.println(“onServiceDisconnected”);

mBound = true;

LocalBinder myBinder = (LocalBinder) service;

myService = myBinder.getService(); //在下面就可以对service进行操作了。
myService.showNumber();
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。

后面我就自己整理了一套资料,还别说,真香!

资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。

系列教程图片

2020Android复习资料汇总.png

flutter

NDK

设计思想开源框架

微信小程序

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

,还别说,真香!

资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。

[外链图片转存中…(img-M4PlhtAo-1713788351946)]

[外链图片转存中…(img-IuUqSDtl-1713788351947)]

[外链图片转存中…(img-htzvs3bF-1713788351948)]

[外链图片转存中…(img-sAScFr2P-1713788351949)]

[外链图片转存中…(img-Q92KygOH-1713788351950)]

[外链图片转存中…(img-cZ5aV2Ib-1713788351951)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值