Android Service服務

一行代碼一行代碼地抄寫,寫的時候也順便想了想到底這代碼的功能是什麼。

這代碼是深圳圖書館借來的書上的,今天學習到Android Service服務模塊,

記下這些代碼,看雖然似簡單,但能通過這些代碼學習到Android Service程序的步驟甚至原理。

 

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="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="開始服務" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="停止服務" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


 

 

AndroidManifest.xml 文件內容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo.myService"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".myService"
                  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:enabled="true" android:name = ".excampleService"/>
    </application>
</manifest> 

 

myService.java文件內容如下:

package com.demo.myService;

import com.demo.myService.excampleService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class myService extends Activity {
    /** Called when the activity is first created. */
	Button btStart = null;
	Button btStop = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //綁定元素
        btStart = (Button)findViewById(R.id.Button01);
        btStop = (Button)findViewById(R.id.Button02);
        
        //新建Service意圖
        final Intent serviceIntent = new Intent(this,excampleService.class);
        
        //添加監聽接口以及消息處理函數
        btStart.setOnClickListener(new OnClickListener()
        {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//單擊開始服務按鈕後啓動服務
				startService(serviceIntent);
			}
        	
        });
        
        //添加監聽接口以及消息處理函數
        btStop.setOnClickListener(new OnClickListener()
        {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//單擊開始服務按鈕後啓動服務
				stopService(serviceIntent);
			}
        	
        });
    }
}


excampleService.java 文件內容如下:

package com.demo.myService;

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

public class excampleService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//當服務第一次創建時調用該方法
	public void onCreate(){
		Log.d("MyService","onCreate");
		super.onCreate();
	}
	
	//當服務銷燬時調用該方法
	public void onDestroy(){
		Log.d("MyService","onDestroy");
		super.onDestroy();
	}
	
	//當服務開始時調用該方法
	public void onStart(Intent intent,int startId){
		Log.d("MyService","onStart");
		super.onStart(intent,startId);
	}

}



 

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