【声明】此文转载自:http://liangruijun.blog.51cto.com/3061169/647804/
——尊重作者,知识无价,交流无限!
package com.android.service.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private Button startBtn;
private Button stopBtn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button) findViewById(R.id.startBtn);
stopBtn = (Button) findViewById(R.id.stopBtn);
//添加监听器
startBtn.setOnClickListener(listener);
stopBtn.setOnClickListener(listener);
}
//启动监听器
private OnClickListener listener=new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this, ServiceDemo.class);
switch (v.getId())
{
case R.id.startBtn:
startService(intent);
break;
case R.id.stopBtn:
stopService(intent);
break;
default:
break;
}
}
};
}
ServiceDemo.java
package com.android.service.activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ServiceDemo extends Service
{
private static final String TAG="Test";
@Override
//Service时被调用
public void onCreate()
{
Log.i(TAG, "Service onCreate--->");
super.onCreate();
}
@Override
//当调用者使用startService()方法启动Service时,该方法被调用
public void onStart(Intent intent, int startId)
{
Log.i(TAG, "Service onStart--->");
super.onStart(intent, startId);
}
@Override
//当Service不在使用时调用
public void onDestroy()
{
Log.i(TAG, "Service onDestroy--->");
super.onDestroy();
}
@Override
//当使用startService()方法启动Service时,方法体内只需写return null
public IBinder onBind(Intent intent)
{
return null;
}
}
package com.android.bindservice.activity;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
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;
public class MainActivity extends Activity {
// 声明Button
private Button startBtn,stopBtn,bindBtn,unbindBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置当前布局视图
setContentView(R.layout.main);
// 实例化Button
startBtn = (Button)findViewById(R.id.startBtn1);
stopBtn = (Button)findViewById(R.id.stopBtn2);
bindBtn = (Button)findViewById(R.id.bindBtn3);
unbindBtn = (Button)findViewById(R.id.unbindBtn4);
// 添加监听器
startBtn.setOnClickListener(startListener);
stopBtn.setOnClickListener(stopListener);
bindBtn.setOnClickListener(bindListener);
unbindBtn.setOnClickListener(unBindListener);
}
// 启动Service监听器
private OnClickListener startListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent
Intent intent = new Intent();
// 设置Class属性
intent.setClass(MainActivity.this, BindService.class);
// 启动该Service
startService(intent);
}
};
// 停止Service监听器
private OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent
Intent intent = new Intent();
// 设置Class属性
intent.setClass(MainActivity.this, BindService.class);
// 启动该Service
stopService(intent);
}
};
// 连接对象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("Service", "连接成功!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("Service", "断开连接!");
}
};
// 綁定Service监听器
private OnClickListener bindListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent
Intent intent = new Intent();
// 设置Class属性
intent.setClass(MainActivity.this, BindService.class);
// 绑定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
};
// 解除绑定Service监听器
private OnClickListener unBindListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent
Intent intent = new Intent();
// 设置Class属性
intent.setClass(MainActivity.this, BindService.class);
// 解除绑定Service
unbindService(conn);
}
};
}
package com.android.bindservice.activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class BindService extends Service{
private static final String TAG="Test";
//返回null
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service onBind--->");
return null;
}
// Service创建时调用
public void onCreate() {
Log.i(TAG, "Service onCreate--->");
}
// 当客户端调用startService()方法启动Service时,该方法被调用
public void onStart(Intent intent, int startId) {
Log.i(TAG, "Service onStart--->");
}
// 当Service不再使用时调用
public void onDestroy() {
Log.i(TAG, "Service onDestroy--->");
}
// 当解除绑定时调用
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Service onUnbind--->");
return super.onUnbind(intent);
}
}