MainActivity.java

package com.hanqi.test2;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { 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(); }
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 sc;
public void bt3_onclick(View v)
{
//通过意图Intent
//显式意图
Intent intent = new Intent(this,TestService1.class);
if (sc == null) { sc = 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,sc, Context.BIND_AUTO_CREATE); Toast.makeText(MainActivity.this, "绑定服务成功", Toast.LENGTH_SHORT).show(); } public void bt4_onclick(View v)
{
//判断是否已经绑定,连接是否为空
if (sc != null)
{
//解除绑定
unbindService(sc);
sc = null;
Toast.makeText(MainActivity.this, "解除绑定", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "尚未绑定", Toast.LENGTH_SHORT).show();
}
} @Override
protected void onDestroy() { if (sc != null) {
//解除绑定
unbindService(sc);
sc = null;
}
super.onDestroy();
}
}

TestService1.java

package com.hanqi.test2;

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);
}
}

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.test2.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>

效果图:

Android课程---关于Service的学习(后台运行)的更多相关文章

  1. Android课程---关于对话框的学习

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Android开发按返回键应用后台运行

    @Override  public boolean onKeyDown(int keyCode, KeyEvent event) {   if (keyCode == KeyEvent.KEYCODE ...

  3. Android 中的 Service 全面总结

    1.Service的种类   按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另 ...

  4. Android 中的 Service 全面总结(转载)

    转载地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 感谢作者 Android 中的 Service 全面总结 1.Ser ...

  5. Android 中的 Service 全面总结 (转)

    原文地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 1.Service的种类   按运行地点分类: 类别 区别  优点 ...

  6. Android开发之Service的写法以及与Activity的通信

    Service的总结: 1.按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外 ...

  7. Android四大组件——Service

    Service相关链接 Service初涉 Service进阶 Service精通 Service是Android系统中的一种组件,它跟Activity的级别差不多,但是它不能自己运行,只能后台运行, ...

  8. Android中的Service 与 Thread 的区别[转]

    很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...

  9. 关于android编程中service和activity的区别

    一. 绝大部分情况下,Service的作用是用来“执行”后台的.耗时的.重要的任务,三者缺一不可,而最重要的原因是第三点:要执行重要的任务. 因为当一个进程启动了Service后,进程的优先级变高了, ...

随机推荐

  1. JavaScript跨域提交数据

    1.通过jsonp跨域    场景:假设前台有JS方法"crossJS", 1.1发送请求http://www.xxx.com/?callback=crossJS.(创建一个scr ...

  2. Fight my work!

    来这个公司第一天工作, 上来就是装ubantu系统,对于玩linux玩的不熟的我.还是相当吃力的, 反正有问题尝试着自己解决,不会就问, 压力还是很大了. 学了一下企业的历史,理念等相关信息,也没重点 ...

  3. PHP发送短信功能

    发送短信的功能主要在于获得短信接口后,在函数中模仿用户行为,例如浏览器跳转输出短信接口的链接. 需要运用的函数为 curl_init(); curl_setopt(); curl_exec(); cu ...

  4. Tips标签显示

    浏览地址 http://www.7yusou.com 鱼搜官网 下载到本地

  5. Python 爬取网站资源文件

    爬虫原理: 以下来自知乎解释 首先你要明白爬虫怎样工作.想象你是一只蜘蛛,现在你被放到了互联“网”上.那么,你需要把所有的网页都看一遍.怎么办呢?没问题呀,你就随便从某个地方开始,比如说人民日报的首页 ...

  6. 2016-11-05实战-定义ssh服务的日志

    1.编辑/etc/rsyslog.conf 输入 local 0 .*     /var/log/sshd.log   #日志的保存路径 2.定义ssh服务的日志级别 编辑sshd服务的主配置文件:/ ...

  7. 在Asp.Net MVC 中如何用JS访问Web.Config中appSettings的值

    应用场景: 很多时候我们要在Web.Config中添加appSettings的键值对来标识一些全局的信息,比如:调用service的domain,跳转其他网站页面的url 等等: 那么此时就涉及到了一 ...

  8. 获取 windows 商店内的 aapx 安装包 并 安装(旁加载)

    这是一篇教程,写在 win10 版<量子破碎>发售近期. 主要原因:windows 商城的应用下载实在难以忍受...... #######2016-4-9更新####### 4-6号通过旁 ...

  9. Protocol Buffer搭建及示例

    本文来源:http://www.tanhao.me/code/150911.html/ Protocol Buffer(简称Protobuf或PB)是由Google推出的一种数据交换格式,与传统的XM ...

  10. 毕业论文—使用js将canvas保存为图片文件,并且自定义文件名

    该文章引用http://blog.csdn.net/qq547276542/article/details/51906741 1.从canvas中直接提取图片元数据 // 图片导出为 png 格式 v ...