android教程之service使用方法示例详解

这篇文章主要介绍了android中的service使用方法示例,需要的朋友可以参考下

Service的生命周期 (适用于2.1及以上)

1. 被startService的
无论是否有任何活动绑定到该Service,都在后台运行。onCreate(若需要) -> onStart(int id, Bundle args).  多次startService,则onStart调用多次,但不会创建多个Service实例,只需要一次stop。该Service一直后台运行,直到stopService或者自己的stopSelf()或者资源不足由平台结束。

2. 被bindService的
调用bindService绑定,连接建立服务一直运行。未被startService只是BindService,则onCreate()执行,onStart(int,Bundle)不被调用;这种情况下绑定被解除,平台就可以清除该Service(连接销毁后,会导致解除,解除后就会销毁)。

3. 被启动又被绑定
类似startService的生命周期,onCreate onStart都会调用。

4. 停止服务时
stopService时显式onDestroy()。或不再有绑定(没有启动时)时隐式调用。有bind情况下stopService()不起作用。

以下是一个简单的实现例子,某些部分需要配合logcat观察。
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

复制代码 代码如下:


        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                        android:layout_height="wrap_content" android:text="@string/hello" />
       

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(); // 可以不用
        }
}

AnndroidManifest.xml

复制代码 代码如下:


        package="jtapp.myservicesamples" android:versionCode="1"
        android:versionName="1.0">
                        android:debuggable="true">
               
                       
                               
                               
                       

               

               
                       
                               
                       

               

       


以上就是android教程之service使用方法示例详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动