Android——Service

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的更多相关文章
- android service两种启动方式
android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...
- 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 ...
- Android Service完全解析,关于服务你所需知道的一切(下)
转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...
- Android Service完全解析,关于服务你所需知道的一切(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...
- android service 的各种用法(IPC、AIDL)
http://my.oschina.net/mopidick/blog/132325 最近在学android service,感觉终于把service的各种使用场景和用到的技术整理得比较明白了,受益颇 ...
- Android service介绍和启动方式
1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...
- Android Service初始
一.Service概念 1.Service是一个应用程序组件 2.Service没有图像化界面 3.Service通常用来处理一些耗时比较长的操作 4.可以使用Service更新ContentProv ...
- Android Service与Thread的区别
Android Service,后台,Android的后台就是指,它的运行是完全不依赖UI的.即使Activity被销毁,或者程序被关闭,只要进程还在,Service就可以继续运行.比如说一些应用程序 ...
- Android service binder aidl 关系
/********************************************************************************** * Android servic ...
- Android Service AIDL 远程调用服务 【简单音乐播放实例】
Android Service是分为两种: 本地服务(Local Service): 同一个apk内被调用 远程服务(Remote Service):被另一个apk调用 远程服务需要借助AIDL来完成 ...
随机推荐
- Openssl aes加解密例程
原文链接: http://blog.csdn.net/itmes/article/details/7714854 假设我们已经下载了 openssl的源码,并成功编译,设置好了编程环境. 我们现在来看 ...
- CentOS 7安装Redis4.0.10
cd /usr/local/src && wget http://download.redis.io/releases/redis-4.0.10.tar.gz && t ...
- IPython介绍及安装
IPython介绍 - CSDN博客https://blog.csdn.net/gavin_john/article/details/53086766 python命令行在windows下实现tab自 ...
- 浅谈cocos2dx(18) 中工厂模式
----我的生活.我的点点滴滴! ! cocos2d-x中也有工厂模式.何为工厂模式,顾名思义就是用来产生产品的,工厂就是用来创建其它类对象的类,我们把这个创建其它类对象的类叫做工厂类.而这些被创建的 ...
- 具体解释Hibernate中cascade与inverse
学习hibernate的时候对级联关系的概念老是分不清楚,尤其是cascade.inverse傻傻分不清.以下通过样例来简单说明. 准备工作: 首先创建数据库,新建两张表: 教室表classes (字 ...
- 一点做用户画像的人生经验:ID强打通
1. 背景 在构建精准用户画像时,面临着这样一个问题:日志采集不能成功地收集用户的所有ID,且每条业务线有各自定义的UID用来标识用户,从而造成了用户ID的零碎化.因此,为了做用户标签的整合,用户ID ...
- Python——验证码识别 Pillow + tesseract-ocr
至于安装教程在这里不再重复说了,可以参考博客,网上有大把的教程 https://blog.csdn.net/testcs_dn/article/details/78697730 要是别的验证码是如下类 ...
- 批量修改Mysql数据库表Innodb为MyISAN
mysql -uroot -e "SELECT concat('ALTER TABLE ', TABLE_NAME,' ENGINE=MYISAM;') FROM Information_s ...
- impress.js 一个创建在线幻灯的js库
真的好奇怪,我居然会写前端技术的博客.没有办法的,最近实习,看的大多是前端.所以今天就用这个来练练手了. Impress.js 是一个非常棒的用来创建在线演示的Javascript库.它基于CSS3转 ...
- haproxy rpm制做
[root@c01 tmp]# fpm -s dir -t rpm -v 1.7.7 -n haproxy --before-install /tmp/haproxy_before.sh --afte ...