您的位置:首页 > 移动开发 > Android开发

Android Service两种启动方式

2015-12-21 20:29 465 查看
  两种启动service的方式:



  

  ok,可以看出,service的生命周期为:

  onCreate(),onStartCommand(),onBind(),onUnbind(),onDestory()

  下面我们来直接上代码验证。

  

  布局:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/bn_start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="startService" />
<Button
android:id="@+id/bn_stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="stopService" />

<Button
android:id="@+id/bn_bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="bindService" />
<Button
android:id="@+id/bn_unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="unBindService" />

</LinearLayout>


  Service类:

  

package com.kevin.service;

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 Binder binder=new Binder();
@Override
public IBinder onBind(Intent intent)
{
Log.d("", "onBind--");
return binder;
}

@Override
public void onCreate()
{
super.onCreate();
Log.d("", "onCreate--");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("", "onStartCommand--");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy()
{
super.onDestroy();
Log.d("", "onDestroy--");
}

@Override
public boolean onUnbind(Intent intent)
{
Log.d("", "onUnbind--");
return super.onUnbind(intent);
}

}


Activity类:

package com.kevin.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.kevin.service.MyService;

public class MainActivity extends Activity implements OnClickListener
{

private Button bnStart, bnBind, bnStop, bnUnBind;

private Binder binder;
private ServiceConnection conn = new ServiceConnection()
{

@Override
public void onServiceDisconnected(ComponentName name)// 这个方法一般不会被调用,只有Service被破坏了或者被杀死的时候才会被调用
{
binder = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
binder = (Binder) service;
}
};
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}

private void initViews()
{
bnStart = (Button) findViewById(R.id.bn_start_service);
bnStop = (Button) findViewById(R.id.bn_stop_service);
bnBind = (Button) findViewById(R.id.bn_bind_service);
bnUnBind = (Button) findViewById(R.id.bn_unbind_service);
bnStart.setOnClickListener(this);
bnBind.setOnClickListener(this);
bnStop.setOnClickListener(this);
bnUnBind.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
intent = new Intent(this, MyService.class);
if (v == bnStart)
{
startService(intent);// 正常启动
} else if (v == bnBind)
{
bindService(intent, conn, BIND_AUTO_CREATE);// 绑定启动
} else if (v == bnStop)
{
stopService(intent);// 正常结束
} else if (v == bnUnBind)
{
unbindService(conn);// 解除绑定
}
}
}


注册service:

<service android:name="com.kevin.service.MyService" >
</service>


好了,看效果吧。



先看下正常启动service。

点击startService按钮:



点击stopService按钮:



再看下绑定启动service:

点击bindService按钮:



点击unBindService按钮:



如果既点击了startService按钮,又点击了bindService按钮(不论点击顺序),需要点击stopService和unBindService两个按钮才能结束服务。

源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: