4.19

所花时间(包括上课):2

打码量(行):200

博客量(篇):1

了解到知识点:学习startSerview与生命周期

 

 import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

import androidx.annotation.Nullable;

 

public class MyService extends Service {

 

    private static final String TAG = "MyService";

 

    @Override

    public void onCreate() {

        super.onCreate();

        // Service 被创建时调用

        Log.d(TAG, "Service onCreate");

    }

 

    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

        // 每次通过 startService 启动 Service 时调用

        Log.d(TAG, "Service onStartCommand");

 

        // 在后台线程中执行长时间操作

        new Thread(new Runnable() {

            @Override

            public void run() {

                performLongRunningTask();

            }

        }).start();

 

        // 如果系统因内存不足杀掉 Service,重启后不会重新创建 Service

        return START_NOT_STICKY;

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        // Service 被销毁时调用

        Log.d(TAG, "Service onDestroy");

    }

 

    @Nullable

    @Override

    public IBinder onBind(Intent intent) {

        // 不提供绑定服务,返回 null

        return null;

    }

 

    private void performLongRunningTask() {

        // 模拟长时间操作

        try {

            Thread.sleep(5000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        Log.d(TAG, "长时间操作完成");

    }

}

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取按钮视图

        Button startServiceButton = findViewById(R.id.start_service_button);

        Button stopServiceButton = findViewById(R.id.stop_service_button);

 

        // 设置启动服务按钮点击监听器

        startServiceButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 启动服务

                Intent intent = new Intent(MainActivity.this, MyService.class);

                startService(intent);

            }

        });

 

        // 设置停止服务按钮点击监听器

        stopServiceButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 停止服务

                Intent intent = new Intent(MainActivity.this, MyService.class);

                stopService(intent);

            }

        });

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">

 

    <!-- 启动服务按钮 -->

    <Button

        android:id="@+id/start_service_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="启动服务"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="50dp" />

 

    <!-- 停止服务按钮 -->

    <Button

        android:id="@+id/stop_service_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="停止服务"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/start_service_button"

        android:layout_marginTop="20dp" />

 

</RelativeLayout>

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

    package="com.example.myserviceapp">

 

    <application

        android:allowBackup="true"

        android:label="@string/app_name"

        android:icon="@mipmap/ic_launcher"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        

        <!-- 声明 MyService -->

        <service android:name=".MyService" />

        

    </application>

</manifest>

posted @ 2024-04-19 17:39  赵千万  阅读(2)  评论(0编辑  收藏  举报