在Service里面装数据,从Activity里面用serviceConnection取数据

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="duqusjonclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="绑定停止服务"
android:onClick="bangdingtzonclick" />
</LinearLayout> </LinearLayout>

Service

package com.example.chenshuai.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class MyService_1 extends Service {
public MyService_1() { //构造方法里测试
Log.e("TAG","构造Service");
} int i = 0; Boolean aBoolean = true;
//自定义类,继承Binder,装数据
public class Mybinder extends Binder
{
public int getText()
{
return i;
}
} //绑定的回调方法
//必须要重写
@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(); //启动子线程,改变i的值
new Thread()
{
public void run()
{
while (aBoolean)
{
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} i++;
}
}
}.start(); return new Mybinder();
} @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","启动命令Command");
return super.onStartCommand(intent, flags, startId);
}
//解绑时候调用 @Override
public boolean onUnbind(Intent intent) { Log.e("TAG","解除绑定"); //停止循环
aBoolean = false;
return super.onUnbind(intent);
}
}

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; MyService_1.Mybinder mybinder; 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", "连接服务"); //接收数据 Mybinder
mybinder = (MyService_1.Mybinder)service; } //异常断开才会调用
@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();
}
} //读取服务数据
public void duqusjonclick(View view)
{
//调用服务对象的方法
if (mybinder != null)
{
Toast.makeText(ActivityService.this, "读取的信息是:"+mybinder.getText(), Toast.LENGTH_SHORT).show();
} } //重写回调方法,防止强制退出时,绑定服务还在运行
@Override
protected void onDestroy() { if (serviceConnection != null) {
//解除绑定
unbindService(serviceConnection); serviceConnection = null;
}
super.onDestroy();
}
}

manifest.xml

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

Android——Service装取数据的更多相关文章

  1. Android MaoZhuaWeiBo开发Service抓取个人信息-2

    前面把基本的东西讲完了,之后就是数据的获取和解析显示出来了,那接下来我们就负责抓取数据的这块吧,首先我们须要 在清单文件中载入服务和活动 加入:. <activity android:name= ...

  2. Android中Service通信(一)——启动Service并传递数据

    启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_ ...

  3. Android移动网络如何抓取数据包

    1)下载tcpdump工具 tcpdump(dump the traffic on a network)是Linux中强大的网络数据采集分析工具之一,可以将网络中传送的数据包头完全截获下来提供分析.它 ...

  4. 浅谈android Service和BroadCastReceiver

    1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...

  5. 多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中【我】

    多线程查询数据,将结果存入到redis中,最后批量从redis中取数据批量插入数据库中 package com.xxx.xx.reve.service; import java.util.ArrayL ...

  6. android应用安全——(数据抓包)跟踪监控android数据包

    转载博客:http://blog.csdn.net/xyz_lmn/article/details/8808169 web开发中Chrome.IE.firefox等浏览器都自带提供了插件帮助开发者跟踪 ...

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

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

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

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

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

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

随机推荐

  1. C#基础第六天-作业-利用面向对象的思想去实现名片

    1.利用面向对象的思想去实现: (增加,修改,删除,查询,查询全部)需求:根据人名去(删除/查询).指定列:姓名,年龄,性别,爱好,电话. 本系列教程: C#基础总结之八面向对象知识点总结-继承与多态 ...

  2. 更新django到2.x

    django-2.x 已经发布了.想体验一下它的新功能.于是把之前电脑上的django-1.11.7uninstall 掉 一.卸载django-1.11.7: pip3 uninstall djan ...

  3. Java:多线程,Exchanger同步器

    1. 背景 类java.util.concurrent.Exchanger提供了一个同步点,在这个同步点,一对线程可以交换数据.每个线程通过exchange()方法的入口提供数据给他的伙伴线程,并接收 ...

  4. git报错之index.lock

    当想回退到某个版本的时候,用git reset --hard commit_id,发现报错,原因是.git目录下多了个index.lock文件,可以通过rm命令删除,然后再回退 rm -f ./.gi ...

  5. 2-7-集合运算(A-B)∪(B-A)-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第2章  线性表 - 集合运算(A-B)∪(B-A) ——<数据结构>-严蔚敏.吴伟民版        ★有疑问先阅读★ 源码使用说明  链接☛☛☛ <数据结构-C语言 ...

  6. RenderTexture动态创建纹理

    CCRenderTexture,它允许你来动态创建纹理,并且可以在游戏中重用这些纹理. 使用 CCRenderTexture非常简单 – 你只需要做以下5步就行了: 创建一个新的CCRenderTex ...

  7. (原创)C++11改进我们的程序之简化我们的程序(四)

    这次要讲的是:c++11统一初始化.统一begin()/end()和for-loop循环如何简化我们的程序 初始化列表 c++11之前有各种各样的初始化语法,有时候初始化的时候还挺麻烦,比较典型的如v ...

  8. Ubantu 使用extundelete恢复数据

    所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候改如何快速.有效地恢复数据呢?本文我们就来介绍一下Linux系统下常用的几个数据恢复工具. 一.如何使用“rm -rf ...

  9. 每日英语:5 Things to Know About Missing Malaysia Airlines Flight and Air Safety

    Malaysia Airlines Flight MH370, with 239 people aboard, lost contact early Saturday with the airline ...

  10. 九个问题从入门到熟悉HTTPS

    九个问题从入门到熟悉HTTPS Q1: 什么是 HTTPS? LHQ: HTTPS 是安全的 HTTP HTTP 协议中的内容都是明文传输,HTTPS 的目的是将这些内容加密,确保信息传输安全.最后一 ...