MyService.java 的代码如下:
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private InfoBinder mBinder = new InfoBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
class InfoBinder extends Binder {
public void start() {
Log.d(TAG, "start entered");
}
public int getCurrentInfo() {
Log.d(TAG, "getCurrentInfo entered");
return 0;
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate entered");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
Notification notification = new Notification.Builder(this)
.setContentTitle("This is title")
.setContentText("This is content")
.setAutoCancel(false)
.setOngoing(true)
.setTicker("This is content")
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand entered");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy entered");
}
}
MyIntentService.java 的代码如下:
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent entered thread id is " + Thread.currentThread().getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy entered");
}
}
测试用的文件MainActivity.java的代码如下:
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.KeyEvent;
import android.view.View;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private MyService.InfoBinder infoBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
infoBinder = (MyService.InfoBinder) iBinder;
infoBinder.start();
infoBinder.getCurrentInfo();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyUp(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void startService(View view) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stopService(View view) {
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
public void bindService(View view) {
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
}
public void unbindService(View view) {
unbindService(connection);
}
public void startIntentService(View view) {
Log.d(TAG, "UI thread id is " + Thread.currentThread().getId());
Intent intent = new Intent(this, MyIntentService.class);
startService(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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_service"
android:onClick="startService"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stop_service"
android:onClick="stopService"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bind_service"
android:onClick="bindService"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unbind_service"
android:onClick="unbindService"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_intent_service"
android:onClick="startIntentService"
/>
</LinearLayout>
本示例中使用的字符串内容 string.xml 的代码如下:
<resources>
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
<string name="bind_service">Bind Service</string>
<string name="unbind_service">Unbind Service</string>
<string name="start_intent_service">Start Intent Service</string>
</resources>
另外需要在 AndroidManifest.xml 中加入两个 Service 的相关内容:
<service android:name=".MyService" />
<service android:name=".MyIntentService" />
本文参考自 《Android 第一行代码》