首先看一段官方对Service的解释:
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service>
declaration in its package'sAndroidManifest.xml
. Services can be started withContext.startService()
andContext.bindService()
.
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found inProcesses and Threads. TheIntentService
class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
大概意思:
服务是一种应用程序组件,进行较长时间的运行操作。每个服务类必须有一个相应的包的AndroidManifest.xml <service>声明。服务可以开始Context.startService()和Context.bindService()。
注意,服务,像其他应用程序对象,运行在他们的主机进程的主线程。这意味着,如果你的服务去做任何CPU密集型(如MP3播放)或阻塞(如网络)的操作,它会在它自己的线程来做这项工作。关于这方面的更多信息可以在进程和线程的发现。intentservice类可作为一个标准的实施服务,有它自己的线程,调度工作要做。
file:///***/sdk/docs/reference/android/app/Service.html#ServiceLifecycle (其中*换成本地sdk的路径即可,当下载sdk是需要下载doc)
自己总结的图
小例子:
<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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnCreate"
android:text="启动服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnClose"
android:text="关闭服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnBind"
android:text="绑定服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnUnBind"
android:text="解除绑定服务" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="显示信息" />
</LinearLayout>
package com.example.testservice;
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.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
}
public void btnCreate(View v) {
Intent intent = new Intent(this, myService.class);
this.startService(intent);
}
public void btnClose(View v) {
Intent intent = new Intent(this, myService.class);
this.stopService(intent);
}
public void btnBind(View v) {
Intent intent = new Intent(this, myService.class);
this.bindService(intent, connect, BIND_AUTO_CREATE);
}
public void btnUnBind(View v) {
Intent intent = new Intent(this, myService.class);
this.unbindService(connect);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
forActivity callback = new forActivity.Stub() {
@Override
public void showText() throws RemoteException {
// Auto-generated method stub
Toast.makeText(getApplicationContext(), "aidl方法调用案例", 0).show();
Log.d("Test", "aidl调试成功!");
}
};
// forService serviceAidl;
ServiceConnection connect = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// serviceAidl = null;
// Log.d("Test", "断开连接!");
}
public void onServiceConnected(ComponentName name, IBinder service) {
// // Auto-generated method stub
// serviceAidl = forService.Stub.asInterface(service);
// try {
// serviceAidl.registerActivity(callback);
// } catch (RemoteException e) {
// e.printStackTrace();
// }
}
};
}
package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class myService extends Service {
private String TAG = "test";
private forActivity testActivity;
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "执行了 ..onBind");
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "执行了 ..onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "执行了 ..onStart");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "执行了 ..onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG, "执行了 ..onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "执行了 ..onUnbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "执行了 ..onRebind");
super.onRebind(intent);
}
// private final forService mBind = new forService.Stub() {
//
// @Override
// public void registerActivity(forActivity callBack)
// throws RemoteException {
// testActivity = callBack;
//
// }
//
// @Override
// public void invokeCall() throws RemoteException {
// // Auto-generated method stub
// testActivity.showText();
//
// }
// };
}