利用BroadcastReceiver實現搶購倒計時

來源:互聯網
上載者:User

利用BroadcastReceiver實現搶購倒計時,當然你也可以用Thread+Handler來完成。實際應用中我們倒計時的時間是從服務端擷取的。

運行效果:

布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal"    android:orientation="vertical" >    <EditText        android:id="@+id/et_input"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="請輸入倒計時(秒)" >           </EditText>    <Button        android:id="@+id/bt_start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="倒計時開始"         android:onClick="begin"/>    <TextView        android:id="@+id/tv_time"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="搶購倒計時:"         android:textSize="20sp"        /> </LinearLayout>

java代碼:

package com.test.countdown;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.os.Handler;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class CountDownActivity extends Activity {private EditText et_input;private Button bt_start;private TextView tv_time;private long time;private static final String DOWN="down";private boolean flag;//線程迴圈標識private StringBuilder sb;private BroadcastReceiver receiver=new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent){if(intent.getAction().equals(DOWN)){   if(time>0)  {  time=time-1;  }    if(time==0)  {  flag=false;//讓線程停止  bt_start.setClickable(true);  }  long hour=time/3600;  if(hour<10){ sb.append(0);  }  sb.append(hour);  sb.append(":");    long minute=time%3600/60;  if(minute<10){ sb.append(0);  }  sb.append(minute);  sb.append(":");      long second=time%3600%60;  if(second<10){ sb.append(0);  }  sb.append(second);  tv_time.setText("搶購倒計時:"+sb.toString());  sb.delete(0, sb.length());//刪除StringBulider裡面的內容}}};    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        sb=new StringBuilder();        setView();        //註冊廣播事件        IntentFilter filter=new IntentFilter("down");        registerReceiver(receiver, filter);      }        private void setView()    {    et_input=(EditText) findViewById(R.id.et_input);    bt_start=(Button) findViewById(R.id.bt_start);    tv_time=(TextView) findViewById(R.id.tv_time);    }        public void begin(View view)    {       String content=et_input.getText().toString().trim();    System.out.println(content);    if(TextUtils.isEmpty(content))    {    Toast.makeText(this, "輸入不可為空,請輸入時間(秒)!", 0).show();      return;    }        if(!TextUtils.isDigitsOnly(content))    {    Toast.makeText(this, content+"是錯誤的時間!", 0).show();    et_input.setText("");    return;    }    time=Long.parseLong(content);    flag=true;    bt_start.setClickable(false);//按鈕在開始倒計時後不能被點擊    //開闢一個子線程每隔一秒發送一下廣播    new Thread(){    public void run() {                while(flag){        try        {        sleep(1000);        Intent intent=new Intent("down");        sendBroadcast(intent);        } catch (InterruptedException e)        {        e.printStackTrace();        }                }    };    }.start();    }            @Override    protected void onDestroy()    {    unregisterReceiver(receiver);    flag=false;    super.onDestroy();    }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.