AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lpc.service">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Myservice"></service>
</application>
</manifest>
MainActivity.java
package com.example.lpc.service;
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.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//啓動服務
public void startMyService(View v) {
Intent intent = new Intent(this, Myservice.class);
startService(intent);
Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show();
}
public void stopMyService(View v) {
Intent intent = new Intent(this, Myservice.class);
stopService(intent);
Toast.makeText(this, "stop service", Toast.LENGTH_SHORT).show();
}
private ServiceConnection conn;
public void bindMyService(View v) {
Intent intent = new Intent(this, Myservice.class);
if (conn == null) {
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("ATG", "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("ATG", "onServiceDisconnected");
}
};//實現一個接口 注意有逗號
} else {
Toast.makeText(this, "已經綁定", Toast.LENGTH_SHORT).show();
}
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void unbindMyService(View v) {
if (conn != null) {
unbindService(conn);
conn = null;
Toast.makeText(this, "綁定", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "no綁定", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (conn != null) {
unbindService(conn);
conn = null;
}
}
}
MyService.java
package com.example.lpc.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/*
1. startService(intent)
第一次調用 : -->構造方法()-->onCreate()-->onStartCommand()
(重要)後面再調用 : -->onStartCommand()
stopService() : -->onDestory()
2. bindService(intent, serviceConnection)
調用 : -->構造方法()-->onCreate()-->onBind()-->onServiceConnected()
unbindService(): (中有當前Activity與Service連接)-->onUnbind()-->onDestroy()
*/
public class Myservice extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("TAG", "onBind()");
return new Binder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG", "onUnbind()");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG","onCreate()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG","onDestory()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG","onStarteCommand()");
return super.onStartCommand(intent, flags, startId);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1. 測試啓動本地服務"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="startMyService"
android:text="啓動服務" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stopMyService"
android:text="停止服務" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2. 測試綁定本地服務"
android:textSize="25sp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="bindMyService"
android:text="綁定服務" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="unbindMyService"
android:text="解綁服務" />
</LinearLayout>
</LinearLayout>