Android四大组件之一(服务的用法10) 00-50-56-C0-00-01(2015)

前言

了解了Android多线程编程的技术之后,下面就让我们进入到本次的正题,开始对服务的相关内容进行学习。作为Android四大组件之一,服务也少不了有很多非常重要的知识点,那我们自然要从最基本的用法开始学习了。

一、 服务的基本用法

了解了Android多线程编程的技术之后,下面就让我们进入到本章的正题,开始对服务的相关内容进行学习。作为Android四大组件之一,服务也少不了有很多非常重要的知识点,那我们自然要从最基本的用法开始学习了。
首先看一下如何在项目中定义一个服务。新建一个ServiceTest项目,然后右击com.example.servicetest →New→Service →Service,会弹出如图所示:
在这里插入图片描述
可以看到,这里我们将服务命名为 MyService,Exported 属性表示是否允许除了当前程序之外的其他程序访问这个服务,Enabled属性表示是否启用这个服务。将两个属性都勾中,点击Finish完成创建。
在这里插入图片描述

二、程序部分

1.activity.main.xml

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    tools:context=".MainActivity">
<Button
    android:id="@+id/start_service"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="start Service"/>

    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stop Service" />
    <Button
        android:id="@+id/bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bind Service"/>
    <Button
        android:id="@+id/unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="unbind Serivice"/>

</LinearLayout>

2.MainActivity.Java

代码如下(示例):

package com.example.androidthreadtest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.security.Provider;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
    private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            downloadBinder=(MyService.DownloadBinder) service;
            downloadBinder.startDownload();
            downloadBinder.getprogress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bindService=(Button) findViewById(R.id.bind_service);
        Button unbindService=(Button) findViewById(R.id.unbind_service);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
    }

        @Override
        public void onClick(View v){
        switch (v.getId()){
            case R.id.bind_service:
                Intent startIntent=new Intent(this,MyService.class);
                Intent bindInten = null;
                bindService(bindInten,connection,BIND_AUTO_CREATE);//绑定服务
                break;
            case R.id.unbind_service:
                unbindService(connection);//解绑服务
            break;
            default:
                break;
        }

        }

    }

3.MyService.Java

代码如下(示例):

package com.example.androidthreadtest;

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

public abstract class MyService extends Service {
    private DownloadBinder mBinder = new DownloadBinder();

    class DownloadBinder extends Binder {

        public void startDownload() {
            Log.d("MyService", "startDownload executed");
        }

        public int getprogress() {
            Log.d("MyService", "getprogress executed");
            return 0;
        }
    }

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

4.AndroidManifest.xml

代码如下(示例):

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

    <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/Theme.AndroidThreadTest">
        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"></service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.运行程序

部分运行结果如图所示:
在这里插入图片描述

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了服务的基本用法的简单介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值