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 ...
随机推荐
- php接口和抽象类
接口关键字:interface,不加class关键字接口里面有成员方法,但是没有函数体.实现接口使用的关键字:implements 不是extends子类必须实现接口的所有方法 使用接口,你可以指定某 ...
- 【AT91SAM3S】英倍特串口示例工程05-UART中,串口是怎样初始化的
在这个示例工程的main.c文件中,进入main之后,没有发现串口功能的任何配置.直接使用了printf这个东西进行输出.将软件下载到开发板上之后,在电脑端使用串口软件,可以看板子有数据发来.说明这个 ...
- Hibernate5.2之原生SQL查询
Hibernate5.2之原生SQL查询 一. 介绍 在上一篇博客中笔者通过代码的形式给各位读者介绍了Hibernate中最重要的检索方式--HQL查询.在本博文中笔者将向各位读者介绍Hiberna ...
- 51nod 1290 Counting Diff Pairs 莫队 + bit
一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[j]中,有多少对数,abs(A[i] - A[j]) <= K(abs表示绝对值) ...
- 企业架构(Enterprise Architecture)
ylbtech-Miscellaneos: 企业架构(Enterprise Architecture) A,返回顶部 1, 简称EA.是指对企业事业信息管理系统中具有体系的.普遍性的问题而提供的通用解 ...
- error BK1506 : cannot open file '.\Debug\????????.sbr': No such file or dire
http://blog.csdn.net/shuilan0066/article/details/8738035 分类: 调试错误信息2013-03-29 19:08492人阅读 ...
- 每天一个Linux命令(3):pwd命令
Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文 ...
- 根据 MySQL 状态优化 ---- 1. 慢查询
查看 MySQL 服务器运行的各种状态值: mysql> show global status: 1. 慢查询 mysql> show variables like '%slow%'; + ...
- ZYB's Premutation POJ5592
Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...
- JNI日志调试LOG和中文乱码
添加日志: 1. 增加log支持. Android.mk文件增加LOCAL_LDLIBS += -llog 2. C代码中增加(放在最前面) #include <android/log.h> ...