您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
63_通过startservice开启服务的生命周期
发布时间:2021-03-07 10:27:44编辑:雪饮阅读()
一个简单的项目,就做一个服务的开启与关闭,服务什么事情都不干。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启服务" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.servicelifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bt_start;
Button bt_stop;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_start = this.findViewById(R.id.button);
bt_stop = this.findViewById(R.id.button2);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
intent = new Intent(this,MyService.class);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button: // 开启服务
startService(intent);
break;
case R.id.button2: //停止服务
stopService(intent);
break;
}
}
}MyService.java:
package com.example.servicelifecycle;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {return null;}
@Override
public void onCreate() {
System.out.println("oncreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onstart");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
System.out.println("ondestroy");
super.onDestroy();
}
}AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicelifecycle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Servicelifecycle">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"></service>
</application>
</manifest>然后部署到设备中经过测试可以发现,当服务开启的时候会执行onCreate、onStart,然后若服务已经开启,则再次点击开启服务就会只执行onStart。除非先把服务关闭,然后再次开启才会又执行onCreate、onStart。而当执行服务停止的时候就会触发onDestroy。另外就是当应用程序退出(返回键退出)到桌面时候会发现没有触发到onDestroy方法,说明单纯的返回键退出应用程序并不会影响到服务的状态。这就是startService开启服务的生命周期。
关键字词:startservice,android,服务