`
liangoogle
  • 浏览: 276617 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

intentservice的应用

阅读更多

intentservice是service的一个子类,这个类单独的开启了一个线程,利用handler的机制,把发送过来的intent放到消息列队中,一个接着一个的处理intent。

 

 要使用这个类就要做到两条:

 1   public MyIntentService() {

        super("MyServicethread");

    }

2  protected void onHandleIntent(Intent intent) {

        Bundle bundle=intent .getExtras();

        int t1= bundle.getInt("t1");

        j=t1;

        switch (j) {

            case 11:

                i=j;

                break;

            case 22:

                i2=j;

                break;

 

            default:

                break;

        }

     Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>111", ""+t1);

 

 

    }

例子:

1 <service android:name="MyIntentService"></service>在配置文件中加上;

2 MyIntentService 类

package com.hao.hello;

 

import android.app.IntentService;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

//使用IntentService,两点必须实现,一个是构造函数,另一个是实现onHandleIntent

public class MyIntentService extends IntentService {

    public static int i=0;

    public static int i2=0;

    int j;

//传入父类构造方法的字符串"MyServicethread"将作为工作线程的名字

    public MyIntentService() {

        super("MyServicethread");

    }

    @Override

    protected void onHandleIntent(Intent intent) {

        Bundle bundle=intent .getExtras();

        int t1= bundle.getInt("t1");

        j=t1;

        switch (j) {

            case 11:

                i=j;

                break;

            case 22:

                i2=j;

                break;

 

            default:

                break;

        }

     Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>111", ""+t1);

 

 

    }

 

}

3 主activity的调用

 

package com.hao.hello;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.widget.TextView;

 

public class hello extends Activity {

    TextView textView,textView2;

  Handler handler=new Handler(){

      @Override

    public void handleMessage(Message msg) {

          switch (msg.what) {

            case 0:

                textView.setText("" + MyIntentService.i);

                handler.sendEmptyMessageDelayed(1, 50000);

                break;

            case 1:

                textView2.setText("" + MyIntentService.i2);

                break;

            default:

                break;

        }

 

    }

 

 

  };

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Intent intent = new Intent(this, MyIntentService.class);

        intent.putExtra("t1", 11);

        startService(intent);

        Intent intent2 = new Intent(this, MyIntentService.class);

        intent.putExtra("t1", 22);

        startService(intent);

        textView = (TextView) findViewById(R.id.textView1);

        textView2=(TextView) findViewById(R.id.textView2);

        handler.sendEmptyMessageDelayed(0, 10000);

 

    }

}


分享到:
评论

相关推荐

    IntentService简单应用

    - 日志记录:IntentService可以用于异步记录应用日志,避免影响主要业务流程。 总之,IntentService是Android开发者处理后台任务的强大工具。它的设计使得我们可以轻松地实现异步操作,同时避免了许多并发和资源...

    IntentService

    IntentService是Android系统中一种特殊的Service,它是Service的子类,设计用于在后台执行单一的任务,然后自动停止服务,不需要手动调用stopSelf()。...正确理解和使用IntentService能够提高应用的性能和用户体验。

    IntentService实现,使用代码

    - **安全性**:由于工作在单独线程,IntentService可以安全地执行长时间操作,而不会导致ANR(应用无响应)错误。 4. **使用IntentService的步骤** - 创建一个新的类,继承自IntentService,并重写onHandleIntent...

    IntentService学习Demo

    在Android应用开发中,IntentService是一个非常重要的组件,它继承自Service,并且简化了后台服务的处理流程。IntentService主要用于执行单一的任务或者一系列串行任务,而不会长时间占用主线程,提高了应用的响应...

    android 中的服务Service intentService例子

    在Android应用开发中,服务(Service)是一种用于在后台长时间运行的任务,即使用户与应用程序的交互界面已经关闭,服务仍然可以继续执行。Service是Android四大组件之一,它主要用于执行耗时的操作,如网络通信、...

    IntentService+retrofit2.0下载文件、更新APP(项目一部分demo)

    总结,这个“IntentService+retrofit2.0下载文件、更新APP”的示例展示了如何在Android应用中优雅地处理后台下载任务。通过IntentService确保了下载过程在后台进行且不会阻塞UI,而Retrofit2.0提供了简洁的API接口...

    service和Intentservice示例

    在Android应用开发中,`Service`和`IntentService`是两个关键组件,它们用于在后台执行长时间运行的任务,不依赖于用户界面。本篇将详细阐述`Service`和`IntentService`的用法以及需要注意的要点。 首先,我们来...

    IntentService写一个应用切到后台也正常运行的Service

    IntentService则提供了一种优雅的解决方案,即使应用被切换到后台,它也能正常运行,直到所有任务完成,然后自动停止自身,释放资源。 IntentService的主要特点包括: 1. 单线程:IntentService内部维护了一个工作...

    IntentService模拟上传图片

    这样既保证了UI线程不被阻塞,又遵循了Android应用的良好设计原则。 首先,创建一个继承自IntentService的类,例如`UploadImageService`。在这个类中,我们需要重写`onHandleIntent()`方法,这是IntentService处理...

    Android—IntentService

    在Android应用开发中,IntentService的主要特点和优势包括: 1. 单线程执行:IntentService内部使用了一个工作队列,保证了所有任务按照顺序逐一执行,避免了多线程并发导致的竞态条件。 2. 自动启动与停止:当...

    详解Android中IntentService的使用方法

    在Android应用开发中,IntentService是一个非常重要的组件,它继承自Service类,专门用于执行后台的单线程任务,尤其适合处理那些可能阻塞主线程的操作,如网络请求、文件下载等。IntentService的设计旨在避免主线程...

    IntentService1

    在Android应用开发中,IntentService是一个非常重要的组件,它继承自Service,并且专门设计用于执行后台的单一任务。"IntentService1"这个示例显然旨在教你如何使用IntentService来处理异步任务,避免阻塞主线程,...

    android IntentService实现原理及内部代码分享

    在Android开发中,IntentService是一个特殊的Service子类,它的设计主要目的是为了简化异步任务的执行,特别是那些一次性...通过理解IntentService的实现原理,开发者可以更好地利用它来优化应用程序的性能和用户体验。

    Android中的IntentService简介.pdf

    - **自动创建工作线程**:IntentService会自动创建一个工作线程来执行onHandleIntent()方法,避免在主线程中执行耗时操作,防止应用UI卡顿。 - **任务队列**:IntentService接收到的Intent会被放入一个消息队列中...

    Android线程,线程池,AsyncTask,HandlerThread和IntentService的用法

    以下是对Android线程、线程池、AsyncTask、HandlerThread和IntentService的详细解释。 1. **Android线程**: Android应用主要运行在主线程(UI线程)上,负责显示和交互。为了防止主线程被长时间运行的任务阻塞,...

    IntentService使用Demo

    这个"IntentService使用Demo"将帮助我们深入理解IntentService的工作原理、使用方法及其在实际开发中的应用。 1. **IntentService的基本概念** IntentService是Service的一个子类,它的主要特点是将所有来自Intent...

    深入剖析Android系统中Service和IntentService的区别

    在Android开发中,Service是用于实现应用程序在后台运行的关键组件,它可以持续运行,即使用户离开...因此,理解Service和IntentService的区别以及它们各自的适用场景,对于优化Android应用的性能和用户体验至关重要。

    IntentService 和ResultReceiver

    在Android应用开发中,IntentService和ResultReceiver是两个非常重要的组件,它们分别用于处理后台服务和应用程序间的异步通信。接下来我们将深入探讨这两个概念及其在实际开发中的应用场景。 IntentService是一个...

    Android IntentService详解及使用实例

    Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中;  Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务;  二、IntentService特征 会创建独立的worker线程来...

Global site tag (gtag.js) - Google Analytics