Android 之Service

  service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作。

接下来分析一下service 的生命周期:

1:actiivty_main.xml

Android 之Service

<RelativeLayout 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">



    <Button 

        android:id="@+id/btn_bind_service"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Bind Service"/>

    

    <Button 

        android:id="@+id/btn_stop_bind"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/btn_bind_service"

        android:text="Stop Bind"/>

    

    <Button 

        android:id="@+id/btn_start_service"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/btn_stop_bind"

        android:text="Start Service"/>

    

    <Button 

        android:id="@+id/btn_stop_service"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/btn_start_service"

        android:text="Stop Service"/>

</RelativeLayout>

2:ServiceDemo.java

public class ServiceDemo extends Service{

    private static final String TAG="ServiceDemo";

    public static final String ACTION="com.yan.service.ServiceDemo";

    

    @Override

    public IBinder onBind(Intent arg0) {

        Log.i(TAG, "onBind");

        return null;

    }



    @Override

    public void onCreate() {

        Log.i(TAG,"onCreate");

        super.onCreate();

    }



    @Override

    @Deprecated

    public void onStart(Intent intent, int startId) {

        Log.i(TAG, "onStart");

        super.onStart(intent, startId);

    }



    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "onStartCommand");

        return super.onStartCommand(intent, flags, startId);

    }



    @Override

    public void onDestroy() {

        Log.i(TAG, "onDestroy");

        super.onDestroy();

    }



    @Override

    public boolean onUnbind(Intent intent) {

        Log.i(TAG, "onUnbind");

        return super.onUnbind(intent);

    }

}

3:MainActivity.java

public class MainActivity extends Activity {

    private Button btnBindService=null;

    private Button btnStopBind=null;

    private Button btnStartService=null;

    private Button btnStopService=null;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        btnBindService=(Button)findViewById(R.id.btn_bind_service);

        btnStopBind=(Button)findViewById(R.id.btn_stop_bind);

        btnStartService=(Button)findViewById(R.id.btn_start_service);

        btnStopService=(Button)findViewById(R.id.btn_stop_service);

        

        btnBindService.setOnClickListener(new OnClickListener(){

            public void onClick(View view){

                Intent intent=new Intent(ServiceDemo.ACTION);

                bindService(intent,sc,BIND_AUTO_CREATE);

            }

        });

        

        btnStopBind.setOnClickListener(new OnClickListener(){

            public void onClick(View view){

                unbindService(sc);

            }

        });

        

        btnStartService.setOnClickListener(new OnClickListener(){

            public void onClick(View view){

                Intent intent=new Intent(ServiceDemo.ACTION);

                startService(intent);

            }

        });

        

        

        btnStopService.setOnClickListener(new OnClickListener(){

            public void onClick(View view){

                Intent intent=new Intent(ServiceDemo.ACTION);

                stopService(intent);

            }

        });

    }



    ServiceConnection sc=new ServiceConnection(){

        @Override

        public void onServiceConnected(ComponentName arg0, IBinder arg1) {

            Log.i("SC", "onServiceConnected");

        }

        @Override

        public void onServiceDisconnected(ComponentName arg0) {

            Log.i("SC", "onServiceDisconnected");

        }

    };

}

4:AndroidManifest.xml

<service 

            android:name="com.yan.service.ServiceDemo">

            <intent-filter>

                <action android:name="com.yan.service.ServiceDemo"/>

            </intent-filter>

        </service>

5:运行结果分析。

(1)点击StartService:

执行 onCreate()->onStartCommand()->onStart()

(2)点击StopService:

执行 onDestroy()

(3)点击BindService

执行 onCreate()->onBind()

(4)点击StopBind

执行 onUnbind()->onDestory()

 

 

 

你可能感兴趣的:(android)