Service 是 Android 四大基本组件之一,是无界面的应用程序,可以长期在后台运行,在实际工作中非常重要,比如接收推送消息、在锁屏状态下侦听传感器信息

第一课时:

使用 Service本课讲解如何使用 startService 启动服务和如何使用 stopService 停止服务,以及在过程中需要注意的问题。

MyService extends Service:


package com.example.learnservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(){


@Override
public void run() {
super.run();
while(true){
System.out.println("服务正在进行...");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

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

MainActivity extends Activity :


package com.example.learnservice;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个两个Intent操作都是的一个service
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this,MyService.class));
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this,MyService.class));
}
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


第二课时: 绑定 Service
  • 06:26

    本课讲解如何使用 bindService 绑定并启动服务,如何使用 unbindService 解除绑定服务,以及 Activity 与被绑定的服务之间的关系。

  • 绑定 Service

    06:26

    本课讲解如何使用 bindService 绑定并启动服务,如何使用 unbindService 解除绑定服务,以及 Activity 与被绑定的服务之间的关系。




private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent(MainActivity.this,MyService.class);
//这个两个Intent操作都是的一个service
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this)
;

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
case R.id.button3:
//this:代表服务的连接,监听服务的
bindService(intent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(this);
break;

default:

break;
}
}
public void onSericeConnected(ComponentName name,IBinder service){

}
private void bindService(Intent intent2, MainActivity mainActivity,
int bindAutoCreate) {
// TODO Auto-generated method stub

}


private void unbindService(MainActivity mainActivity) {
// TODO Auto-generated method stub

}


private void bindService(Intent intent2) {
// TODO Auto-generated method stub

}


private void stopActivity(Intent intent2) {
// TODO Auto-generated method stub

}

第三课时:

  • Service生命周期

    本课介绍 Service 的生命周期,并 Service 的生命周期对于开发实际项目的意义。



  • 绑定 Service

    06:26

    本课讲解如何使用 bindService 绑定并启动服务,如何使用 unbindService 解除绑定服务,以及 Activity 与被绑定的服务之间的关系。

  • Service生命周期

    06:58

    本课介绍 Service 的生命周期,并 Service 的生命周期对于开发实际项目的意义。

更多相关文章

  1. ArcGIS for Android示例解析之GP服务调用-----ViewShed
  2. Android(安卓)day_11-2 (服务)
  3. Android(安卓)service跨进程调用和启动检查
  4. context、getApplicationContext()
  5. Android(安卓)binder机制(native服务篇)
  6. (20120731)android面试总结(002)
  7. Android(安卓)服务入门(电话监听)
  8. Android(安卓)服务端将位置信息发送给客户端的实现
  9. Android(安卓)webview加载本地html实现跨域访问

随机推荐

  1. C语言中sizeof和strlen的区别是什么
  2. c++清屏函数是什么
  3. c++中头文件和源文件的区别是什么
  4. c源程序中main函数的位置是什么?
  5. c语言程序从哪里开始执行
  6. C++运算符中不能重载的是哪些
  7. eof在c语言中表示什么
  8. c语言本身没有输入输出语句吗
  9. c语言中continue语句的作用是什么
  10. C语言中字符串的结束标志是什么