1. 程式人生 > >Android Service 服務 1.第一種開啟和關閉服務的方式

Android Service 服務 1.第一種開啟和關閉服務的方式

分別介紹兩種開啟和關閉服務的方式

第一種開啟和關閉服務的方式

/MyService/res/layout/activity_main.xml

<LinearLayout  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:orientation="vertical"
    tools:context="com.briup.myservice.MainActivity" >

    <Button 
        android:id="@+id/btn_onestartservice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一種開啟服務"/>
    <Button 
        android:id="@+id/btn_onestopservice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一種停止服務"/>
</LinearLayout>
/MyService/src/com/briup/myservice/MyService.java
package com.briup.myservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind執行了");
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("onCreate執行了");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onStartCommand執行了");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("onDestroy執行了");
	}
}

/MyService/src/com/briup/myservice/MainActivity.java

package com.briup.myservice;

import com.briup.myservice.MyService.MyBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
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 Activity implements OnClickListener {
	private Button btn_onestartservice, btn_onestopservice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_onestartservice = (Button) findViewById(R.id.btn_onestartservice);
		btn_onestartservice.setOnClickListener(this);
		btn_onestopservice = (Button) findViewById(R.id.btn_onestopservice);
		btn_onestopservice.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_onestartservice:// 第一種啟動服務的方式
			//直接跳到服務中
			Intent intent = new Intent(MainActivity.this, MyService.class);
			startService(intent);// 啟動服務
			break;
		case R.id.btn_onestopservice:// 第一種停止服務的方式
			Intent intent2 = new Intent(MainActivity.this, MyService.class);
			stopService(intent2);// 關閉服務
			break;
		default:
			break;
		}
	}

}

配置服務


執行方式,點選"第一種開啟服務方式"按鈕,


點選“第一種停止服務”按鈕