溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Service 中的Intent service

發(fā)布時(shí)間:2020-06-19 09:20:30 來(lái)源:網(wǎng)絡(luò) 閱讀:370 作者:王琦123456 欄目:移動(dòng)開(kāi)發(fā)

1.類似于Activity類和其他組件,開(kāi)發(fā)人員必須在應(yīng)用程序配置文件中聲明全部的service.為了聲明Service,需要向<application>標(biāo)簽中添加<Service>子標(biāo)簽如圖 Mainest中的配置

2.如  IntentService 工程 圖一個(gè)service類在Activity 去startService

3.對(duì)于InternService 所包含的的的全部操作:沒(méi)有參數(shù)的構(gòu)造方法和onHandleIntent()方法,如果開(kāi)發(fā)人員決定重寫(xiě)其他回調(diào)方法,如OnCreate()、OnStartCommand()或OnDestory(),需要調(diào)用父類實(shí)現(xiàn),這樣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();//創(chuàng)建對(duì)象
  time.setToNow();//獲取當(dāng)前系統(tǒng)時(shí)間
  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
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI