service是在主线程中运行的,我们要处理耗时操作是在服务里还要新建线程,那服务有什么好处呢?
服务一旦运行,程序退出是不会被销毁的(最好的例子就是播放器),除非人为停止或程序崩溃
接下来请看
这是服务要处理的耗时操作,每秒数据加一
package com.example.serverthread;
import android.util.Log;
public class MyThread extends Thread{
public static int i = 0;
//线程开关
boolean flag;
//留出一个控制线程开启的接口
public void setStart(){
flag = true;
start();//启动线程
}
//留出一个控制线程关闭的接口
public void setStop(){
flag = false;
}
@Override
public void run() {
//true就跑起线程
while(flag){
try {
Thread.sleep(1000);
i++;
Log.i("log", "MyThread if running ---->" + i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是服务
package com.example.serverthread;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyServer extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.i("log", "---onBind---->");
return null;
}
/**
* 当服务一旦开启,只执行一次方法onCreate, 除非应用程序被强制干掉
*/
@Override
public void onCreate() {
Log.i("log", "---onCreate---->");
super.onCreate();
}
MyThread mThread;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("log", "---onStartCommand---->");
/**
* 服务运行在主线程,不能进行耗时操,所以开启线程处理数据 当程序退出时,服务没有关闭的情况下,这里的东西都没有被销毁
* 当程序再次进来时,我们依然能够找到上次开辟的线程
*/
String action = intent.getStringExtra("action");
Log.i("log", action);
if (action.equals("start")) {
// 如果不为空,创建线程
if (mThread == null) {
mThread = new MyThread();
mThread.setStart();
Toast.makeText(getApplicationContext(), "服务开启",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "服务下的线程已经开启",
Toast.LENGTH_LONG).show();
}
} else if (action.equals("stop")){
// 如果不为空,销毁线程
if (mThread != null) {
mThread.setStop();
mThread = null;
//服务自己关闭服务
stopSelf();
Toast.makeText(getApplicationContext(), "服务关闭",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "服务下的线程已经销毁",
Toast.LENGTH_LONG).show();
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("log", "---onDestroy---->");
super.onDestroy();
}
}
这是主程序
package com.example.serverthread;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTv;
Button mStart, mEnd;
Intent myServer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.main_text);
mStart = (Button) findViewById(R.id.main_bt_start);
mEnd = (Button) findViewById(R.id.main_bt_end);
myServer = new Intent(MainActivity.this, MyServer.class);
mStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 给服务发送消息,控制线程的开启与关闭
myServer.putExtra("action", "start");
// 开启服务
startService(myServer);
}
});
mEnd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 给服务发送消息,控制线程的开启与关闭
myServer.putExtra("action", "stop");
// 关闭服务
startService(myServer);
}
});
}
}
这是效果图