Service 中的Intent service

发布时间:2020-06-19 09:20:30 作者:王琦123456
来源:网络 阅读:357

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;
 }

}

 

附件:http://down.51cto.com/data/2364289
推荐阅读:
  1. android的service
  2. Service详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

service 应用程序 配置文件

上一篇:OnTouchListener触摸事件

下一篇:TextView中ellipsize属性

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》