Android 面试题之Service干货篇,android热修复技术

本文详细介绍了Android中的Service组件,重点讨论了bindService的生命周期管理,包括onCreate、onBind、onUnbind和onDestroy方法,以及与Thread的区别。同时展示了如何在Activity中操作Service,并提到了IntentService的特性。
摘要由CSDN通过智能技术生成

public IBinder onBind(Intent intent) {

return null;

}

}

  • 2.创建ServiceActivity.java和配套的activity_service.xml文件,其代码如下:

public class ServiceActivity extends ActivityBase {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_service);

Intent intentStart = new Intent(ServiceActivity.this, StartService.class);

findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startService(intentStart);

}

});

findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

stopService(intentStart);

}

});

}

}

  • 配套的activity_service.xml文件
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/ll_bg”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

android:background=“@color/color_666666”>

<Button

android:id=“@+id/btn_start”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“start启动服务”/>

<Button

android:id=“@+id/btn_stop”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“start停止服务”/>

  • 3.添加Service组件声明,在AndroidManifest.xml文件中声明一个Service组件,其代码如下:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“com.scc.demo”>

<application

…>

<activity

…>

  • 运行结果

07-07 16:41:11.474 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonCreate

07-07 16:41:11.481 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonStart

07-07 16:41:11.482 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonResume

07-07 16:41:13.313 E/-SCC-com.scc.demo.service.StartService: onCreate

07-07 16:41:13.334 E/-SCC-com.scc.demo.service.StartService: onStartCommand

07-07 16:41:16.705 E/-SCC-com.scc.demo.service.StartService: onDestroy

bindSe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值