最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)實(shí)例分析

 更新時(shí)間:2015年09月18日 11:58:27   作者:Ruthless  
這篇文章主要介紹了Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)的方法,實(shí)例分析了Service組件基于線程操作實(shí)現(xiàn)數(shù)值實(shí)時(shí)傳遞的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:

這里演示一個(gè)案例,需求如下:

在Service組件中創(chuàng)建一個(gè)線程,該線程用來生產(chǎn)數(shù)值,每隔1秒數(shù)值自動(dòng)加1,然后把更新后的數(shù)值在界面上實(shí)時(shí)顯示。

步驟如下:

1、新建一個(gè)android項(xiàng)目工程,取名為demo。

2、新建一個(gè)Service類,用來實(shí)時(shí)生產(chǎn)數(shù)值,供界面實(shí)時(shí)顯示。

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);
   //發(fā)送廣播
   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、新建一個(gè)Activity類,顯示數(shù)據(jù)。

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);
    //啟動(dòng)服務(wù)
    startService(new Intent(MainActivity.this, CountService.class));
 //注冊(cè)廣播接收器
 receiver=new MyReceiver();
 IntentFilter filter=new IntentFilter();
 filter.addAction("com.ljq.activity.CountService");
 MainActivity.this.registerReceiver(receiver,filter);
  }
  @Override
 protected void onDestroy() {
   //結(jié)束服務(wù)
    stopService(new Intent(MainActivity.this, CountService.class));
 super.onDestroy(); 
 }
  /**
   * 獲取廣播數(shù)據(jù)
   * 
   * @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>

效果如下:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

美姑县| 茌平县| 江源县| 青海省| 桓仁| 光泽县| 银川市| 莎车县| 渝中区| 荣成市| 上高县| 海阳市| 乌审旗| 馆陶县| 勐海县| 嵩明县| 宾阳县| 绍兴市| 石屏县| 嘉禾县| 石阡县| 泗洪县| 麻城市| 祥云县| 容城县| 乐平市| 桂平市| 海宁市| 德化县| 常州市| 化隆| 页游| 东港市| 新营市| 饶河县| 临澧县| 南充市| 锡林浩特市| 天门市| 东海县| 黄龙县|