Android中Service的一個Demo例子
Android中Service的一個Demo例子
Service元件是Android系統重要的一部分,網上看了程式碼,很簡單,但要想熟練使用還是需要Coding。
本文,主要貼程式碼,不對Service做過多講解。
程式碼是從網上找的一個例子,Copy下來發現程式碼不完全正確,稍微修改了下。
AndroidManifest.xml
ServiceMainActivity.java
LocalService.java
service.xml佈局檔案
[2015-11-14 17:39:10 - xp2p4android] ------------------------------
[2015-11-14 17:39:10 - xp2p4android] Android Launch!
[2015-11-14 17:39:10 - xp2p4android] adb is running normally.
[2015-11-14 17:39:10 - xp2p4android] Performing cn.fansunion.service.ServiceMainActivity activity launch
[2015-11-14 17:39:10 - xp2p4android] Automatic Target Mode: using device '51bf63f2'
[2015-11-14 17:39:10 - xp2p4android] Uploading xp2p4android.apk onto device '51bf63f2'
[2015-11-14 17:39:10 - xp2p4android] Installing xp2p4android.apk...
[2015-11-14 17:39:13 - xp2p4android] Success!
[2015-11-14 17:39:13 - xp2p4android] Starting activity cn.fansunion.service.ServiceMainActivity on device 51bf63f2
[2015-11-14 17:39:13 - xp2p4android] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=cn.fansunion/.service.ServiceMainActivity }
[2015-11-14 17:39:13 - xp2p4android] Attempting to connect debugger to 'cn.fansunion' on port 8600
執行效果圖
原來的程式碼,Toast對話方塊沒有展示出來。
在CSDN論壇找到一個貼子說,可能是被手機遮蔽了。
我倒是覺得更有可能呼叫的方式不對。
Service中呼叫,Toast合適麼?
public void onCreate() {
Log.e(TAG, "onCreate()");
//Toast.makeText(this, "onCreate()", Toast.LENGTH_SHORT).show();
}
最後參考網友的辦法,在UI執行緒,新建執行緒執行Toast。
private void toast(final String tip){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), tip, Toast.LENGTH_SHORT).show();
}
});
}
不錯,是在Activity中呼叫的。
這充分說明,網上程式碼再漂亮,還是得動手執行下。
程式碼的那個貼子,是2011年了,好古老啊~
參考資料
Android中Service元件詳解
http://blog.csdn.net/zuolongsnail/article/details/6427037
Android Toast顯示不出來
http://bbs.csdn.net/topics/390889540
Service元件是Android系統重要的一部分,網上看了程式碼,很簡單,但要想熟練使用還是需要Coding。
本文,主要貼程式碼,不對Service做過多講解。
程式碼是從網上找的一個例子,Copy下來發現程式碼不完全正確,稍微修改了下。
AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".service.ServiceMainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 註冊Service -->
<service android:name="LocalService">
<intent-filter>
<action android:name="cn.fansunion.service.LocalService" />
</intent-filter>
</service>
</application>
ServiceMainActivity.java
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;
}
};
}
LocalService.java
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;
}
}
}
service.xml佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/start" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="啟動Service" />
<Button android:id="@+id/stop" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="停止Service" />
<Button android:id="@+id/bind" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="繫結Service" />
<Button android:id="@+id/unbind" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="解除Service" />
</LinearLayout>
[2015-11-14 17:39:10 - xp2p4android] ------------------------------
[2015-11-14 17:39:10 - xp2p4android] Android Launch!
[2015-11-14 17:39:10 - xp2p4android] adb is running normally.
[2015-11-14 17:39:10 - xp2p4android] Performing cn.fansunion.service.ServiceMainActivity activity launch
[2015-11-14 17:39:10 - xp2p4android] Automatic Target Mode: using device '51bf63f2'
[2015-11-14 17:39:10 - xp2p4android] Uploading xp2p4android.apk onto device '51bf63f2'
[2015-11-14 17:39:10 - xp2p4android] Installing xp2p4android.apk...
[2015-11-14 17:39:13 - xp2p4android] Success!
[2015-11-14 17:39:13 - xp2p4android] Starting activity cn.fansunion.service.ServiceMainActivity on device 51bf63f2
[2015-11-14 17:39:13 - xp2p4android] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=cn.fansunion/.service.ServiceMainActivity }
[2015-11-14 17:39:13 - xp2p4android] Attempting to connect debugger to 'cn.fansunion' on port 8600
執行效果圖
原來的程式碼,Toast對話方塊沒有展示出來。
在CSDN論壇找到一個貼子說,可能是被手機遮蔽了。
我倒是覺得更有可能呼叫的方式不對。
Service中呼叫,Toast合適麼?
public void onCreate() {
Log.e(TAG, "onCreate()");
//Toast.makeText(this, "onCreate()", Toast.LENGTH_SHORT).show();
}
最後參考網友的辦法,在UI執行緒,新建執行緒執行Toast。
private void toast(final String tip){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), tip, Toast.LENGTH_SHORT).show();
}
});
}
不錯,是在Activity中呼叫的。
這充分說明,網上程式碼再漂亮,還是得動手執行下。
程式碼的那個貼子,是2011年了,好古老啊~
參考資料
Android中Service元件詳解
http://blog.csdn.net/zuolongsnail/article/details/6427037
Android Toast顯示不出來
http://bbs.csdn.net/topics/390889540
相關文章
- 一個基於Android的MVP框架DemoAndroidMVP框架
- Android中的Web ServiceAndroidWeb
- Android Service 服務(一)—— ServiceAndroid
- LoadRunner中檔案操作的一個例子
- Android中Service概述Android
- Android中Service總結Android
- 一個jquery的小例子jQuery
- .NET調PHP Web Service的典型例子PHPWeb
- 一個clean框架的demo框架
- iPhone 呼叫Web Service 例子(轉)iPhoneWeb
- 從一個例子中體會React的基本面React
- SAP Spartacus SSR 中的 shimming 實現的一個例子
- Activiti中的各個service的作用
- Android Service詳解(一)Android
- 行列轉換的一個例子
- 一個可逆加密的例子 (轉)加密
- 一個RPC的demo(good)RPCGo
- 關於Sqlite的一個demoSQLite
- 巢狀表在表定義中的使用:一個例子巢狀
- 軟體開發中的矛盾——一個簡單的例子 (轉)
- Android中Service的啟動方式及Activity與Service的通訊方式Android
- web到service簡單原理例子Web
- 一個Demo帶你理解Android介面回撥機制Android
- Java程式設計思想中關於閉包的一個例子Java程式設計
- 一個複雜的json例子JSON
- 一個小例子搞懂redux的套路Redux
- 擼一個簡單的MVVM例子MVVM
- 第一個JavaScript的例子學習JavaScript
- 繫結變數的一個例子變數
- 分散式死鎖的一個例子分散式
- android中drawable資源的解釋及例子Android
- 一個spark清洗資料的demoSpark
- 一個vue的電影資訊demoVue
- android: 碎片的demoAndroid
- 一個sql的行列轉置的例子SQL
- Android中bindService的使用及Service生命週期Android
- Android中startService的使用及Service生命週期Android
- 一個例子上手 SVG 動畫SVG動畫