One of the four components: service

1, Android Service:

The service in Android is a service running in the background. It is invisible and has no interface. You can start a service to play music, record changes in your geographic information location, or start a service to run and listen for certain actions all the time. Service, like other components, runs in the main thread, so it cannot be used for time-consuming requests or actions. You can start one thread in the service and do time-consuming actions in the thread.

Code implementation will be easy to understand

The first kind of IntentService

1. Create a class that inherits the Service

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

public class ExampleService extends Service {

    private static final String TAG = "ExampleService";

    public ExampleService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "ExampleService===>>onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        Log.i(TAG, "ExampleService===>>onStart");

        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.i(TAG, "ExampleService===>>onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy()
    {
        Log.i(TAG, "ExampleService===>>onDestroy");
        super.onDestroy();
    }


}

2. Set start and stop buttons on an Activity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.chenxh.myttestapplication.R;
import com.example.chenxh.myttestapplication.service.ExampleService;

public class ServiceActivity extends AppCompatActivity implements Button.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        findViewById(R.id.startService).setOnClickListener(this);
        findViewById(R.id.stopService).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {
            case R.id.startService:
                Toast.makeText(this, "Open service", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ServiceActivity.this, ExampleService.class);

                startService(intent);

                break;
            case R.id.stopService:
                Toast.makeText(this, "Shut down service", Toast.LENGTH_SHORT).show();
                Intent stopIntent = new Intent(ServiceActivity.this, ExampleService.class);

                stopService(stopIntent);

                break;
        }
    }


}

activity_serivice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.chenxh.myttestapplication.activity.ServiceActivity">

    <Button
        android:id="@+id/startService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open service"
        />
    <Button
        android:id="@+id/stopService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Shut down service"
        />
</LinearLayout>

LogCat log:

//Click start for the first time
ExampleService===>>onCreate
ExampleService===>>onStartCommand
ExampleService===>>onStart
//Click start for the first time
ExampleService===>>onStartCommand
ExampleService===>>onStart
//Click start for the first time
ExampleService===>>onStartCommand
ExampleService===>>onStart
//Click stop service
ExampleService===>>onDestroy

Description onCreate() only goes once onStartCommand() and onstart() will execute again, but onstart() is out of date. When the service is stopped, the ondestore() method will be used.

Tags: Android xml encoding

Posted on Mon, 04 May 2020 22:00:01 -0400 by Joe