Android之Service,androidservice

來源:互聯網
上載者:User

Android之Service,androidservice
服務是運行在背景一段代碼。不是進程,也不是線程,但它運行在進程和線程中。Android中的服務與Activity不同,不能與使用者互動,不能自己啟動。媒體播放器的服務,當使用者退出媒體選擇使用者介面,仍然希望音樂依然可以繼續播放,這就是由服務Service來保證當使用者介面關閉時音樂繼續播放。當我們某個應用的資料是通過網路擷取的,不同時間的資料是不同的,這時我們可以用Service在後台定時更新,而不用每次開啟應用的時候去擷取。

執行個體:ServiceDemo
運行效果:


代碼清單:
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.rainsong.servicedemo"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk        android:minSdkVersion="11"        android:targetSdkVersion="19" />    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">        <activity android:name="MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".MyService" />    </application></manifest>

布局檔案:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <Button android:id="@+id/btn_start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Start Service" />    <Button android:id="@+id/btn_stop"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Stop Service" /></LinearLayout>

Java原始碼檔案:MainActivity.java
package com.rainsong.servicedemo;import android.app.Activity;import android.os.Bundle;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity{    Button btn_start;    Button btn_stop;        OnClickListener listener_start;    OnClickListener listener_stop;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        listener_start = new OnClickListener() {            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, MyService.class);                startService(intent);            }        };        listener_stop = new OnClickListener() {            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, MyService.class);                stopService(intent);            }        };        btn_start = (Button)findViewById(R.id.btn_start);        btn_start.setOnClickListener(listener_start);        btn_stop = (Button)findViewById(R.id.btn_stop);        btn_stop.setOnClickListener(listener_stop);    }}

Java原始碼檔案:MyService.java

package com.rainsong.servicedemo;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;import android.util.Log;public class MyService extends Service {    @Override    public IBinder onBind(Intent i) {        return null;    }    @Override    public boolean onUnbind(Intent i) {        return false;    }    @Override    public void onCreate() {        Toast.makeText(this, "Service onCreate", Toast.LENGTH_SHORT).show();    }    @Override    public void onStart(Intent i, int startId) {        Toast.makeText(this, "Service onStart", Toast.LENGTH_SHORT).show();    }    @Override    public void onDestroy() {        Toast.makeText(this, "Service  onDestroy", Toast.LENGTH_SHORT).show();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.