Android -- Service的使用

來源:互聯網
上載者:User

標籤:android

Service正如其名服務,我們之前瞭解過Activity表示的是一個頁面,但是如果我們某些操作,不需要展示頁面,值需要進行背景一個操作,這時候我們可以建立一個Service,一般進行socket通訊、http請求等操作。

1、我們建立一個Android的工程
2、增加一個Service,代碼如下

package com.example.learnservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        new Thread() {            @Override            public void run() {                super.run();                //while (true) {                    System.out.println("後台運行...");                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                //}            }        }.start();        return super.onStartCommand(intent, flags, startId);    }}

繼承Service並且重寫onStartCommand,此方法在啟用此Service的時候執行。

3、我們在空的activity頁面增加2個按鈕,分別為啟動和停止Service的操作,代碼如下

package com.example.learnservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {    private Intent intent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        intent = new Intent(MainActivity.this, MyService.class);        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                startService(intent);            }        });        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("關閉後台服務....");                stopService(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;    }}

通過startService和stopService操作。

4、我們看下Android的後台運行進程,

5、我們先點擊啟動Service按鈕,再查看後台進程

看到了嗎,這個是我啟動的Service,它在後台被啟動起來啦 。

6、我們調用stopService方法停止Service,

Android -- Service的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.