Search Tutorials

Monday 11 November 2013

Display Time using Broadcast Receiver in Android

In this application, we will learn how to use broadcast services and how to perform action in every minute. We used one analog clock and one text view to display time. Create a new project, open XML file and drop one analog clock and one text view in relative layout. The code of android XML file is given below:

Display time using Broadcast receiver in Android
Broadcast Receiver Android Example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="#abc" >
<AnalogClock
  android:id="@+id/analogClock1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignTop="@+id/textfield"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="24dp" />
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/analogClock1"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="30dp"
  android:textStyle="bold"
  android:textSize="25sp"
  android:text="Current time is processing ...." />
</RelativeLayout>

Now open your Java file and use broadcast class just like in the previous example but here we are registering broadcast object with time tick intent filter which will be used to get updates every minute. The code is almost similar to the previous example except for the code in onReceive() method. First onCreate() method will be called, then after one minute onReceive() method will be called which will display time on Toast and Textview and this method will be called every minute. The code of the android Java file is given below:

package com.example.check_out_time; // Your Package name
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;
import android.widget.Toast;

public class MainActivity extends Activity {
  Context context=null;
  //broadcast class is used as nested class
  private BroadcastReceiver mtimeInfoReceiver = new BroadcastReceiver(){
  @Override
  public void onReceive(Context c, Intent i) {
  //initialize objects
  TextView textv=(TextView)findViewById(R.id.textView1);
  Calendar calendar=Calendar.getInstance();
  //display time on toast
  Toast.makeText(getApplicationContext(), "current time is--- :-"+calendar.getTime(), Toast.LENGTH_LONG).show();
  //set time on text view
  textv.setText(""+calendar.getTime());
  }
 };
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //use action time tick for getting update every minute
  IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
  //register broadcast receiver
  registerReceiver(mtimeInfoReceiver, mTime);
  }
} 

Now run your project and test application. If you have any doubts, please comment. Share and help others.

Related Tutorials:-

Convert Text to Speech

Use Shared Preferences

Perform action on any Hardware button

Get all mobile Contacts

Get Battery level using Broadcast Receiver

1 comment:

  1. I created new blank activity project with Action bar types navigation and implemented the above code and showed the following warning

    RenderSecurityManager being replaced by another thread

    ReplyDelete

Back to Top