Skip to main content

Posts

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...
Recent posts

RxJava introduction for Android developer

RxJava is Java implementation of Reactive Extension. Basically it’s a library that composes asynchronous events by following Observer Pattern. You can create asynchronous data stream on any thread, transform the data and consumed it by an Observer on any thread. The library offers operators like map, combine, merge, filter and lot more that can be applied onto data stream.  Below are the list of schedulers available and their brief introduction. - Schedulers.io()  – This is used to perform non CPU-intensive operations like making network calls, reading disc / files, database operations etc., This maintains pool of threads. 

 - AndroidSchedulers.mainThread()  – This provides access to android Main Thread / UI Thread. Usually operations like updating UI, user interactions happens on this thread. We shouldn’t perform any intensive operations on this thread as it makes the app glitchy or ANR dialog can be thrown. 

 - Schedulers.newThre...

How prevent the Android OS to take a screenshot of the app when it's being pushed into the background for security reasons

Introduction to Kotlin

Why Kotlin? Java have so much boilerplate code. Some other languages has performance issues.  Kotlin is statically typed language that runs on the Java virtual machine and also can be compiled to JavaScript source code. Inspired by Java, Scala, C# and Groovy. Kotlin develop such a way that it can be use for any type of application such as web, Desktop,  Server-side. But it's adopted heavily in Android.  Interoperability allows gradual adoption. So no need to change whole codebase into Kotlin. Gradle has adopted Kotlin for it’s scripting and plugins. Gradle using Groovy for scripting and plugins. In Kotlin everything is an object. Two ways to declare variables in Kotlin : var for mutable and val is for immutable. These are some of the plus points of this language. So let's dive into this language. Kotlin programs start at the main function. When targeting JVM, function will be compiled as a static method. To save...

Sign in with Phone Number using Digits

Today we are going to implement "Sign in with Phone Number" using Digits. I'm using Android Studio for development. So first we need to download and install Fabric plugin. Go to  https://fabric.io  and create account. After that you need to download Fabric plugin. Once you download and install plugin, you will see Fabric plugin icon in Android Studio. 1. Now create a sample project with one activity. 2. Click on that Fabric plugin button and you will see login page. 3. Enter username and password and login to Fabric.It will detect if any of it's kit installed in project or not.  4. Then click on install button of Digits Kit. Apply the changes in your project. After that it will add some data to your project as well as in AndroidManifest. That's it you are successfully integrate Digits kit in your Android project. Here is the activity_main.xml < LinearLayout xmlns:android= "http://schemas.android.com/apk/res/an...

WeekView in Android

Hello Friends,    Have you Searching for Android calender with WeekView.  Today I am sharing android tutorial for android custom calendar WeekView. WeekView.java public class WeekView extends ActionBarActivity implements OnItemClickListener, OnClickListener { private GridView mGrid; private GregorianCalendar mCalendar; private Date[] mWeek; private TextView mMonthText; private RelativeLayout mArrowRight; private RelativeLayout mArrowLeft; private CalendarAdapter mAdapter; private SimpleDateFormat mFormatMonth = new SimpleDateFormat("MMMM"); private SimpleDateFormat mFormatDay = new SimpleDateFormat("d"); private SimpleDateFormat mFormatYear = new SimpleDateFormat("yyyy"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_week_view); mGrid=(GridView)findViewById(R.id.gridview); mGrid.s...

Pull links from string in Android

Sample code: String Message="http://indianexpress.com/article/sports/football/mexico-holds-host-brazil-to-0-0-draw-at-world-cup/ http://indianexpress.com/article/sports/cricket/suresh-raina-surprised-by-stuart-binnys-performance/ http://indianexpress.com/article/sports/football/fifa-world-cup-every-match-is-like-a-final-for-us-says-andres-iniesta/" ArrayList<String> forrmatted=pullLinks(Message);  for(int i=0;i<forrmatted.size();i++){       String temp=forrmatted.get(i).toString();       Message=Message.replace(temp, "["+temp+"]"); } private ArrayList<String> pullLinks(String text) {  ArrayList<String> links = new ArrayList<String>();  String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";  Pattern p = Pattern.compile(regex);  Matcher m = p.matcher(text);  while(m.find()) {     String urlStr = m.group(); ...