public class SendDataToServer extends Service{
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
// 开始执行后台任务
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
//与activity 绑定
class MyBinder extends Binder {
public void startDownload() {
// 执行具体的下载任务
}
}
public class MainActivity extends Activity implements OnClickListener {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private MyService.MyBinder myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.startDownload();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(connection);
break;
default:
break;
}
}
}
<service android:name="com.example.servicetest.MyService" >
分享到:
相关推荐
android service 例子
android开机自动启动service例子 android开机自动启动service例子
Jersey Rest Web Service 例子源码
两个C语言的Web service例子 希望对大家学习C有帮助
android web service 例子源代码,可以作为技术收藏。
axis实现的web service例子,并做了一些封装,分为服务端和客户端两个项目,可以直接使用到自己的项目中
reporting service 研发 reporting service例子 rdl文件 使用rdl文件 一个比较成熟的产品
一个录音机程序,简单的实现了广播,service等功能
一个结合了用service获得GPS信息,并用service将gps写入sqlite的gps表里,并用service通过broadcast发送经纬度信息给activity接收,activity通过BroadcastReceiver接收到service发送过来的经纬度信息后,在activity...
一个对初学者很有帮助的例子,web service 的编写以及asp.net的调用。
Java Web Service 学习例子,里面有大量用来开发web services的Java程序示例,非常值得学习。 使用到的技术及工具如下: Apache: www.apache.org and xml.apache.org -- Ant 1.4.1 -- SOAP 2.2 -- Tomcat 3.2...
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. FEATURES Powerful router ...Route parameters with wildcards and conditions Route redirect, halt, ...
一个XFire webservices的例子,包括服务的创建和客户端的调用,另外还写了几个调用远程web服务的例子
本案例为java开发的axis的web service案例。下载后直接部署到tomcat下即可运行。大家互相学习。
android service 简单实例代码 . binder继承一个接口,实现方法。
android web service 示例源码及apk程序
一个用android应用调用web service的例子,实现的功能是:查询号码归属地。
smack.tar 是一个native server 的例子 native_service.tar 是一个apk,验证native server。 http://blog.csdn.net/xiayu98020214/article/details/8895567