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
原创粉丝点击