Service里发送广播实现Service实时向Activity传递数据案例
来源:互联网 发布:极乐净土动作数据作者 编辑:程序博客网 时间:2024/04/28 18:06
在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1并且方式广播到activity,activity里有个广播接收器,接受到数据,最后把更新后的数值在界面上实时显示。
新建一个Service类,用来实时生产数值,供界面实时显示。
package com.example.serviceandbroadcast;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;@Overridepublic void onCreate() {super.onCreate();new Thread(new Runnable() {@Overridepublic 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();}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();count = 0;threadDisable = true;Log.v("CountService", "on destroy");}}
新建一个Activity类,定义一个广播接收器MyReceiver,接收数据,并在activity里显示数据。
package com.example.serviceandbroadcast;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.widget.EditText;public class MainActivity extends Activity {private EditText editText = null;private MyReceiver receiver = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_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);}@Overrideprotected void onDestroy() {// 结束服务stopService(new Intent(MainActivity.this, CountService.class));super.onDestroy();}/** * 获取广播数据 * * @author jiqinlin * */public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();int count = bundle.getInt("count");editText.setText(count + "");}}}
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>
清单文件
<span style="color:#4b4b4b;"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.serviceandbroadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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> </span><span style="color:#ff0000;"><service android:name ="com.example.serviceandbroadcast.CountService" /></span><span style="color:#4b4b4b;"> </application></manifest></span>
0 0
- Service里发送广播实现Service实时向Activity传递数据案例
- Service实时向Activity传递数据案例
- Service实时向Activity传递数据案例
- Service实时向Activity传递数据案例
- Service实时向Activity传递数据案例,,,,BroadcastReceiver中的数据传递给activity(2中注册方式广播)
- Android bind 回调实现Service向activity传递数据
- Android bind 回调实现Service向activity传递数据
- Service与Activity实时双向传递数据
- Android中用广播从Service中向Activity发送信息
- Android中用广播从Service中向Activity发送信息
- Android中用广播从Service中向Activity发送信息
- android 在service中如何向activity传递数据
- 安卓Service向Activity传递数据,更新UI
- Android开发学习之路-回调实现Service向activity传递数据
- Android中Service发送广播给activity
- Android Activity接收Service发送的广播
- Android Activity接收Service发送的广播
- [转载]Android中用广播从Service中向Activity发送信息
- Img出现空白间隙的问题
- 深入理解RunLoop
- Android开发实践:检测App的内存占用和泄漏
- 一张图片让你了解android的事件分发机制
- 游戏开发必看
- Service里发送广播实现Service实时向Activity传递数据案例
- Java中Set巧用,去掉重复数据
- Android ui 测试课堂笔记
- Excel查找函数--Vlookup(多条件匹配)
- OC里面的集中数组遍历方法
- 《TCP/IP详解 卷1:协议》 读书笔记 第二十章 TCP的成块数据流
- dx反编译尝试--Android软件安全与逆向分析--学习日记
- github是什么
- Xcode-code snippets