<?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="Service测试" android:gravity="center_horizontal" /> <Button android:id="@+id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/stopButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop Service" /> <Button android:id="@+id/bindButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Bind Service" /> <Button android:id="@+id/unBindButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Unbind Service" /> </LinearLayout>
public class MyService extends Service
{
final static String TAG = "MyService";
@Override
public IBinder onBind(Intent intent)
{
Log.i(TAG,"OnBind()......");
showInfo("Onbind()......");
return null;
}
// Setvice创建时被调用
@Override
public void onCreate()
{
Log.i(TAG,"onCreate()......");
showInfo("onCreate()......");
super.onCreate();
}
//当客户调用startService()启动Service时被调用
@Override
public void onStart(Intent intent, int startId)
{
Log.i(TAG,"onStart()........");
showInfo("onStart()........");
super.onStart(intent, startId);
}
@Override
public void onDestroy()
{
Log.i(TAG,"onDestroy()........");
showInfo("onDestroy()........");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent)
{
Log.i(TAG,"onUnbind()........");
showInfo("onUnbind()........");
return super.onUnbind(intent);
}
public void showInfo(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
<!-- 增加的Service --> <service android:name=".MyService"> <intent-filter > <action android:name="com.study.android.action.MY_SERVICE"/> </intent-filter> </service>
package com.study.android;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
final static String ACTION = "com.study.android.action.MY_SERVICE";
private Button startButton,stopButton;
private Button bindButton,unbindButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button)findViewById(R.id.startButton);
stopButton = (Button)findViewById(R.id.stopButton);
bindButton = (Button)findViewById(R.id.bindButton);
unbindButton = (Button)findViewById(R.id.unBindButton);
// 开启服务
startButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 启动服务
startService(buildIntent());
}
});
//startButton按下后 周期函数执行顺序 onCreate()----->onStart()
/******************************************/
// 停止服务
stopButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
stopService(buildIntent());
}
});
//stopButton按下后 周期函数执行顺序 onDestroy()
/******************************************/
// 绑定服务
bindButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
bindService(buildIntent(),conn,Service.BIND_AUTO_CREATE);
/*第三个参数:如何创建service 一般是制定绑定时自动创建*/
}
});
// bindButton 被按下后(当前服务已经stop掉)周期函数执行顺序 onCreate()------->onBind()
/******************************************/
// 接触绑定Service
unbindButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
unbindService(conn);
}
});
//unbindButton按下后 周期函数执行顺序onUnbind()------->onDestroy()
}
// 连接对象
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
Log.i("SERVICE","连接服务成功!");
showInfo("连接服务成功!");
}
@Override
public void onServiceDisconnected(ComponentName name)
{
Log.i("SERVICE","服务连接断开!");
showInfo("服务连接断开!");
}
};
public Intent buildIntent()
{
Intent intent = new Intent();
// 设置Intent属性 注意:这里action属性要与Manifest里服务对应
intent.setAction(ACTION);
return intent;
}
public void showInfo(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有