android初接觸之service1

初學參考http://blog.csdn.net/wtao158/article/details/5149721http://www.cnblogs.com/gzgg/archive/2012/02/20/2359485.html的解釋說明
1.對於android的service的理解
android中的sevice是android用於做後臺服務的便於進行多任務同時運行
2.實現方式有兩種
1>啓動某個服務使用startService();啓動某個服務調用了服務中onCreate()方法進行啓動
2>綁定某個服務到調用的客戶類綁定如果調用這個客戶類被銷燬,Service 也會被銷燬。用這個方法的一個好處是,bindService() 方法執行後 Service 會回調上邊提到的 onBind() 方發,你可以從這裏返回一個實現了 IBind 接口的類,在客戶端操作這個類就能和這個服務通信了,比如得到 Service 運行的狀態或其他操作。如果 Service 還沒有運行,使用這個方法啓動 Service 就會 onCreate() 方法而不會調用 onStart()。
3.代碼實現:
方式1:創建一個activity類讓他繼承service這個抽象類重寫onCreate()和onDestroy()這兩個方法

package com.example.test;

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

public class ServiceTest extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("服務啓動");
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("服務註銷");
    }
}

在activity_main.xml中加入連個button用於啓動和停止服務

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.MainActivity" >

    <Button
        android:id="@+id/startService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="啓動服務" />

    <Button
        android:id="@+id/destoryService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/startService"
        android:layout_alignRight="@+id/startService"
        android:layout_below="@+id/startService"
        android:text="註銷服務" />

</RelativeLayout>

在MainActivity.java中給button加入監聽和調用service

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements OnClickListener {
    private Button startService,destroyService;
    private Intent IntentService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //創建調用服務的Intent
        IntentService = new Intent(this,ServiceTest.class);
        //獲取button按鈕
        startService = (Button) findViewById(R.id.startService);
        destroyService = (Button) findViewById(R.id.destoryService);
        startService.setOnClickListener(this);
        destroyService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.startService:
            //啓動服務
            startService(IntentService);
            break;
        case R.id.destoryService:
            //停止服務
            stopService(IntentService);
            break;
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

在AndroidMainfest.xml中加入調用服務的seriver標籤

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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="ServiceTest"></service>

    </application>

</manifest>

這樣點擊按鈕就可以在logcat中顯示了注意要在allmessages中才能看到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章