Service的基本用法

Service是一个应用程序组件,没有界面,在后台运行和Activity是一个级别的。通常用来处理一些耗时较长的操作。可以使用Service更新ContentProvider,播放MP3,网络连接,发送Intent以及启动系统的通知等等。

Service不是一个单独的进程

Service不是一个线程,(一个进程有多个线程)

启动Service:

—Context.startSevice();

停止Service

—Context.stopService();

FirstService.java中的代码:

//创建了一个服务类
public class FirstService extends Service {
//重载onBind()函数,该函数在Service被绑定后调用,能够返回Service对象实例
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
//当创建一个Service对象之后,首先会调用这个函数
	 public void onCreate() {
	        super.onCreate();
	        System.out.println("Service onCreate");
	      	    }
//主要功能放在这里
	public int onStartCommand(Intent intent,int flags,int startId){
		System.out.println("flags-->"+flags);
		System.out.println("startld-->"+startId);
		System.out.println("Service onStartCommand");
		return START_NOT_STICKY;
		}
//关闭Service,释放资源。
	public void onDestroy(){
		System.out.println("Service onDestroy");
		super.onDestroy();
	}
}

MainActivity.java中的代码:

public class MainActivity extends Activity {
//创建两个按钮
	private Button startServiceButton=null;
	private Button stopServiceButton=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startServiceButton=(Button)findViewById(R.id.startService);
//添加监听
        startServiceButton.setOnClickListener(new StartServiceListener());
        stopServiceButton=(Button)findViewById(R.id.stopService);
        stopServiceButton.setOnClickListener(new StopServiceListener());

    }
//监听类
    class  StartServiceListener implements OnClickListener{
    @Override
	public void onClick(View v) {
//创建intent,并为它指定当前的应用程序上下文以及要启动的service
		Intent intent=new Intent();
		intent.setClass(MainActivity.this,FirstService.class);
		//传递一个intent对象
		startService(intent);
	}
    }
    class  StopServiceListener implements OnClickListener{
        @Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		Intent intent=new Intent();
    		intent.setClass(MainActivity.this,FirstService.class);
    		stopService(intent);
    	}
        }
  }

在AndroidManifest.xml中注册service:

<service android:name=".FirstService"></service>
时间: 2024-08-17 04:58:04

Service的基本用法的相关文章

关于Android Service的基本用法(上)(转)

相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的Android程序员如果连Service都没听说过的话,那确实也太逊了. Service作为Android四大组件之一,在每一个应用程序中都扮演着非常重要的角色.它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长 期运行的任务.必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持运行状态. 不过,虽然Service几 乎被每一个Android程序员所熟知,但并不是每个人都已经将Service的各个知识点都

服务Service的基本用法

作为 Android四大组件之一, 服务也少不了有很多非常重要的知识点,那自然要从最基本的用法开始学习了. 定义一个服务: public class MyService extends Service { /** * onBind是继承Service后唯一的一个抽象方法所以必须要重写的一个方法 */ @Override public IBinder onBind(Intent intent) { return null; } /** * 服务每次启动的时候调用 */ @Override publ

关于Android Service的基本用法(下)(转)

在上篇文章中我们知道了,Service其实是运行在主线程里的,如果直接在Service中处理一些耗时的逻辑,就会导致程序ANR. 让我们来做个实验验证一下吧,修改上一篇文章中创建的ServiceTest项目,在MyService的onCreate()方法中让线程睡眠60秒,如下所示: [java] view plaincopy public class MyService extends Service { ...... @Override public void onCreate() { su

k8s系列---Service之ExternalName用法

需求:需要两个不同的namespace之间的不同pod可以通过name的形式访问 1:创建A名称空间的pod [[email protected] lihongxing]# cat myapp.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp namespace: cjy-test spec: replicas: 1 selector: #标签选择器 matchLabels: #匹配的标签为 app: myapp re

Android中service用法

Service翻译: Class Overview A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each servi

&lt;Android基础&gt; (十)Service Part 2 基本用法

10.3 Service的基本用法 10.3.1 定义一个服务 New——>Service public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationExc

Android Service完全解析,关于服务你所需知道的一切(下) (转载)

转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在 上一篇文章中,我们学习了Android Service相关的许多重要内容,包括Service的基本用法.Service和Activity进行通信.Service的销毁方式. Service与Thread的关系.以及如何创建前台Service.以上

Android Service完全解析,关于服务你所需知道的一切(上)

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的Android程序员如果连Service都没听说过的话,那确实也太逊了.Service作为Android四大组件之一,在每一个应用程序中都扮演着非常重要的角色.它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务.必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持

Android Service完全解析,关于服务你所需知道的一切(下)

转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要内容,包括Service的基本用法.Service和Activity进行通信.Service的销毁方式.Service与Thread的关系.以及如何创建前台Service.以上所提到的这些知识点,基本上涵盖了大部分日常开发工作当中可能使用到的Service技术.不过关于Service其实还有一个更加