Android课程---关于Service的学习(后台运行)
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的学习(后台运行)的更多相关文章
- Android课程---关于对话框的学习
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android开发按返回键应用后台运行
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE ...
- Android 中的 Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另 ...
- Android 中的 Service 全面总结(转载)
转载地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 感谢作者 Android 中的 Service 全面总结 1.Ser ...
- Android 中的 Service 全面总结 (转)
原文地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 1.Service的种类 按运行地点分类: 类别 区别 优点 ...
- Android开发之Service的写法以及与Activity的通信
Service的总结: 1.按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外 ...
- Android四大组件——Service
Service相关链接 Service初涉 Service进阶 Service精通 Service是Android系统中的一种组件,它跟Activity的级别差不多,但是它不能自己运行,只能后台运行, ...
- Android中的Service 与 Thread 的区别[转]
很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...
- 关于android编程中service和activity的区别
一. 绝大部分情况下,Service的作用是用来“执行”后台的.耗时的.重要的任务,三者缺一不可,而最重要的原因是第三点:要执行重要的任务. 因为当一个进程启动了Service后,进程的优先级变高了, ...
随机推荐
- SMP、NUMA、MPP(Teradata)体系结构介绍
从系统架构来看,目前的商用服务器大体可以分为三类,即对称多处理器结构 (SMP : Symmetric Multi-Processor) ,非一致存储访问结构 (NUMA : Non-Uniform ...
- UML大战需求分析——阅读笔记05
最近看过几个程序员大学后一起创业,与大公司抢项目并成功逆袭的视频,感触颇深:第一.技术是关键:第二.有一群可靠并且技术超群的队友,在关键时刻不会掉链子:第三.善于部署谨慎周密的计划:第四.一流的口才+ ...
- 使用dd命令备份Linux分区
为了备份分区,开始使用的是Remastersys,但最终生成的iso文件仅有几十K,应该是软件bug,且此软件不再更新,后尝试使用Linux Respin,但github一直连接不上. 其实可以尝试使 ...
- gcc -Wall -pedantic -ansi(转载)
转载自R-G-Y-CQ的新浪博客 -Wall显示所有的警告信息 -Wall选项可以打开所有类型的语法警告,以便于确定程序源代码是否是正确的,并且尽可能实现可移植性. 对Linux开发人员来讲,GCC给 ...
- Python的多类型传值和冗余参数
多类型传值(向函数中传递元组和字典) 1 向函数中传递元组 def func(x,y): print x+y 调用这个函数的时候,我们只需要传入两个变量就可以了,但是比如我有一个元组t = ( ...
- swift 手机号码正则表达式 记录一下
func isTelNumber(num:NSString)->Bool { var mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$&qu ...
- JDBC入门之一--连接Mysql实验
工具:mysql-connector-java-5.1.40.eclipse 1)首先要将mysql-connector-java包整合到eclipse中,右击项目,然后选择build path,出现 ...
- HTML基础知识
一个完美的web前端攻城狮,所具备的专业素养有:HTML5.XHTML.CSS3.JavaScript.JQuery.PS.PHP等.所以说,我要学的东西还有很多... 没别得,我也是一个H5的初学者 ...
- Hololens 手势事件执行顺序
InteractionManager_SourcePressed (Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runt ...
- 线段树 - ZYB's Premutation
ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ...