Andrews 20.1 service Service open close method

Services Service from a certain point of view, it can be understood that there is no interface Activity,

Service life-cycle approach is less than some of Activity, only onCreate, onStart, onDestroy

Which is also divided into two services use,

The first is to open and close, and the second is the binding tie of reconciliation,

 

Open service (startService)

Once the service is not open to any relationship with the caller, when caller Activity quit, service is still running, without affecting the operation of the background service.

You can not call the service inside the method in Activity

 

Binding Services (bindService)

By binding way open service, the service with the service not to do with the students, but for the same die. If the caller's Activity quit, he bound service will follow exit.

Activity in the service can call inside the method

 

 

The first, beginning on the implementation of oncreate, and onstart, direct destroy at the end,

And the caller exits when the service does not exit, the next time the caller on the line again, still can turn off this service

This means that the service can live independently

The first phenomenon is the case

 

 

By phenomenon we can see, running three cases

1. This service has never been used before, perform is to create and launch a two-step,

2. Create a service before, and not destroyed, then click to open the service can only be performed "start" step,

3. When closed the service, click the Open began a re-creation and start

4. Click to close the service no service which is not performed

 

Realization of ideas: Create a new Service, as our new Activity as new, so he inherited Service,

Then he went to register it in mainfest.

Package com.example.xialm.service_21tolog; 

Import android.app.Service;
 Import android.content.Intent;
 Import android.os.IBinder;
 Import android.support.annotation.Nullable;
 Import android.util.Log; 

/ ** 
 * . the created by xialm ON 2019/11/7 
 * / 
public  class MyOwnService the extends Service { 

    Private  int I1, i2, i3, I4; 

    // time only create will be called 
    @Override
     public  void the onCreate () { 

        I1 ++ ; 
        the Log .i ( "log", "I created the first" + i1 + "times" );

    } 

    // every time you run will be called 
    @Override
     public  void onStart (the Intent the Intent, int startID) { 
        i2 ++ ; 
        Log.i ( "log", "I ran the first" + i2 + "times" ); 

    } 

    // every time off will be called. 
    @Override
     public  void onDestroy () { 
        i3 ++ ; 
        Log.i ( "log", "I ended up on" + i3 + "times" ); 

    } 

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

 

mainactivity Code

package com.example.xialm.service_21tolog;

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

public class MainActivity extends AppCompatActivity {

    private Button b_open;
    private Button b_close;

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

        B_open = (the Button) the findViewById (R.id.b_1); 
        b_close = (the Button) the findViewById (R.id.b_2); 

        b_open.setOnClickListener ( new new View.OnClickListener () { 
            @Override 
            public  void onClick (View v) { 
                the Intent the Intent = new new the Intent ( "fuwu1" );
                 // since Android 5.0 will need to add its name to get the package,
                 // compatible with the need to set the package name after 5.0, 5.0 Android,
                 // this is in order to prevent conflicts (where a plurality of Service with the same intent-filter), where is the opposite between different packages
                 //Note that the service is not started start the Activity 
                intent.setPackage (getPackageName ()); 
                the startService (Intent); 
            } 
        }); 

        b_close.setOnClickListener ( new new View.OnClickListener () { 
            @Override 
            public  void the onClick (View V) { 
                the Intent Intent = new new the Intent ( "fuwu1" ); 
                intent.setPackage (getPackageName ()); 
                // Log.i ( "current package", getPackageName ()); 
                stopService (Intent); 
            } 
        }); 

    } 
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.xialm.service_21tolog.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/b_1"
        android:layout_below="@+id/tv1"
        android:text="开服务"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/b_2"
        android:layout_below="@+id/b_1"
        android:text="关服务"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

 

 

Guess you like

Origin www.cnblogs.com/gzyx/p/11811111.html