重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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://cqcxhl.com/article/gdeios.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP