Android中Service實時向Activity傳遞數(shù)據(jù)實例分析
本文實例講述了Android中Service實時向Activity傳遞數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:
這里演示一個案例,需求如下:
在Service組件中創(chuàng)建一個線程,該線程用來生產(chǎn)數(shù)值,每隔1秒數(shù)值自動加1,然后把更新后的數(shù)值在界面上實時顯示。
步驟如下:
1、新建一個android項目工程,取名為demo。
2、新建一個Service類,用來實時生產(chǎn)數(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、新建一個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);
//啟動服務(wù)
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() {
//結(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>
效果如下:

希望本文所述對大家的Android程序設(shè)計有所幫助。
- Android檢測Activity或者Service是否運行的方法
- Android中Service和Activity相互通信示例代碼
- Android中Service與Activity之間通信的幾種方式
- Android Activity與Service通信(不同進程之間)詳解
- Android Activity 與Service進行數(shù)據(jù)交互詳解
- 淺談Android Activity與Service的交互方式
- Android使用Messenger實現(xiàn)service與activity交互
- Android實現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解
- Android實現(xiàn)從activity中停止Service的方法
- android使用service和activity獲取屏幕尺寸的方法
- 詳解Android Service與Activity之間通信的幾種方式
相關(guān)文章
Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導航欄步驟詳解
這篇文章主要介紹了Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導航欄步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02
Android onClick方法與setOnClickListener方法對比
這篇文章主要介紹了Android onClick方法與setOnClickListener方法對比的相關(guān)資料,這兩個方法都是點擊事件處理函數(shù)的方法,它們之間到底有什么區(qū)別呢,下面就給大家說下,需要的朋友可以參考下2016-12-12
Android使用SurfaceView實現(xiàn)飄贊動畫
這篇文章主要為大家詳細介紹了Android如何使用SurfaceView實現(xiàn)飄贊動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android在自定義類中實現(xiàn)自定義監(jiān)聽器方式
這篇文章主要介紹了Android在自定義類中實現(xiàn)自定義監(jiān)聽器方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

