Android-BroadCastReceive广播小练习
来源:互联网 发布:微博登陆不了网络异常 编辑:程序博客网 时间:2024/05/16 07:23
/** * Created by zxn on 7/16/2016. * 说明:开启一个Service服务,在服务中开启一个线程并定义一个全局变量i和j,每2秒钟i+1,j*2并分别更新Activity中的两个TextView */public class Activity_BroadCast_Work extends Activity{ TextView textViewi; TextView textViewj; IntentFilter intentFilter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_broadcast_work); //intent过滤器,设置action intentFilter = new IntentFilter(); intentFilter.addAction("signel"); textViewi = (TextView)findViewById(R.id.texti); textViewj = (TextView)findViewById(R.id.textj); Intent intent = new Intent(); intent.setClass(Activity_BroadCast_Work.this, Service_BroadCast_Work.class); startService(intent); } @Override protected void onStart() { //注册广播接收器 registerReceiver(printbroadcast,intentFilter); super.onStart(); } @Override protected void onDestroy() { super.onDestroy(); //销毁回收广播接收器对象 unregisterReceiver(printbroadcast); } //广播接收器 BroadcastReceiver printbroadcast = new BroadcastReceiver() { static final String name = "signel"; @Override public void onReceive(Context context, Intent intent) { //根据service服务广播的内容更新textview if (name.equals(intent.getAction())) { textViewi.setText(intent.getStringExtra("i")); textViewj.setText(intent.getStringExtra("j")); } } };}
/** * Created by zxn on 7/16/2016. * 说明: */public class Service_BroadCast_Work extends Service{ @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(final Intent intent, int flags, int startId) { final Runnable newThread = new Runnable() { int i = 0; int j = 1; @Override public void run() { while(true) { i++; j *= 2; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Intent broadcaseintent = new Intent(); broadcaseintent.putExtra("i",i+""); broadcaseintent.putExtra("j",j+""); broadcaseintent.setAction("name_a"); sendBroadcast(broadcaseintent); } } }; Thread newThreadd = new Thread(newThread); newThreadd.start(); return super.onStartCommand(intent, flags, startId); }}
//activity_broadcast_work.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Service—Update—I:"/> <TextView android:id="@+id/texti" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更新i"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Service—Update—J:"/> <TextView android:id="@+id/textj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更新i"/> </LinearLayout></LinearLayout>
0 0
- Android-BroadCastReceive广播小练习
- Android低阶之【chapter-8】BroadCastReceive 广播
- BroadcastReceive广播接收器:
- 广播接收者BroadcastReceive
- 黑马程序员-Android四大组建之BroadcastReceive广播接收者
- BroadcastReceive 系统广播 短信广播
- Android BroadCastReceive
- Android四大基本组件(1)之Activity与BroadcastReceive广播接收器
- 细说Android BroadcastReceive
- 【Android基础知识】BroadcastReceive
- android 四大组件BroadcastReceive
- Android简单的BroadcastReceive
- Android 广播接收者练习-发送有序广播
- 关于Android开发中的BroadCastReceive
- android广播经典小例子
- 小白 android广播 小结
- 【安卓篇】四大组件●BroadcastReceive广播接收器介绍及生命周期
- 安卓有关广播接收者(BroadCastReceive)的知识
- Activity的生命周期
- 第一节 Python基础
- JDBC之读取数据库
- form属性
- UVALive 5963 Confusion in the Problem Set 思维题、Interesting
- Android-BroadCastReceive广播小练习
- 1 数据校验
- 符号的魅力
- JDBC之CLOB与BLOB
- pat 1104. Sum of Number Segments
- malloc函数进行内存分配是在什么阶段?
- 一个简单的生产者与消费者的多线程例子(二):不让消费者或生产者无限期地等待
- 冷却算法综合排名改进模型
- Spring Boot异常:BeanCreationException:Injection of autowired dependencies failed;