20.11.2016 Views

ANDROID APP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

60 บทที่ 3 เธรด เซอร์วิส รีซีฟเวอร์ และการแจ้งเตือน<br />

ชุดคำสั่งที่ 3.6 src/com/cookbook/background_timer/BackgroundTimer.java<br />

package com.cookbook.background_timer;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.os.Handler;<br />

import android.os.SystemClock;<br />

import android.view.View;<br />

import android.widget.Button;<br />

import android.widget.TextView;<br />

public class BackgroundTimer extends Activity {<br />

//keep track of button presses, a main thread task<br />

private int buttonPress=0;<br />

TextView mButtonLabel;<br />

//counter of time since app started, a background task<br />

private long mStartTime = 0L;<br />

private TextView mTimeLabel;<br />

//Handler to handle the message to the timer task<br />

private Handler mHandler = new Handler();<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

if (mStartTime == 0L) {<br />

mStartTime = SystemClock.uptimeMillis();<br />

mHandler.removeCallbacks(mUpdateTimeTask);<br />

mHandler.postDelayed(mUpdateTimeTask, 100);<br />

}<br />

mTimeLabel = (TextView) findViewById(R.id.text);<br />

mButtonLabel = (TextView) findViewById(R.id.trigger);<br />

}<br />

Button startButton = (Button) findViewById(R.id.trigger);<br />

startButton.setOnClickListener(new View.OnClickListener() {<br />

public void onClick(View view){<br />

mButtonLabel.setText("Pressed " + ++buttonPress<br />

+ " times");<br />

}<br />

});

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!