andorid service 本地服务

ActivityManifect.xml

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

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service
            android:name=".TestService1"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.hanqi.testservice.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="一般启动服务"
            android:onClick="bt1_onclick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="一般停止服务"
            android:onClick="bt2_onclick"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="绑定启动服务"
            android:onClick="bt3_onclick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="解绑停止服务"
            android:onClick="bt4_onclick"/>
    </LinearLayout>

</LinearLayout>

TestService.java

package com.hanqi.testservice;

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

public class TestService1 extends Service {
    public TestService1() {

        Log.e("TAG","构造Service");
    }
    //绑定的回调方法
    //必须要重写
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        Log.e("TAG","绑定被调用");
        return new Binder();
    }
    @Override
    public void onCreate() {
        Log.e("TAG","创建Service");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.e("TAG","销毁Service");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TAG","启动命令");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG","解除绑定");
        return super.onUnbind(intent);
    }
}

mainactivity.java

package com.hanqi.testservice;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //一般方式启动Service
    //参考Activity的启动方式
    public void bt1_onclick(View v)
    {
        //通过意图Intent启动
        //显示意图
        Intent intent = new Intent(this,TestService1.class);
        startService(intent);
        Toast.makeText(MainActivity.this, "启动Service", Toast.LENGTH_SHORT).show();

    }
    //一般方式停止Service
    //参考Activity的启动方式
    public void bt2_onclick(View v)
    {
        //通过意图Intent启动
        //显示意图
        Intent intent = new Intent(this,TestService1.class);
        stopService(intent);
        Toast.makeText(MainActivity.this, "停止Service", Toast.LENGTH_SHORT).show();

    }

    ServiceConnection serviceConnection;
    public void bt3_onclick(View v)
    {

        //通过意图Intent启动
        //显示意图
        Intent intent = new Intent(this,TestService1.class);

        if(serviceConnection == null) {
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.e("TAG", "连接服务");

                }

                @Override
                public void onServiceDisconnected(ComponentName name) {

                    Log.e("TAG", "断开链接服务");
                }
            };
        }
        //参数: 意图,连接,绑定方式   BIND_AUTO_CREATE(自动创建)
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

        Toast.makeText(MainActivity.this, "绑定服务成功", Toast.LENGTH_SHORT).show();
    }

    public void bt4_onclick(View v)
    {
        //判断是否已经绑定,连接是否为空
        if(serviceConnection != null)
        {
            //解除绑定
            unbindService(serviceConnection);

            serviceConnection = null;

            Toast.makeText(MainActivity.this, "解除绑定", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "尚未绑定", Toast.LENGTH_SHORT).show();
        }
    }

    //  Acticity销毁的回调方法
    @Override
    protected void onDestroy() {

        if(serviceConnection != null) {
            //解除绑定
            unbindService(serviceConnection);

            serviceConnection = null;
        }
        super.onDestroy();
    }
}
时间: 2024-12-11 04:47:00

andorid service 本地服务的相关文章

Android Service学习之本地服务

Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindserverice来启动. Service和其他的应用组件一样,运行在进程的主线程中.这就是说如果service需要很多耗时或者阻塞的操作,需要在其子线程中实现. service的两种模式(startService()/bindService()不是完全分离的): 本地服务 Local Service

Android Service 后台服务之本地服务

Service是Android系统的服务组件,适用于开发没有用户界面且长时间在后台运行的功能 - Service简介 因为手机硬件性能和屏幕尺寸的限制,通常Android系统仅允许一个应用程序处于激活状态并显示在手机屏幕上,而暂停其他处于未激活状态的程序. 因此,Android系统需要一种后台服务机制,允许在没有用户界面的情况下,使程序能够长时间在后台运行,实现应用程序的后台服务功能. 在实际应用中,有很多应用需要使用Service,比如MP3播放器要求在关闭播放器界面后,仍然能够后台保持音乐持

Android Binder机制(3) 本地服务注册过程

本博客将讲解本地服务的注册过程,为了方便大家更好地理解,选择了MediaPlayer Service作为例子. 启动并注册MediaPlayer Service的代码在frameworks/base/media/mediaserver/main_mediaserver.cpp中,如下: main_mediaserver.cpp 1 2 3 4 5 6 7 8 9 10 11 12 int main(int argc, char** argv) { sp<ProcessState>proc(Pr

android基础部分再学习---再谈Service进程服务通信

Bound Services 快速查看 bound服务允许被其它控件绑定,以便与之交互并进行进程间通信 一旦所有的客户端都解除了绑定,bound服务将被销毁.除非该服务同时又是started类型的. 在本文中(参见目录) 关键类 Service ServiceConnection IBinder 范例 RemoteService LocalService bound服务是客户端-服务器模式的服务.bound服务允许组件(比如activity)对其进行绑定.发送请求.接收响应.甚至进行进程间通信(

绑定本地服务调用方法的步骤

服务的生命周期: 一.采用start的方式开启服务 生命周期如下: onStart()过时了开启服务: onCreate()--> onStartCommand() ---> onDestory(); 如果服务已经开启,不会重复的执行onCreate(), 而是会调用onStart()和 onStartCommand(); 服务停止的时候 onDestory(). 服务只会被停止一次 Service若通过new来创建,则只能为一普通类,不是服务,服务创建必须通过框架实现,即调用服务的方法,必须

Android专题8——本地服务和远程服务通信

关于远程服务 远程服务指的是服务和访问者不在同一个应用程序中,即不在同一个进程中. 访问远程服务类似进程间通信. 访问者和远程服务需要遵守能够被操作系统理解的协议,AIDL. 1. 在服务端和客户端配置AIDL aidl文件最好都放在aidl目录下,aidl文件用于接口描述 会自动生成一个同名的JAVA文件 在自动生成的JAVA文件中,系统会自动定义一个Stub类,继承Binder类,实现aidl文件中描述的接口 2. 编写service业务代码 在清单文件中注册 实现自定义的IBinder <

APP本地服务安全测试

一.安全测试基本分类: 1.系统安全 系统加固 安全加固:比如linux中关闭telnet端口,修改ssh端口 检测一些不必要的服务(需要卸载一个ping)--保证系统的最小集 app安全加固:加一层外壳 补丁 消息中间件:activityMQ,rabbitMQ,safMQ(关闭页面,非业务端口,默认用户) 防火墙规则(iptables) 防病毒 2.应用安全(安装包,服务,业务)----用户(人和服务)口令.敏感信息 (1)黑白名单(IP:port)----访问控制 (2)消息层面:数据加密和

关于tomcat本地服务无法启动

问题:运行tomcat的startup.bat,tomcat可以正常启动,但localhost不能访问,于是发现tomcat本地服务没有启动. 在启动tomcat本地服务时,弹出错误:windows不能在本地计算机启动Apache Tomcat.有关更多信息,查阅系统事件日志.如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码0. 解决方法:1)查看日志:tomcat/logs/commons-daemon.2018-11-01.log. 错误:Failed creati

Android四大组件——Service后台服务、前台服务、IntentService、跨进程服务、无障碍服务、系统服务

Service后台服务.前台服务.IntentService.跨进程服务.无障碍服务.系统服务 本篇文章包括以下内容: 前言 Service的简介 后台服务 不可交互的后台服务 可交互的后台服务 混合性交互的后台服务 前台服务 IntentService AIDL跨进程服务 AccessibilityService无障碍服务 系统服务 部分源码下载 前言 作为四大组件之一的Service类,是面试和笔试的必备关卡,我把我所学到的东西总结了一遍,相信你看了之后你会对Service娓娓道来,在以后遇