首页 > 其他 > 详细

Service 中的Intent service

时间:2014-04-13 05:09:01      阅读:1054      评论:0      收藏:0      [点我收藏+]

1.类似于Activity类和其他组件,开发人员必须在应用程序配置文件中声明全部的service.为了声明Service,需要向<application>标签中添加<Service>子标签如图 Mainest中的配置

2.如  IntentService 工程 图一个service类在Activity 去startService

3.对于InternService 所包含的的的全部操作:没有参数的构造方法和onHandleIntent()方法,如果开发人员决定重写其他回调方法,如OnCreate()、OnStartCommand()或OnDestory(),需要调用父类实现,这样IntentService能正确处理工作线程的生命周期

4.Service类中的代码:

package com.example.intentservice;

import android.app.IntentService;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;

public class CurrentTimeService extends IntentService {

 

 public CurrentTimeService() {
  super("CurrentTimeService");
  // TODO Auto-generated constructor stub
 }

 @Override
 protected void onHandleIntent(Intent intent) {
  // TODO Auto-generated method stub
  Time time  = new Time();//创建对象
  time.setToNow();//获取当前系统时间
  String currentTime = time.format("%Y-%m-%d %H:%M:%S");
  Log.i("CurrentTimeService", currentTime);


 }

}

 

5.Activity类中的代码:

package com.example.intentservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CurrentTimeActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button button1=(Button)findViewById(R.id.button1);
  button1.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    startService(new Intent(CurrentTimeActivity.this, CurrentTimeService.class));

   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

 

Service 中的Intent service,布布扣,bubuko.com

Service 中的Intent service

原文:http://7129486.blog.51cto.com/7119486/1394631

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!