Service正如其名服务,我们之前了解过Activity表示的是一个页面,但是如果我们某些操作,不需要展示页面,值需要进行后台的一个操作,这时候我们可以创建一个Service,一般进行socket通信、http请求等操作。

1、我们创建一个Android的工程
2、增加一个Service,代码如下

package com.example.learnservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        new Thread() {            @Override            public void run() {                super.run();                //while (true) {                    System.out.println("后台运行...");                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                //}            }        }.start();        return super.onStartCommand(intent, flags, startId);    }}

继承Service并且重写onStartCommand,此方法在启用此Service的时候执行。

3、我们在空的activity页面增加2个按钮,分别为启动和停止Service的操作,代码如下

package com.example.learnservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {    private Intent intent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        intent = new Intent(MainActivity.this, MyService.class);        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                startService(intent);            }        });        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("关闭后台服务....");                stopService(intent);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

通过startService和stopService操作。

4、我们看下Android的后台运行进程,如图
Android -- Service的使用_第1张图片

5、我们先点击启动Service按钮,再查看后台进程
Android -- Service的使用_第2张图片
看到了吗,这个是我启动的Service,它在后台被启动起来啦 。

6、我们调用stopService方法停止Service,如图
Android -- Service的使用_第3张图片

7、补充配置文件,需要增加service的配置在AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.learnservice" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal">    <uses-sdk  android:minSdkVersion="8" android:targetSdkVersion="21" />    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >        <activity  android:name=".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" android:enabled="true" android:exported="true">        </service>    </application></manifest>

更多相关文章

  1. Android中的Intent在两个页面传值问题
  2. Android 屏幕操作原理
  3. H5案例分享:使用JS判断客户端、浏览器、操作系统类型
  4. iphone风格设置页面PreferenceActivity
  5. Android Studio 初步在代码中操作控件
  6. Android:view常用属性和操作方法
  7. 如何检查Android后台服务线程(Service类)是否正在运行
  8. android 数据库操作初试
  9. 如何保持service长期在后台运行

随机推荐

  1. Android入门第十六篇之Style与Theme[转]
  2. “分裂”的 Android 再引争议
  3. Android(安卓)系统应用调用,intent的使用
  4. Android平台上的11个感应器你都知道吗
  5. Android中几种图像特效处理
  6. GalHttprequest类库简介——android平台
  7. android 百度sdk之 百度定位
  8. Android 应用程序之间数据共享 - Content
  9. android中的Notification使用
  10. connectbot 1.8.2 下载from github