`

关于Android的Service

 
阅读更多
说起来真是羞愧,以前手机经常开机的时候,不会有任何QQ消息通知 ,但是我打开QQ,然后关掉以后,每隔一段时间,就会QQ提示一下消息,搞不明白是什么原理。直到昨天才明白原来是QQ的服务没有真正关掉。

查看手机service的方法:设置:应用:服务

Service是Android中重要的四大组件之一, 但是一直不怎么了解,昨天研究了一下,写了个小例子。

先介绍一下:Service跟Activity不同,基本不会出现在界面上跟大家进行交互,都是通过后台运行,最大的好处就是如果Activity运行中onDestroy了, service可以不受影响。
start启动的service,当前activity销毁,service不会停止
bind 启动的service,当前activity销毁,service会停止

下面上带代码:


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener,ServiceConnection{

	Intent intent;
	private MyService myservice = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById(R.id.but1).setOnClickListener(this);
		findViewById(R.id.but2).setOnClickListener(this);
		findViewById(R.id.but3).setOnClickListener(this);
		findViewById(R.id.but4).setOnClickListener(this);
		findViewById(R.id.but5).setOnClickListener(this);
		intent  = new Intent(this,MyService.class);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.but1:
			System.out.println("===========");
			startService(intent);
			break;
		case R.id.but2:
			stopService(intent);
			break;
		case R.id.but3:
			bindService(intent, this, Context.BIND_AUTO_CREATE);
			break;
		case R.id.but4:
			unbindService(this);
			myservice = null;
			break;
		case R.id.but5:
			if(myservice!=null){
				System.out.println(myservice.getcurrentnum());
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onServiceConnected(ComponentName arg0, IBinder arg1) {
		System.out.println("Service onServiceConnected");
		myservice = ((MyService.MyIBinder) arg1).getService();
	}

	@Override
	public void onServiceDisconnected(ComponentName arg0) {
		System.out.println("Service onServiceDisconnected");
	}

}


import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service{

	private MyIBinder myibinder = new MyIBinder() ;
	private Timer timer = null;
	private TimerTask timertask = null;
	private int i = 0;
	
	
	public class MyIBinder extends Binder{
		public MyService getService(){
			return MyService.this;
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		System.out.println("Service onBind");
		return myibinder;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("Service onCreate");
		startTimer();
	}
	
	private void startTimer() {
		if(timer == null){
			timer = new Timer();
			timertask = new TimerTask() {
				
				@Override
				public void run() {
					i++;
					System.out.println(i);
				}
				
			};
			timer.schedule(timertask, 1000, 1000);
		}
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("Service onDestroy");
		stopTimer();
	}

	private void stopTimer() {
		if(timer !=null){
			timer.cancel();
			timertask.cancel();
			timer=null;
			timertask=null;
		}
	}
	
	public int getcurrentnum(){
		return i;
	}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/but1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启用service" />

    <Button
        android:id="@+id/but2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="听用service" />

    <Button
        android:id="@+id/but3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定service" />

    <Button
        android:id="@+id/but4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="解除绑定service" />

    <Button
        android:id="@+id/but5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="得到当前数字" />

</LinearLayout>


<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.l302service.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="MyService"></service>
    </application>
分享到:
评论

相关推荐

    android service 简单实例源代码

    在Android开发中,Service是四大组件之一,它在后台运行,不与用户界面直接交互,常用于执行长时间的任务,如播放音乐、网络通信等。本篇文章将深入解析"android service 简单实例源代码",帮助你理解如何在Android...

    Android_Service.rar

    这个名为"Android_Service.rar"的压缩包包含了一个关于Android Service的示例项目,可以帮助我们深入理解如何创建和使用Service,以及它与Activity之间的交互。 首先,让我们了解什么是Android Service。Service是...

    android--service实例

    本教程的“android-tutorial”可能包含了一系列关于Android Service的实例代码和详细解释,涵盖从基础到进阶的各种应用场景,对于理解和掌握Service的使用非常有帮助。通过学习和实践这些示例,开发者能够更好地应对...

    android service toast 01

    在Android开发中,Service是应用组件之一,它可以在后台长时间运行,即使用户界面不在活动状态。Service主要用于执行长时间运行的任务,如音乐播放、网络通信等。而`Toast`则是一种轻量级的通知方式,用于显示短暂的...

    Android service讲解文档ppt

    在Android应用开发中,Service是四大组件之一,它在后台执行长时间运行的操作,不与用户界面直接交互。本讲解文档将深入探讨Local Service和Remote Service的实现与使用,以及广播接收器的重要作用。 首先,我们来...

    Android-Service与Activity传值

    在Android应用开发中,`Service`和`Activity`是两个重要的组件。`Service`用于在后台执行长时间运行的任务,而`Activity`则负责用户界面交互。在某些场景下,我们可能需要在`Service`和`Activity`之间传递数据,比如...

    android service的小实例

    在Android应用开发中,Service是四大组件之一,用于在后台执行长时间运行的操作,即使用户界面不在活动状态。本文将通过四个小实例详细介绍Android Service的四种启动方式:启动方式、绑定方式、线程方式以及AIDL...

    Android Service简单实例

    在Android应用开发中,Service是四大组件之一,用于在后台执行长时间运行的操作,即使用户界面关闭也能继续工作。本篇文章将深入探讨`startService`类型的Android Service,通过一个简单的实例来展示其工作原理和...

    Android Service下载,EventBus更新界面

    在Android应用开发中,Service和EventBus是两个重要的组件,它们在实现后台任务处理和界面交互方面发挥着关键作用。Service用于在后台长时间运行任务,而EventBus则是一种优秀的事件总线框架,使得组件间通信更为...

    Android Service深入解析Demo

    在Android应用开发中,Service是四大组件之一,它在后台长时间运行,不依赖于任何用户界面,用于执行长时间运行的任务,如播放音乐、网络通信等。这篇博客"Android Service深入解析Demo"通过实例深入讲解了Service的...

    Android service

    在提供的文件名"ServiceActPrj"中,我们可以推测这是一个关于Service和Activity交互的项目实例。该项目可能包含了Service的实现,Activity的UI更新逻辑,以及BroadcastReceiver的使用代码。通过查看和分析这个项目,...

    android service下载资源,同时解压资源

    在Android开发中,Service是一种非常重要的组件,它可以在后台长时间运行,执行一些不需要与用户交互的任务。本示例中,我们关注的是如何利用Service来实现资源的异步下载,并且在下载完成后对ZIP文件进行解压。这个...

    Android Service 与 定时器

    在Android应用开发中,`Service`和定时器是两个重要的组件,它们被广泛用于实现后台任务和周期性操作。本文将深入探讨`Android Service`和定时器的基本概念、使用方法以及如何结合它们来实现每3秒打印一次日志的功能...

    Android Service Demo

    "Android Service Demo"是一个示例项目,它展示了如何在Android应用中使用Service,尤其是结合AIDL(Android Interface Definition Language)来实现进程间通信(IPC,Inter-Process Communication)。 首先,我们...

    android service使用小demo

    android service使用的小demo 包括startService stopService bindService unbindService 两种开启、关闭service的小demo

    Android防止service多次执行startCommand

    在Android应用开发中,Service是四大组件之一,用于在后台长时间运行操作,比如播放音乐、网络通信等。然而,如果不加以控制,用户或者系统可能会多次启动同一个Service,导致不必要的资源消耗和服务的异常行为。本...

    Android-AndroidService下载文件

    在Android应用开发中,`Service` 是一个非常重要的组件,它允许应用程序在后台长时间运行操作,即使用户已经离开或关闭了应用界面。本教程将详细讲解如何利用Android的`Service` 组件来实现文件下载功能。 一、...

    android Service类简介

    Service是Android系统中的一个重要组件,它是应用程序框架的一部分,允许开发者在后台执行长时间运行的操作而无需与用户交互。这篇博客文章将深入介绍Android Service类的基本概念、功能、生命周期以及如何在实际...

    通过Messenger实现Android Service更新UI

    在Android应用开发中,Service是一种在后台运行的组件,它不具有用户界面,但可以执行长时间的任务或与其他组件进行通信。在某些情况下,我们可能需要一个Service去执行一些任务,并在任务完成后更新UI。这时,我们...

Global site tag (gtag.js) - Google Analytics