20.1安卓的服务Service开启关闭方法

服务Service从一定的角度来看,可以将其理解为没有界面的Activity,

Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy

其中服务还分为两种使用,

第一种是开启关闭,第二种是绑定和解绑,

开启服务(startService)

服务一旦开启就与调用者没有任何关系, 当调用者Activity退出时,服务仍在运行,不会影响后台service的运行。

在Activity中不能调用服务里面的方法

绑定服务(bindService)

通过绑定方式开启服务,服务跟服务不求同生,但求同死。如果调用者的Activity退出了,那他绑定的服务也会跟着退出。

在Activity中能调用服务里面的方法

第一种,开始就执行oncreate,和onstart,结束时直接destroy,

而且调用者退出的时候这个服务不会退出,下次调用者再次上线时,依然可以关闭这个服务

就是说这个服务可以独立生活

第一种现象是这样

通过现象我们可以看出,运行的三种情况

1.这个服务从来都没有使用过,执行的是创建和启动两步,

2.服务创建过,且没有被销毁,那么点击开服务只能执行 “启动” 一步,

3.当关闭了服务的时候,点击开启又开始了重新的创建和启动

4.没有了服务点击关闭服务是哪个都不执行的

实现思路:新建一个Service,像我们新建Activity一样新建,让他继承Service,

然后去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;

/**
 * Created by xialm on 2019/11/7.
 */
public class MyOwnService extends Service {

    private int i1,i2,i3,i4;

    //只有创建的时候会被调用
    @Override
    public void onCreate() {

        i1++;
        Log.i("日志","我创建了第"+i1+"次");

    }

    //每次运行的时候都会被调用
    @Override
    public void onStart(Intent intent, int startId) {
        i2++;
        Log.i("日志","我运行了第"+i2+"次");

    }

    //每次关闭的时候都会被调用。
    @Override
    public void onDestroy() {
        i3++;
        Log.i("日志","我结束了第"+i3+"次");

    }

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

mainactivity代码

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);
        setContentView(R.layout.activity_main);

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

        b_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("fuwu1");
                //自安卓5.0以来就需要添加获得包的名字,
                //兼容Android 5.0,5.0之后需要设置包名,
                // 这是为了防止造成冲突(有多个Service用同样的intent-filter的情况),这里相对的是不同的包之间
                //注意是开始服务不是开始Activity
                intent.setPackage(getPackageName());
                startService(intent);
            }
        });

        b_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("fuwu1");
                intent.setPackage(getPackageName());
                //Log.i("当前包", 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>

猜你喜欢

转载自www.cnblogs.com/gzyx/p/11811111.html
今日推荐