Examples of how to use service in the android tutorial
- 2020-05-24 06:08:11
- OfStack
Life cycle of Service (for 2.1 and above)
1. Be startService
Whether or not there is any activity bound to the Service, it runs in the background. onCreate(if required) -
>
onStart(int id, Bundle args). If startService is used multiple times, onStart will be called multiple times, but not multiple instances of Service will be created. stop only needs to be used once. The Service1 runs in the background until either stopService or its own stopSelf() or the resource runs out of the platform.
2. Be bindService
Calling the bindService binding, the connection establishes service 1 to run straight. If startService is only BindService, onCreate() is executed, and onStart(int,Bundle) is not called. In this case, when the binding is unbound, the platform can clear the Service(after the connection is destroyed, it will cause the Service to be unbound, and then it will be destroyed).
3. Enabled and bound
Similar to the life cycle of startService, onCreate onStart will be called.
4. When service is discontinued
onDestroy() is explicit when stopService. Or implicitly invoked when there is no longer a binding (no startup). stopService() does not work with bind.
Here is a simple example of an implementation, some of which needs to be accompanied by logcat observations.
AcMain.java
package jtapp.myservicesamples;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 AcMain extends Activity implements OnClickListener {
private static final String TAG = "AcMain";
private Button btnStart;
private Button btnStop;
private Button btnBind;
private Button btnExit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
private void findView() {
btnStart = (Button) findViewById(R.id.Start);
btnStop = (Button) findViewById(R.id.Stop);
btnBind = (Button) findViewById(R.id.Bind);
btnExit = (Button) findViewById(R.id.Exit);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnExit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent("jtapp.myservicesamples.myservice");
switch(v.getId()) {
case R.id.Start:
startService(intent);
Toast.makeText(this,
"myservice running " + MyService.msec/1000.0 + "s.",
Toast.LENGTH_LONG).show();
break;
case R.id.Stop:
stopService(intent);
Toast.makeText(this,
"myservice running " + MyService.msec/1000.0 + "s.",
Toast.LENGTH_LONG).show();
break;
case R.id.Bind:
bindService(intent, sc, Context.BIND_AUTO_CREATE);
break;
case R.id.Exit:
this.finish();
break;
}
}
private MyService serviceBinder;
private ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "in onServiceDisconnected");
serviceBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "in onServiceConnected");
serviceBinder = ((MyService.MyBinder)service).getService();
}
};
@Override
protected void onDestroy() {
//this.unbindService(sc);
//this.stopService(
// new Intent("jtapp.myservicesamples.myservice"));
super.onDestroy();
}
}
main.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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:text="Start MyService" android:id="@+id/Start"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Stop MyService" android:id="@+id/Stop"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Bind MyService" android:id="@+id/Bind"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Exit AcMain" android:id="@+id/Exit"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
MyService.java
package jtapp.myservicesamples;
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";
public static long msec = 0;
private boolean bThreadRunning = true;
private final IBinder binder = new MyBinder();
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
new Thread(new Runnable(){
@Override
public void run() {
while (bThreadRunning) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
Log.i(TAG, "myservice running " + (msec+=100) + "ms.");
}
}
}).start();
}
@Override
public void onDestroy() {
bThreadRunning = false;
super.onDestroy(); // Don't have to
}
}
AnndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jtapp.myservicesamples" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".AcMain" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="MyService">
<intent-filter>
<action android:name="jtapp.myservicesamples.myservice"></action>
</intent-filter>
</service>
</application>
</manifest>