標籤:
MainActivity.java
package com.hanqi.test2;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //一般方式啟動Service //參考Activity 的啟動方式 public void bt1_onclick(View v) { //通過意圖Intent //顯式意圖 Intent intent = new Intent(this,TestService1.class); startService(intent); Toast.makeText(MainActivity.this, "啟動Service", Toast.LENGTH_SHORT).show(); } public void bt2_onclick(View v) { //通過意圖Intent //顯式意圖 Intent intent = new Intent(this,TestService1.class); stopService(intent); Toast.makeText(MainActivity.this, "停止Service", Toast.LENGTH_SHORT).show(); } ServiceConnection sc; public void bt3_onclick(View v) { //通過意圖Intent //顯式意圖 Intent intent = new Intent(this,TestService1.class); if (sc == null) { sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "串連服務"); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "中斷連線服務"); } }; } //參數:意圖,串連,綁定方式 BIND_AUTO_CREATE:自動建立 bindService(intent,sc, Context.BIND_AUTO_CREATE); Toast.makeText(MainActivity.this, "綁定服務成功", Toast.LENGTH_SHORT).show(); } public void bt4_onclick(View v) { //判斷是否已經綁定,串連是否為空白 if (sc != null) { //解除綁定 unbindService(sc); sc = null; Toast.makeText(MainActivity.this, "解除綁定", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "尚未綁定", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { if (sc != null) { //解除綁定 unbindService(sc); sc = null; } super.onDestroy(); }}
TestService1.java
package com.hanqi.test2;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class TestService1 extends Service { public TestService1() { Log.e("TAG","構造Service"); } //綁定的回調方法 //必須要重寫 @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //throw new UnsupportedOperationException("Not yet implemented"); Log.e("TAG","綁定被調用"); return new Binder(); } @Override public void onCreate() { Log.e("TAG","建立Service"); super.onCreate(); } @Override public void onDestroy() { Log.e("TAG","銷毀Service"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("TAG","啟動命令"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.e("TAG","解除綁定"); return super.onUnbind(intent); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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: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.hanqi.test2.MainActivity" android:orientation="vertical"> <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:text="一般啟動服務" android:onClick="bt1_onclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一般停止服務" android:onClick="bt2_onclick"/> </LinearLayout> <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:text="綁定啟動服務" android:onClick="bt3_onclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="解除綁定停止服務" android:onClick="bt4_onclick"/> </LinearLayout></LinearLayout>
:
Android課程---關於Service的學習(後台運行)