刚开始学习Android,最开始的配置环境都搞了好久,之后又一直在搞算法方面和继续学习Java,Android自从最开始看过Activity和Intent跟着视频做了一个计算器之后再也没花时间搞过。今天终于又抽出一些时间来学习一下Service,由于之前疏忽一直没有做笔记和总结如今已经忘了七七八八,所以今天Service看完之后根据自己的理解做一些笔记和总结,以便于以后的复习和查阅。
首先Service是Android四大组件的一种,它与Activity很像但是却有着区别,Activity是前台页面,而Service是后台运行,不会显示在前台,而让程序运行在后台是非常必需的,比如我现在要做的项目的最主要的代码实现都是要在后台进行的,好吧,废话了好多,总结一下今天学习的内容吧。
首先我们创建一个工程,在UI视图添加两个按钮来控制Service的开始与结束,然后在MainActivity中添加两个监听事件以监听两个按钮,在最后写一个点击事件。代码贴出来的是将监听事件写在了类的前面(用了接口,但是java的接口还没有完全搞懂还有点不明白看来还需要回头再看一遍了T_T),所以我们把点击事件统一在最后写了一个函数,而另外一种方式就是直接在监听函数后面写点击事件。
添加按钮
<Button
android:layout_width="wrap_content" //按钮宽度
android:layout_height="wrap_content" //按钮高度
android:text="启动服务" //按钮文本内容
android:id="@+id/btnStartService" /> //按钮id
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:id="@+id/btnStopService" />
监听事件
//监听按钮btnStartService findViewById(R.id.btnStartService).setOnClickListener(this);
//监听按钮btnStopService findViewById(R.id.btnStopService).setOnClickListener(this);
创建Service
package com.example.connectservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder; //总有一天我要搞懂这些库是怎么实现的 =_=
public class MyService extends Service {
private boolean running=false; //用于控制后台代码的开始与结束
private String data="这是默认信息"; //自定义的一个字符串
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
} //"Service被创建时回调该方法",说实话我不知道这个是干吗的,下次再认真看看
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
data = intent.getStringExtra("data");
return super.onStartCommand(intent, flags, startId);
} //onStartCommand只在Service被创建时调用一次,如果服务没有被destroy掉不会再次调用
@Override
public void onCreate(){
super.onCreate();
running=true; //将running赋值为真使代码运行
new Thread(){
@Override
public void run(){
super.run();
while (running){ //如果不destroy(其中running值会被赋为false)则一直循环输出
System.out.println(data); //输出
try {
sleep(1000); //休息一秒
} catch (InterruptedException e) {
e.printStackTrace();
}//异常抛出处理
}
}
}.start();
}
@Override
public void onDestroy(){
super.onDestroy();
running=false; //终止函数停止输出
}
} //onCreat在每次点击启动服务时都会重新调用,而且是作为后台运行代码的主要放置地带你
点击事件
public void onClick(View v) { //点击事件发生
switch (v.getId()){ //获取点击事件的值(确定点的哪个)
case R.id.btnStartService: //如果点的是启动服务
Intent i=new Intent(this,MyService.class);//创建一个Intent,并且指向需要启动的服务
i.putExtra("data",etData.getText().toString());
/**因为Intent本身可以携带信息,所以我们在UI视图写了一个EditText获取用户的输入内容,代码在后面放出来再写上注释*/
startService(i); //启动服务
break;
case R.id.btnStopService: //如果点击了终止服务
stopService(new Intent(this,MyService.class); //那么就直接终止服务o
break;
}
}
EditText
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="默认信息"
android:id="@+id/etData"/>
private EditText etData; //定义获取输出内容的数据
etData= (EditText) findViewById(R.id.etData); //监听获取用户输入内容
i.putExtra("data",etData.getText().toString()); //将输入的内容传入Intent
OK,Service的基本使用大概就是这样了,生命周期在注释中已经大致说明了,Service的绑定与解除绑定此处还没有写出来,下次在写出来好了。显示的结果是这样的。(o.o)
01-21 21:41:00.493 13715-14432/com.example.connectservice I/System.out: 这是默认信息
01-21 21:41:01.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:02.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:03.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:04.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:05.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:06.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:07.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:08.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:09.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:10.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:11.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:12.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:13.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:14.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:15.493 13715-14432/com.example.connectservice I/System.out: 默认信息
01-21 21:41:16.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:17.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:18.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:19.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:20.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:21.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:22.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:23.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:24.493 13715-14432/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:27.723 13715-14886/com.example.connectservice I/System.out: 这是默认信息
01-21 21:41:28.723 13715-14886/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:29.723 13715-14886/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:41:30.723 13715-14886/com.example.connectservice I/System.out: 默认信息看就课
01-21 21:42:23.773 13715-15894/com.example.connectservice I/System.out: 这是默认信息
01-21 21:42:24.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:25.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:26.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:27.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:28.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:29.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:30.773 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:31.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:32.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:33.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:34.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:35.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:36.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:37.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:38.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:39.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:40.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:41.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:42.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:43.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:44.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?
01-21 21:42:45.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:46.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:47.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:48.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:49.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:50.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:51.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:52.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:53.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:54.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:55.783 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
01-21 21:42:56.793 13715-15894/com.example.connectservice I/System.out: 你猜我猜不猜得到你能不能猜到我可以猜到你是谁?哈哈哈哈
哈哈哈,就这样好了,有一些地方还没弄懂还有一些地方有一些小问题明天再继续搞好了,cf比赛开始了~~~开打。加油!!!