IntentService

来源:互联网 发布:大数据 饲料产量 编辑:程序博客网 时间:2024/05/22 06:51


IntentService具有如下特征:
IntentService会创建单独的worker线程来处理所有的intent请求
IntentService会创建单独的worker线程来处理onHandleIntent()方法实现代码
所有请求完成后,IntentService会自动停止,无需调用stopservice()方法来停止该服务
IntentService也提供了OnstartCommand()和onBind()方法的默认实现。

也就是说,继承IntentService,无需重写onBind()和onStartCommand()方法,只需重写onHandleIntent方法即可。

package com.example.intentserviceuse;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service{@Overridepublic IBinder onBind(Intent intent) {try {Thread.sleep(20*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}Log.d("lhm", "MyService唤醒了");return null;}}
package com.example.intentserviceuse;import android.app.IntentService;import android.content.Intent;import android.util.Log;public class MyIntentService extends IntentService{public MyIntentService() {super("MyIntentService");// TODO Auto-generated constructor stub}@Overrideprotected void onHandleIntent(Intent intent) {try {Thread.sleep(20*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}Log.d("lhm", "MyIntentService唤醒了");}}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.intentserviceuse"    android:versionCode="1"    android:versionName="1.0" >    <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="com.example.intentserviceuse.MyIntentService">        </service>        <service android:name="com.example.intentserviceuse.MyService">            <intent-filter >                <action android:name="com.lhm.MyService"/>            </intent-filter>        </service>    </application></manifest>

activity_main.xml

<pre class="html" name="code"><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.timerservice.MainActivity" >    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="普通" />    <Button        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="IntentService" /></LinearLayout>

package com.example.intentserviceuse;import android.app.Activity;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private Button btn1, btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button) findViewById(R.id.btn1);btn2 = (Button) findViewById(R.id.btn2);btn1.setOnClickListener(this);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn1:Intent intent = new Intent("com.lhm.MyService");MainActivity.this.bindService(intent, null, Service.BIND_AUTO_CREATE);break;case R.id.btn2:startService(new Intent(MainActivity.this, MyIntentService.class));break;default:break;}}}

IntentService在唤醒之前退出activity会报错。Service会出现界面没有更新。




0 0
原创粉丝点击