您的位置:首页 > 移动开发 > Android开发

android 的service一点了解

2014-10-20 10:44 288 查看
关于Service的基本知识请点击这个链接。
http://blog.csdn.net/sanjay_f/article/details/40297475
-----------------------------------------------------------------------------------------

一般公司的开发,都只开一个Service.

当你的Service在启动的时候。
调用启动 startService(myService.makeIntent(this)); 是无效的?

测试下这段代码。---Service
public class myService extends IntentService {

private String TAG = this.getClass().getName();

public myService() {
super("myService");
}

public static Intent makeIntent(Context context) {
return new Intent(context, myService.class);
}

@Override
protected void onHandleIntent(Intent intent) {
uploadPlan();
}

/**
*
*/
private void uploadPlan() {

for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("tag", " index =" + i);
}
}

}


-------Activity;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

startService(
myService.makeIntent(getApplicationContext()));

}
});
}

}


--------------------------------------------------------------------------
打印结果

10-20 03:04:26.869: I/tag(2239):  index =0

10-20 03:04:27.369: I/tag(2239):  index =1

10-20 03:04:27.869: I/tag(2239):  index =2

10-20 03:04:28.369: I/tag(2239):  index =3

10-20 03:04:28.873: I/tag(2239):  index =4

10-20 03:04:29.369: I/tag(2239):  index =5

10-20 03:04:29.873: I/tag(2239):  index =6
10-20 03:04:30.369: I/tag(2239):  index =7

10-20 03:04:30.873: I/tag(2239):  index =8

10-20 03:04:31.373: I/tag(2239):  index =9

10-20 03:04:31.873: I/tag(2239):  index =0

10-20 03:04:32.373: I/tag(2239):  index =1

10-20 03:04:32.873: I/tag(2239):  index =2

10-20 03:04:33.377: I/tag(2239):  index =3

10-20 03:04:33.877: I/tag(2239):  index =4

10-20 03:04:34.377: I/tag(2239):  index =5

10-20 03:04:34.873: I/tag(2239):  index =6

10-20 03:04:35.377: I/tag(2239):  index =7

10-20 03:04:35.877: I/tag(2239):  index =8

10-20 03:04:36.377: I/tag(2239):  index =9

-------------------------------------

我们连续点击两次button。
发现,确实当service在运行的时候,Service确实没有重启,还是按序号打印进行下去。
不过他在打印结束,结束任务后。
会重新开启,继续打印!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: