Created
October 4, 2013 14:22
-
-
Save sourabh86/6826730 to your computer and use it in GitHub Desktop.
Main Activity for Action time tick example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cc.demo.digiClock; | |
import java.util.Calendar; | |
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.widget.TextView; | |
public class DigiClockActivity extends Activity { | |
private static BroadcastReceiver tickReceiver; | |
private TextView hourText; | |
private TextView minuteText; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
hourText=(TextView)findViewById(R.id.hourText); | |
minuteText=(TextView)findViewById(R.id.minuteText); | |
//Set the current time at startup | |
hourText.setText(String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY))); | |
minuteText.setText(String.valueOf(Calendar.getInstance().get(Calendar.MINUTE))); | |
//Create a broadcast receiver to handle change in time | |
tickReceiver=new BroadcastReceiver(){ | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK)==0) | |
{ | |
hourText.setText(String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY))); | |
minuteText.setText(String.valueOf(Calendar.getInstance().get(Calendar.MINUTE))); | |
} | |
} | |
}; | |
//Register the broadcast receiver to receive TIME_TICK | |
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); | |
} | |
@Override | |
public void onStop() | |
{ | |
super.onStop(); | |
//unregister broadcast receiver. | |
if(tickReceiver!=null) | |
unregisterReceiver(tickReceiver); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment