Android开发-后端Model_Service

本文介绍了Android中后台Service的实现,包括创建服务、交互内容和调用服务的详细步骤。通过示例展示了如何使用startService()、stopService()、bindService()和unbindService()来管理和通信。此外,还提及了IntentService作为多线程服务的使用。
摘要由CSDN通过智能技术生成

Android开发-后端Model_Service

sf2gis@163.com

2015年12月26日

1 目标:后台服务功能,不具备交互展示功能。

2 原理:Activity删除交互功能。

3 流程:创建服务,创建交互内容,调用服务。

3.1 创建服务:app->java右键-》new-》service-》Service。

3.2 创建服务后会自动重写onBind()绑定响应函数,这里需要重写onCreate()服务创建响应函数和onStartCommand()服务启动响应函数。

//ServiceWithoutIntent.java

package lee.com.demo;

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

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

    @Override
    public void onCreate() {
        super.onCreate();
       Log.d("ServiceWithoutIntent", "created.");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, intstartId) {
       Log.d("ServiceWithoutIntent", "onStartCommand.");

        String info="intent="+intent.getStringExtra("xx")+",flag="+flags+",startId="+startId;
        Log.d("ServiceWithoutIntent",info);
        returnsuper.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communicationchannel to the service.
       Log.d("ServiceWithoutIntent","Binded..");
        return null;
    }
}

3.3 创建交互内容:intent

在页面中添加一个Button,设置响应函数为btnClick(),在其中设置intent和启动服务。

        Intent i=newIntent(MainActivity.this,ServiceWithoutIntent.class);

       i.putExtra("xx","xx123");

3.4 调用服务:startService()

startService(i);

3.5 示例

//结果:点击按钮后在logcat中出现以下信息

01-16 17:59:29.32520378-20378/lee.com.demo D/ServiceWithoutIntent: onStartCommand.

01-16 17:59:29.32520378-20378/lee.com.demo D/ServiceWithoutIntent: intent=xx123,flag=0,startId=1

//ServiceWithoutIntent.java

package lee.com.demo;

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

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

    @Override
    public void onCreate() {
        super.onCreate();
       Log.d("ServiceWithoutIntent", "created.");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, intstartId) {
       Log.d("ServiceWithoutIntent", "onStartCommand.");

        String info="intent="+intent.getStringExtra("xx")+",flag="+flags+",startId="+startId;
       Log.d("ServiceWithoutIntent",info);
        returnsuper.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communicationchannel to the service.
       Log.d("ServiceWithoutIntent","Binded..");
        return null;
    }
}

//AndroidManifest.xml

<?xmlversion="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
    package="lee.com.demo" >

    <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
           android:label="@string/app_name"
           android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <actionandroid:name="android.intent.action.MAIN" />

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

        <service
           android:name=".ServiceWithoutIntent"
           android

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弗里曼的小伙伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值