編輯:關於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 ViewFlipper使用示例(注冊引導頁) 其實ViewFlipper工作機制很簡單,如上圖,就是將添加到ViewFlipper中的子View按照順序定
[android] 天氣app布局練習,androidapp主要練習一下RelativeLayout和LinearLayout <RelativeLayout
Android開發自學筆記(Android Studio)—4.4 AdapterView及其子類,androidadapterview一、引言
android通訊錄導航欄源碼(一),android導航欄 通訊錄導航欄源碼: 1.activity 1 package com.anna.contac