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:
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:
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
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
I created new blank activity project with Action bar types navigation and implemented the above code and showed the following warning
ReplyDeleteRenderSecurityManager being replaced by another thread
I was just browsing around, looking for the best essay writing service 2018 and I got here somehow and I love it! You made my day! Thank you!
ReplyDeleteReally enjoyed this blog post, how can I make is so that I receive an email sent to me every time you write a fresh article? B2B lead generation
ReplyDeleteHave you ever considered about including a little bit more than just your articles? I mean, what you say is important and everything. However think about if you added some great visuals or videos to give your posts more, "pop"! Your content is excellent but with pics and video clips, this site could definitely be one of the most beneficial in its niche. Superb blog! business social marketing
ReplyDeleteSimply wish to say your article is as astonishing. The clarity on your post is just excellent and that i could suppose you are knowledgeable on this subject. Fine together with your permission allow me to take hold of your feed to stay updated with coming near near post. Thank you one million and please keep up the enjoyable work. facebook advertising costs singapore
ReplyDelete