国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中Service實時向Activity傳遞數據實例分析

2020-04-11 11:25:58
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彝良县| 梁平县| 宣汉县| 资溪县| 利辛县| 民权县| 镶黄旗| 图木舒克市| 邵武市| 汨罗市| 枞阳县| 连平县| 延津县| 多伦县| 普格县| 六盘水市| 当雄县| 六安市| 恭城| 台江县| 石楼县| 桂阳县| 南投县| 黎川县| 宿州市| 五寨县| 赣州市| 淅川县| 梅河口市| 颍上县| 浙江省| 宣城市| 万全县| 洛扎县| 梓潼县| 香河县| 都兰县| 成武县| 万山特区| 渝北区| 米易县|