关于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>

猜你喜欢

转载自18767136122.iteye.com/blog/2103901