Service中的Intentservice

1.类似于Activity类和其他组件,开发人员必须在应用程序配置文件中声明全部的service.为了声明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;
 }

}

 

附件:http://down.51cto.com/data/2364289

新闻名称:Service中的Intentservice
链接分享:http://lszwz.com/article/gdeios.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款