.Net程序员玩转Android开发---(19)Android IntentService服务
来源:互联网 发布:java ee 新闻策划案例 编辑:程序博客网 时间:2024/06/05 20:35
Intentservice服务也是安卓中的一个服务,它继承与service,但与servcie有所不同,它新开启一个线程来处理所有的请求, 不需要再UI等待处理,onStartCommand方法把所有请求发送到工作队列中,,然后再由工作队列发送到onHandlerIntent中进行处理
1.interservice默认情况下是新开启一个线程来处理任务, service默认是在ui线程执行
2. interservice会创建一个工作队列来处理接受到任务,只用当前任务处理完成,才能处理下一个任务,否则下一个任务一直处于等待状态.
3.所有的请求任务处理完成后,系统会自动停止服务,不需要手动停止stopselft
下面的例子展示intentservice不断接受外部传了的消息,并按顺序处理,首先在onStartCommand里面接受消息存入队列,然后在onHandleIntent进行处理
1.创建intentservice服务
package com.example.helloword;import android.app.IntentService;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.widget.Toast;public class ReceiveIntentService extends IntentService {String receivemsg;private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { Toast.makeText(ReceiveIntentService.this, " 接受消息:" + receivemsg, Toast.LENGTH_LONG).show(); } }; public ReceiveIntentService() {super("1111");// TODO Auto-generated constructor stub}public ReceiveIntentService(String name) {super(name);// TODO Auto-generated constructor stub}@Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle bundle = intent.getExtras(); receivemsg= bundle.getString("para"); return super.onStartCommand(intent, flags, startId); } @Overrideprotected void onHandleIntent(Intent intent) {// TODO Auto-generated method stub try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} handler.sendEmptyMessage(0); }}
2.通过activity不断向intentservice发送消息
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/etpostmsg" android:layout_width="275dp" android:layout_height="wrap_content" > </EditText> <Button android:id="@+id/btnpostmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送请求" /></LinearLayout>
后台IntentActivity
package com.example.helloword;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class IntentActivity extends Activity {private Button btnpost;//启动服务private EditText etmsg;Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.intentlayout);btnpost=(Button)findViewById(R.id.btnpostmsg);etmsg=(EditText)findViewById(R.id.etpostmsg);btnpost.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub intent = new Intent("com.example.helloword.ReceiveIntentService"); Bundle bundle = new Bundle(); bundle.putString("para",etmsg.getText().toString()); intent.putExtras(bundle); startService(intent); }});}}
AndroidManifest.xml添加配置
<activity android:name="com.example.helloword.IntentActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ReceiveIntentService"> <intent-filter> <action android:name="com.example.helloword.ReceiveIntentService" /> <category android:name="android.intent.category.default" /> </intent-filter> </service>
0 0
- .Net程序员玩转Android开发---(19)Android IntentService服务
- .Net程序员玩转Android开发---(18)Android服务
- .Net程序员玩转Android开发---(20)Android绑定服务
- .Net程序员玩转Android开发---(1)环境搭建
- .Net程序员玩转Android开发---(3)登陆页面布局
- .Net程序员玩转Android开发---(4)注册页面布局
- .Net程序员玩转Android开发---(5)项目文件介绍
- .Net程序员玩转Android开发---(6)线性布局LinearLayout
- .Net程序员玩转Android开发---(7)相对布局RelativeLayout
- .Net程序员玩转Android开发---(8)表格布局TableLayout
- .Net程序员玩转Android开发---(9)绝对布局AbsoluteLayout
- .Net程序员玩转Android开发---(10)帧布局FrameLayout
- .Net程序员玩转Android开发---(11)页面跳转
- .Net程序员玩转Android开发---(12)ListView显示数据
- .Net程序员玩转Android开发---(1)环境搭建
- .Net程序员玩转Android开发---(3)登陆页面布局
- .Net程序员玩转Android开发---(4)注册页面布局
- .Net程序员玩转Android开发---(5)项目文件介绍
- 测试点分析要点
- JS中innerHTML、outerHTML、innerText、outerText的用法与区别
- 删数问题
- 合泰云天郭庆:云清洗三打DDoS
- uboot下载地址
- .Net程序员玩转Android开发---(19)Android IntentService服务
- ViewPager+FragmentTabhost 解决办法
- MyEclipse10.0安装配置pydev插件
- 删除指定轮廓
- nginx http 跳转到https
- linux之cut用法
- 0104 Ant使用【基础】
- iOS 自定义控件 progressView(环形进度条)
- git push提交时报错,拒绝提交