Android-BroadCastReceive广播小练习


/**
 * 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxnsirius

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值