android 广播更新界面
2015-12-01 15:18
393 查看
转载请注明 http://blog.csdn.net/u012184853/article/details/50129897
android 更新界面的方式一般包括,接口回调,servcie加广播,handler, 现在先介绍一下广播加service来更新界面
service
然后在需要的地方注册广播和开启服务,在广播中获取服务中发送来的广播内容, 在广播里面更新界面
源码下载地址
http://download.csdn.net/detail/u012184853/9315919
android 更新界面的方式一般包括,接口回调,servcie加广播,handler, 现在先介绍一下广播加service来更新界面
service
import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class ServiceUpdateUI extends Service { private Timer timer; private TimerTask task; private int count; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); final Intent intent = new Intent(); intent.setAction(MainActivity.ACTION_UPDATEUI); timer = new Timer(); task = new TimerTask() { @Override public void run() { intent.putExtra("count", ++count); sendBroadcast(intent); } }; timer.schedule(task, 1000, 1000); } @Override public void onDestroy() { super.onDestroy(); timer.cancel(); } }
然后在需要的地方注册广播和开启服务,在广播中获取服务中发送来的广播内容, 在广播里面更新界面
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.TextView; public class MainActivity extends Activity { private TextView textView; public static final String ACTION_UPDATEUI = "action.updateUI"; UpdateUIBroadcastReceiver broadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.service_update_ui); textView = (TextView) findViewById(R.id.count_txt); // 动态注册广播 IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_UPDATEUI); broadcastReceiver = new UpdateUIBroadcastReceiver(); registerReceiver(broadcastReceiver, filter); // 启动服务 Intent intent = new Intent(this, ServiceUpdateUI.class); startService(intent); } /** * 定义广播接收器(内部类) */ private class UpdateUIBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { textView.setText(String.valueOf(intent.getExtras().getInt("count"))); } } @Override protected void onDestroy() { super.onDestroy(); // 注销广播 unregisterReceiver(broadcastReceiver); // 消除服务 Intent intent = new Intent(this, ServiceUpdateUI.class); stopService(intent); } }
源码下载地址
http://download.csdn.net/detail/u012184853/9315919
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories