Android中Service实时向Activity传递数据实例分析

 更新时间:2015年09月18日 11:58:27   作者:Ruthless  
这篇文章主要介绍了Android中Service实时向Activity传递数据的方法,实例分析了Service组件基于线程操作实现数值实时传递的相关技巧,需要的朋友可以参考下

本文实例讲述了Android中Service实时向Activity传递数据的方法。分享给大家供大家参考。具体如下:

这里演示一个案例,需求如下:

在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示。

步骤如下:

1、新建一个android项目工程,取名为demo。

2、新建一个Service类,用来实时生产数值,供界面实时显示。

package com.ljq.activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class CountService extends Service {
 private int count = 0;
 private boolean threadDisable=false;
 @Override
 public void onCreate() {
 super.onCreate();
 new Thread(new Runnable() {
  @Override
  public void run() {
  while (!threadDisable) {
   try {
   Thread.sleep(1000);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   count++;
   Log.v("CountService", "Count is " + count);
   //发送广播
   Intent intent=new Intent();
   intent.putExtra("count", count);
   intent.setAction("com.ljq.activity.CountService");
   sendBroadcast(intent);
  }
  }
 }).start();
 }
 @Override
 public IBinder onBind(Intent intent) {
 return null;
 }
 @Override
 public void onDestroy() {
 super.onDestroy();
 count=0;
 threadDisable = true;
 Log.v("CountService", "on destroy");
 }
}

3、新建一个Activity类,显示数据。

package com.ljq.activity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
 private EditText editText=null;
 private MyReceiver receiver=null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText=(EditText)findViewById(R.id.editText);
    //启动服务
    startService(new Intent(MainActivity.this, CountService.class));
 //注册广播接收器
 receiver=new MyReceiver();
 IntentFilter filter=new IntentFilter();
 filter.addAction("com.ljq.activity.CountService");
 MainActivity.this.registerReceiver(receiver,filter);
  }
  @Override
 protected void onDestroy() {
   //结束服务
    stopService(new Intent(MainActivity.this, CountService.class));
 super.onDestroy(); 
 }
  /**
   * 获取广播数据
   * 
   * @author jiqinlin
   *
   */
  public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
   Bundle bundle=intent.getExtras();
   int count=bundle.getInt("count");
   editText.setText(count+"");  
   }
  }
}

4、main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
  <EditText android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cursorVisible="false"
    android:editable="false"
    android:id="@+id/editText"/>
</LinearLayout>

5、清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ljq.activity"
   android:versionCode="1"
   android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <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 =".CountService" />
  </application>
  <uses-sdk android:minSdkVersion="7" />
</manifest>

效果如下:

希望本文所述对大家的Android程序设计有所帮助。

相关文章

  • Android 自定义圆形带刻度渐变色的进度条样式实例代码

    Android 自定义圆形带刻度渐变色的进度条样式实例代码

    这篇文章主要介绍了Android 自定义圆形带刻度渐变色的进度条的实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解

    Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解

    这篇文章主要介绍了Android BottomNavigationView结合ViewPager实现底部导航栏步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • Android实现简单的城市列表功能

    Android实现简单的城市列表功能

    这篇文章主要为大家详细介绍了Android实现简单的城市列表功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Mac Android Studio安装图文教程

    Mac Android Studio安装图文教程

    本文主要介绍了Mac Android Studio安装教程,文中通过图文代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Android 蓝牙开发实例解析

    Android 蓝牙开发实例解析

    本文主要介绍Android 蓝牙开发,这里提供实例代码和详细解析实现方法,对开发Android蓝牙开发的朋友提供简单示例,有需要的朋友可以参考下
    2016-08-08
  • Android onClick方法与setOnClickListener方法对比

    Android onClick方法与setOnClickListener方法对比

    这篇文章主要介绍了Android onClick方法与setOnClickListener方法对比的相关资料,这两个方法都是点击事件处理函数的方法,它们之间到底有什么区别呢,下面就给大家说下,需要的朋友可以参考下
    2016-12-12
  • Android使用SurfaceView实现飘赞动画

    Android使用SurfaceView实现飘赞动画

    这篇文章主要为大家详细介绍了Android如何使用SurfaceView实现飘赞动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Android在自定义类中实现自定义监听器方式

    Android在自定义类中实现自定义监听器方式

    这篇文章主要介绍了Android在自定义类中实现自定义监听器方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android获取ROOT权限的实例代码

    Android获取ROOT权限的实例代码

    这篇文章主要介绍了Android如何获取ROOT权限,写了一个小方法,大家可以在应用中检测ROOT权限
    2013-11-11
  • Android列表RecyclerView排列布局

    Android列表RecyclerView排列布局

    这篇文章主要为大家详细介绍了Android列表RecyclerView排列布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07

最新评论