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();
}
}
andorid service 本地服务的更多相关文章
- Android Service学习之本地服务
Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...
- Anroid四大组件service之本地服务
服务是Android四大组件之一,与Activity一样,代表可执行程序.但Service不像Activity有可操作的用户界面,它是一直在后台运行.用通俗易懂点的话来说: 如果某个应用要在运行时向用 ...
- Android-bindService本地服务-初步-Service返回对象
在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...
- android service 本地 远程 总结
android编写Service入门 android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service) ...
- windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改
以一个redis服务为例: 一个redis注册服务后一般是网络服务账户,但是当系统不存在网络服务账户时,就会导致redis服务无法正常启动.接下来修改redis服务的登录身份. cmd下输入如下命令: ...
- Android-bindService本地服务-音乐播放(后台播放)-下
在上一篇 Android-bindService本地服务-音乐播放-上,博客中是不能在后台中播放到,这次博客增加了一个后台播放 通常情况下,Activity切换到后台,Service提升到前台进程, ...
- Android-bindService本地服务-初步
在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...
- i.mx6 Android5.1.1 servicemanager本地服务
接在之前的 i.mx6 Android5.1.1 初始化流程之init进程 i.mx6 Android5.1.1 初始化流程之init.rc解析 servicemanager是由init创建的本地服务 ...
- Service Mesh服务网格新生代--Istio(转)
万字解读:Service Mesh服务网格新生代--Istio 官网地址:https://preliminary.istio.io/zh/docs/concepts/security/ Servic ...
随机推荐
- nginx相关配置说明
基础: nginx配置文件主要分为六个区域:main section.events section.http section.sever section. location section.upstr ...
- Hadoop学习13--zookeeper相关
zookeeper要保证各个server之间同步,实现同步的协议是zab协议.此协议有两种模式:恢复模式(选主)和广播模式(同步). 服务启动或者leader崩溃时,进入恢复模式.选举成功且大多数se ...
- FC 坦克大战 老巢铁墙
老巢外围铁墙E2A9:AC 80 EFEF80:A5 10 85 45 A5 45 AC D2 E2 用十六进制编辑器打开坦克大战的游戏文件搜索A5 45 F0 25 A5 0B改为AC 80 EF ...
- MVC4.0网站发布和部署到IIS7.0上的方法
最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...
- Spark RDD类源码阅读
每天进步一点点~开搞~ abstract class RDD[T: ClassTag]( //@transient 注解表示将字段标记为瞬态的 @transient private var _sc: ...
- Win32程序和控制台应用程序的项目互转设置
一般情况下,如果是windows程序,那么WinMain是入口函数,在VS2010中新建项目为"win32项目" 如果是dos控制台程序,那么main是入口函数,在VS2010中新 ...
- 实习日记:图像检索算法 LSH 的总结与分析(matlab)
最开始仿真和精度测试,基于 matlab 完成的. Demo_MakeTable.m (生成 Hash 表) %======================================== %** ...
- 云存储性能测试工具--COSBench安装
COSBench安装 Cosbench是Intel的开源云存储性能测试软件,COSBench目前已经广泛使用与云存储测试,并作为云存储的基准测试工具使用 1 环境 1.1 操作系统 COSBench可 ...
- Marathon
早上,挣扎到十点才起,刚好去吃过饭,来教研室,等待着中午的马拉松. 中午一直是很激动,有些紧张.一直到整个马拉松跑完,我达到了我唯一的目标,保持均匀的呼吸节奏.但我能明显感觉到,我并没有拼尽全力.我不 ...
- python socket和socketserver
Python提供了两个基本的socket模块.一个是socket,它提供了标准的BSD Socket API:另一个是socketServer,它提供了服务器中心类,可以简化网络服务器的开发. 下面先 ...