说起来真是羞愧,以前手机经常开机的时候,不会有任何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;@Overrideprotected 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);}@Overridepublic 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;}}@Overridepublic void onServiceConnected(ComponentName arg0, IBinder arg1) {System.out.println("Service onServiceConnected");myservice = ((MyService.MyIBinder) arg1).getService();}@Overridepublic 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;}}@Overridepublic IBinder onBind(Intent arg0) {System.out.println("Service onBind");return myibinder;}@Overridepublic void onCreate() {super.onCreate();System.out.println("Service onCreate");startTimer();}private void startTimer() {if(timer == null){timer = new Timer();timertask = new TimerTask() {@Overridepublic void run() {i++;System.out.println(i);}};timer.schedule(timertask, 1000, 1000);}}@Overridepublic 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>

更多相关文章

  1. 摩托罗拉android产品 MT710
  2. Android(安卓)成用户隐私安全问题严重
  3. 【新知提升】Android中的消息推送
  4. Android网络数据抓包:利用Fiddler进行网络数据抓包
  5. android 事件模型原理
  6. Linux下Android手机刷机指南
  7. 视频直播Android推流SDK初体验
  8. Android手机游戏的救世主--PSX4Droid
  9. Android之高仿微信聊天的界面

随机推荐

  1. Android 初识Retrofit
  2. TabHost两种实现方式
  3. android 命令(adb shell)进入指定模拟器
  4. android2.2资源文件详解4--menu文件夹下
  5. ListItem更改背景色
  6. android触摸实现物体运动方式
  7. 【【【常用的ubuntu第三方工具及android
  8. CSDN精选Android开发博客
  9. Android 中文 API (35) —— ImageSwitcher
  10. Android最佳性能实践(一)——合理管理内