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.example.chenshuai.myapplication.ActivityService"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试普通服务"
android:textSize="30sp"/> <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="yibanqdonclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="一般停止服务"
android:onClick="yibantzonclick" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试绑定服务"
android:textSize="30sp"/>
<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="bangdingqdonclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="绑定停止服务"
android:onClick="bangdingtzonclick" />
</LinearLayout> </LinearLayout>

MyService_1.java

package com.example.chenshuai.myapplication;

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 ActivityService extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_service);
} //普通方式启动Service
//参考Activity的启动方式
public void yibanqdonclick(View view)
{
//通过意图
//显示意图
Intent intent = new Intent(this,MyService_1.class);
startService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show();
}
public void yibantzonclick(View view)
{
//通过意图
//显示意图
Intent intent = new Intent(this,MyService_1.class);
stopService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show();
}
ServiceConnection serviceConnection;
public void bangdingqdonclick(View view)
{
Intent intent = new Intent(this,MyService_1.class); //实现ServiceConnection接口
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(ActivityService.this, "连接服务成功", Toast.LENGTH_SHORT).show();
} public void bangdingtzonclick(View view)
{
//判断是否已经绑定,连接是否为空
if (serviceConnection != null)
{
//解除绑定
unbindService(serviceConnection); serviceConnection = null; Toast.makeText(ActivityService.this, "解除绑定", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(ActivityService.this, "尚未绑定", Toast.LENGTH_SHORT).show();
}
} //重写回调方法,防止强制退出时,绑定服务还在运行
@Override
protected void onDestroy() { if (serviceConnection != null) {
//解除绑定
unbindService(serviceConnection); serviceConnection = null;
}
super.onDestroy();
}
}
ActivityService.java
package com.example.chenshuai.myapplication;

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 ActivityService extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_service);
} //普通方式启动Service
//参考Activity的启动方式
public void yibanqdonclick(View view)
{
//通过意图
//显示意图
Intent intent = new Intent(this,MyService_1.class);
startService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show();
}
public void yibantzonclick(View view)
{
//通过意图
//显示意图
Intent intent = new Intent(this,MyService_1.class);
stopService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show();
}
ServiceConnection serviceConnection;
public void bangdingqdonclick(View view)
{
Intent intent = new Intent(this,MyService_1.class); //实现ServiceConnection接口
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(ActivityService.this, "连接服务成功", Toast.LENGTH_SHORT).show();
} public void bangdingtzonclick(View view)
{
//判断是否已经绑定,连接是否为空
if (serviceConnection != null)
{
//解除绑定
unbindService(serviceConnection); serviceConnection = null; Toast.makeText(ActivityService.this, "解除绑定", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(ActivityService.this, "尚未绑定", Toast.LENGTH_SHORT).show();
}
} //重写回调方法,防止强制退出时,绑定服务还在运行
@Override
protected void onDestroy() { if (serviceConnection != null) {
//解除绑定
unbindService(serviceConnection); serviceConnection = null;
}
super.onDestroy();
}
}

minifest.xml

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

Android——Service的更多相关文章

  1. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  2. 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

    Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...

  3. Android Service完全解析,关于服务你所需知道的一切(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  4. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  5. android service 的各种用法(IPC、AIDL)

    http://my.oschina.net/mopidick/blog/132325 最近在学android service,感觉终于把service的各种使用场景和用到的技术整理得比较明白了,受益颇 ...

  6. Android service介绍和启动方式

    1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...

  7. Android Service初始

    一.Service概念 1.Service是一个应用程序组件 2.Service没有图像化界面 3.Service通常用来处理一些耗时比较长的操作 4.可以使用Service更新ContentProv ...

  8. Android Service与Thread的区别

    Android Service,后台,Android的后台就是指,它的运行是完全不依赖UI的.即使Activity被销毁,或者程序被关闭,只要进程还在,Service就可以继续运行.比如说一些应用程序 ...

  9. Android service binder aidl 关系

    /********************************************************************************** * Android servic ...

  10. Android Service AIDL 远程调用服务 【简单音乐播放实例】

    Android Service是分为两种: 本地服务(Local Service): 同一个apk内被调用 远程服务(Remote Service):被另一个apk调用 远程服务需要借助AIDL来完成 ...

随机推荐

  1. linux 命令中的find locate whereis which type 使用区别

    find 最强大,但参数也较多,需指定查找目录,如 find / -name “filename” locate 是一个快速查找命令,有预先索引好的数据库,由于数据库是定时更新,因此,结果上可能会有迟 ...

  2. 一个Demo带你彻底掌握View的滑动冲突

    本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 近期在又一次学习Android自己定义View这一块的内容.遇到了平时开发中常常碰到的一个棘手问题:View的滑 ...

  3. 转:ECharts图表组件入门教程之Theme:ECharts图表的皮肤是什么?如何给图表换主题(皮肤)Theme?

    一.什么是ECharts图表的皮肤(主题)? 针对这个问题我只能这样回答,ECharts图表的主题(皮肤)就犹如人的衣服一样,是用来衬托和渲染主体,使其变得更加美观好看的目的.你去过ECharts图表 ...

  4. c# xml 输出注释格式控制

    string str="\n\t///*! 妆容类物品子分类 */\n\tenum MakeupSubType\n\t{\n\t\tItem_InvalidMakeupSubType = - ...

  5. Guid ToString 格式

    转自 http://www.cnblogs.com/greenerycn/archive/2010/04/25/guid_tostring_format.html 在日常编程中,Guid是比较常用的, ...

  6. CentOS编译安装vim

    #删除系统自带的vim rpm -qa | grep vim rpm -e vim-X11-7.4.629-5.el6_8.1.x86_64 vim-filesystem-7.4.629-5.el6_ ...

  7. mac 上面安装jdk 1.6

    下载地址 https://support.apple.com/kb/dl1572?locale=zh_CN orcale 支持mac的最低版本是1.7

  8. (原创)c++11改进我们的程序之垃圾回收

    c#和java中有自动垃圾回收机制,.net运行时和java虚拟机可以管理分配的堆内存,在对象失去引用时自动回收,因此在c#和jva中, 内存管理不是大问题.c++语言没有垃圾回收机制,必须自己去释放 ...

  9. Custom Sublime Text Build Systems For Popular Tools And Languages

    Sublime Text is currently the text editor of choice for a number of developers in the open-source co ...

  10. XCode 7 运行 cocos2dx 2.2.6问题小节

    终于磕磕绊绊的在模拟器上,成功运行了已有项目. 公司提供的Mac系统,版本炒鸡低.向同事拷贝了OS,和XCode. 安装完成后,将已有的可以在Windows上成功运行Android版本的项目,拷贝到了 ...