android的Service的實(shí)例

package com.android.service;

import android.app.IntentService;
import android.content.Intent;

public class HelloIntentService extends IntentService{

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

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("休息8秒");
        
        try{
            Thread.sleep(8000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    public void onDestory(){
        System.out.println("執(zhí)行onHandleIntent之后會(huì)自動(dòng)調(diào)用!");
        super.onDestroy();
        
    }
    
    
}

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)永靖,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108

package com.android.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class HelloService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    public void onCreate(){
        super.onCreate();
        
        
        
    }

    public void onDestory(){
        
        super.onDestroy();
        
    }
    
    public void onStart(Intent intent,int startId) {
        // TODO Auto-generated method stub
        System.out.println("啟動(dòng)Service,休眠10秒");
        
        try{
            Thread.sleep(8000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    
    
}

package com.android.service;

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 MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(
                        MainActivity.this,HelloIntentService.class);
                startService(intent);
            }
        });
   

    Button button2=(Button)findViewById(R.id.button2);
    button.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(
                    MainActivity.this,HelloIntentService.class);
            stopService(intent);
        }
    });
    
    Button button3=(Button)findViewById(R.id.button3);
    button.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(
                    MainActivity.this,HelloService.class);
            startService(intent);
        }
    });
    
}
    

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="啟動(dòng)IntentService"
        
        
        
        
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止IntentService"
        
        
        
        
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="啟動(dòng)Service"
        
        
        
        
        />
    
    
   
</LinearLayout>

當(dāng)前文章:android的Service的實(shí)例
轉(zhuǎn)載來(lái)源:http://bm7419.com/article12/pciedc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)