Android モバイル アプリ開発 - ライトのオンとオフ (小さなウサギ) - 実験 8 - サービスの開始と停止

 レイアウトと基本的なコントロールの属性関数と使用方法をマスターする
 サービスを開始および停止する startService() メソッドと stopService() メソッドを

マスターする 線形レイアウトと相対レイアウトを使用してインターフェースを構築する インターフェースの効果を下図に示します。「消灯」ボタンをクリックすると、2 番目の状態に変わります。2 番目の状態では、[ライトをオンにする] ボタンをクリックすると、最初のインターフェイス状態に戻ります。startService() メソッドと stopService() メソッドを使用して、同様の機能を持つ実験的なケースを個別に設計することをお勧めします。

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#efede0">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        >
        <ImageButton
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/btn_open"
            android:id="@+id/ib"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="关灯"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:id="@+id/tv"/>
    </RelativeLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:background="@drawable/img_open"
        android:layout_marginTop="100dp"
        android:id="@+id/iv"/>

</LinearLayout>

 

MainActivity.java 

package com.example.shiyan8;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ImageButton imageButton;
    private ImageView imageView;
    private TextView textView;
    private boolean isOpen=true;

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

    private void init() {
        imageButton = findViewById(R.id.ib);
        imageView = findViewById(R.id.iv);
        textView = findViewById(R.id.tv);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isOpen) {
                    imageButton.setBackgroundResource(R.drawable.btn_close);
                    imageView.setBackgroundResource(R.drawable.img_close);
                    textView.setText("开灯");
                    isOpen=false;
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    stopService(intent);
                }else{
                    imageButton.setBackgroundResource(R.drawable.btn_open);
                    imageView.setBackgroundResource(R.drawable.img_open);
                    textView.setText("关灯");
                    isOpen=true;
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    startService(intent);
                }
            }
        });
    }
}

MyService.java

package com.example.shiyan8;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

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

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("service","服务已开启。");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("service","服务已关闭。");
    }
}

おすすめ

転載: blog.csdn.net/qq_64628470/article/details/130457078