关于Android的Service

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

查看手机service的方法:[color=orange]设置:应用:服务[/color]

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

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

下面上带代码:[/size]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值