編輯:關於Android編程
package cn.fansunion.service; import cn.fansunion.R; import android.app.Activity; import android.app.Service; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ServiceMainActivity extends Activity { private Button startBtn; private Button stopBtn; private Button bindBtn; private Button unBindBtn; private static final String TAG = "MainActivity"; private LocalService myService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.service); startBtn = (Button) findViewById(R.id.start); stopBtn = (Button) findViewById(R.id.stop); bindBtn = (Button) findViewById(R.id.bind); unBindBtn = (Button) findViewById(R.id.unbind); startBtn.setOnClickListener(new MyOnClickListener()); stopBtn.setOnClickListener(new MyOnClickListener()); bindBtn.setOnClickListener(new MyOnClickListener()); unBindBtn.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(ServiceMainActivity.this, LocalService.class); switch (v.getId()) { case R.id.start: // 啟動Service startService(intent); toast("startService"); break; case R.id.stop: // 停止Service stopService(intent); toast("stopService"); break; case R.id.bind: // 綁定Service bindService(intent, conn, Service.BIND_AUTO_CREATE); toast("bindService"); break; case R.id.unbind: // 解除Service unbindService(conn); toast("unbindService"); break; } } } private void toast(final String tip){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), tip, Toast.LENGTH_SHORT).show(); } }); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, "連接成功"); // 當Service連接建立成功後,提供給客戶端與Service交互的對象(根據Android Doc翻譯的,不知道准確否。。。。) myService = ((LocalService.LocalBinder) service).getService(); } @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "斷開連接"); myService = null; } }; }
package cn.fansunion.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class LocalService extends Service { private static final String TAG = "MyService"; private final IBinder myBinder = new LocalBinder(); @Override public IBinder onBind(Intent intent) { Log.e(TAG, "onBind()"); //Toast.makeText(this, "onBind()", Toast.LENGTH_SHORT).show(); return myBinder; } // 調用startService方法或者bindService方法時創建Service時(當前Service未創建)調用該方法 @Override public void onCreate() { Log.e(TAG, "onCreate()"); //Toast.makeText(this, "onCreate()", Toast.LENGTH_SHORT).show(); } // 調用startService方法啟動Service時調用該方法 @Override public void onStart(Intent intent, int startId) { Log.e(TAG, "onStart()"); //Toast.makeText(this, "onStart()", Toast.LENGTH_SHORT).show(); } // Service創建並啟動後在調用stopService方法或unbindService方法時調用該方法 @Override public void onDestroy() { Log.e(TAG, "onDestroy()"); //Toast.makeText(this, "onDestroy()", Toast.LENGTH_SHORT).show(); } //提供給客戶端訪問 public class LocalBinder extends Binder { LocalService getService() { return LocalService.this; } } }
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
在上一篇Android RecylerView入門教程中提到,RecyclerView不再負責Item視圖的布局及顯示,所以RecyclerView也沒有為Item開放O
Android之Http通信——1.初識Http協議Android之Http通信1初識Http協議 引言 正文 Http是什麼鬼 名詞解析 Htt
為延長設備的電池壽命、降低內存使用率以及提升應用性能,Android N 對系統行為做出了一些變更。這些變更可能會影響系統資源和系統通知對應用的可用性。您應仔細檢查這些變
Android使用Handler進行實例化(new)時, 如: private Handler handler = new Handler(); 會報錯Ha