Android service服务

主类:

package com.example.service;

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

public class MainActivity extends AppCompatActivity {

    private Button button1, button2;
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyService.MyBinder myBinder = (MyService.MyBinder) service;
            String s = myBinder.fangfa();
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);

        final Intent intent = new Intent(MainActivity.this, MyService.class);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动服务
//                startService(intent);
                //传信息
                //第一个参数intent对象  第二个参数:ServiceConnection接口对象  第三个参数:是否主动创建服务的标记:BIND_AUTO_CREATE
                bindService(intent, connection, BIND_AUTO_CREATE);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                stopService(intent);
                unbindService(connection);
            }
        });
    }
}

MyService:

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.util.Log;

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

    //创建
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("hh", "onCreate");
    }

    //需求
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("hh", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    //停止服务
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("hh", "onDestroy");
    }

    //返回值
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    //服务器返回信息
    static class MyBinder extends Binder {
        public String fangfa() {
            //这里可以进行任何操作,然后再返回值
            return "sfd";
        }
    }

}

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:text="启动服务"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button2"
        android:text="停止服务"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.service">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">

        </service>
    </application>

</manifest>

猜你喜欢

转载自blog.csdn.net/LIXIAONA_1101/article/details/81257968
今日推荐