Service类与 Activity类的原理类似,只是Service类并不像Activity一样提供与用户交互的表示层。Service是一种后台运行的Android组件,位于android.app包下。
当应用程序不需要在前台显示某些界面时,可以使用Service来完成,比如后台的数据计算,后台音乐播放等。
这个小例子是同时介绍Service组件与BroadcastReceiver组件联合使用的方法,在例子中使用java代码动态注册 BroadcastReceiver 并且通过前台的用户界面控制后台服务的进行。
运行如下
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">请启动 Service</string>
<string name="app_name">xh_test_03</string>
<string name="myButton1">启动Service</string>
<string name="myButton2">停止Service</string>
</resources>
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:gravity="center_horizontal"
android:text="@string/hello"
/>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myButton1"/>
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myButton2"/>
</LinearLayout>
Service类 MyService
package xiaohang.zhimeng;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
MyThread myThread;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
myThread.flag = false;
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
myThread = new MyThread();
myThread.start();
super.onStart(intent, startId);
}
class MyThread extends Thread {
boolean flag = true;
int c = 0;
@Override
public void run() {
while (flag) {
Intent i = new Intent("xiaohang.zhimeng.myThread");
i.putExtra("myThread", c);
sendBroadcast(i);
c++;
try {
// 睡眠指定毫秒数
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
}
Activity类 Activity01
package xiaohang.zhimeng;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Activity01 extends Activity {
Button button1;
Button button2;
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) this.findViewById(R.id.myButton1);
button2 = (Button) this.findViewById(R.id.myButton2);
myTextView = (TextView) this.findViewById(R.id.myTextView);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Activity01.this, MyService.class);
startService(i);
Toast.makeText(Activity01.this, "Service启动成功",
Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 创建Intent
Intent i = new Intent(Activity01.this, MyService.class);
stopService(i);
Toast.makeText(Activity01.this, "Service停止成功",
Toast.LENGTH_SHORT).show();
}
});
// 创建IntentFilter过滤器
IntentFilter intentFilter = new IntentFilter(
"xiaohang.zhimeng.myThread");
MyBroadcasReceiver myBroadcasReceiver = new MyBroadcasReceiver();
registerReceiver(myBroadcasReceiver, intentFilter);
}
public class MyBroadcasReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 获得Intent中的数据
Bundle myBundle = intent.getExtras();
int myInt = myBundle.getInt("myThread");
if (myInt < 10) {
myTextView.setText("后台Service运行了" + myInt + "秒");
} else {
Intent i = new Intent(Activity01.this, MyService.class);
// 停止服务
stopService(i);
myTextView.setText("后台Service在" + myInt + "秒后停止运行");
}
}
}
}
最后在若弱的 提醒大家一句 服务需要在 AndroidManifest.xml中声明一下
源码见附件
- 大小: 44 KB
- 大小: 15.5 KB
- 大小: 24.4 KB
- 大小: 14.5 KB
分享到:
相关推荐
android service 简单实例代码 . binder继承一个接口,实现方法。
2、针对对普通类绑定service进行测试 3、普通类可以绑定service,但是需要activity传入一个Contex参数 4、该测试可以将业务代码和activity或者service分离,实现程序更好的结构,提高代码可读性 5、欢迎大家一起讨论...
android,启动service,并且service把值传给Activity显示
android aidl service
Android Service下载文件
Demo android service toast 第一个版本
关于Android Service的简单实例:属于start service类型,而不是bound service。
android service使用的小demo 包括startService stopService bindService unbindService 两种开启、关闭service的小demo
Android Service Demo
Android service类的应用及源码例子,通过Android service类创建一个音乐服务模块,演示service类的创建、启动和方法使用,以及Service生命周期的说明,该例子继承service 然后重写onStart播放音乐 重写onDestry停止...
Android Service使用,包括Start Service和bind Service启动方式。Service与Acvitity互相传递数据
Android Service下载,EventBus更新界面,取代传统的用Handler或者广播更新界面,代码更加优雅,更加简洁
这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
android service简单代码编写 包括详细的注释信息
android学习之Service启动1
android service 源码 学习
android service 学习(下) ,共两部分
Android Service之start实现