tag:blogger.com,1999:blog-71362426537979225632024-03-16T12:37:46.641+05:30Android DevelopmentOn these blogs I am explaining about how to start android development with java and Kotlin. On this blog I added the fixes and issues we got during android development.SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comBlogger41125tag:blogger.com,1999:blog-7136242653797922563.post-39929461374361191342022-06-27T18:00:00.003+05:302022-06-27T18:00:29.782+05:30Advanced android interview questions<p><b> What is Application class?</b></p><p>The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.</p><p><br /></p><p><b>What is onSavedInstanceState() and onRestoreInstanceState() in activity?</b></p><p><span style="font-family: courier;">onSavedInstanceState() </span>- This method is used to store data before pausing the activity.</p><p><span style="font-family: courier;">onRestoreInstanceState()</span> - This method is used to recover the saved state of an activity when the activity is recreated after destruction. So, the onRestoreInstanceState() receive the bundle that contains the instance state information.</p><p><br /></p><p><b>What is the difference between FragmentPagerAdapter vs FragmentStatePagerAdapter?</b></p><p><span style="font-family: courier;">FragmentPagerAdapter</span>: Each fragment visited by the user will be stored in the memory but the view will be destroyed. When the page is revisited, then the view will be created not the instance of the fragment.</p><p><span style="font-family: courier;">FragmentStatePagerAdapter</span>: Here, the fragment instance will be destroyed when it is not visible to the user, except the saved state of the fragment.</p><p><br /></p><p><b>What is the purpose of addToBackStack() while commiting fragment transaction?</b></p><p>By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button. </p><p><b><br /></b></p><p><b>What are ViewGroups and how they are different from the Views?</b></p><p><span style="font-family: courier;">View</span>: View objects are the basic building blocks of User Interface(UI) elements in Android. View is a simple rectangle box which responds to the user’s actions. Examples are EditText, Button, CheckBox etc. View refers to the android.view.View class, which is the base class of all UI classes.</p><p><span style="font-family: courier;">ViewGroup</span>: ViewGroup is the invisible container. It holds View and ViewGroup. For example, LinearLayout is the ViewGroup that contains Button(View), and other Layouts also. ViewGroup is the base class for Layouts.</p><p><br /></p><p><b>What is a Sticky Intent?</b></p><p>Sticky Intents allows communication between a function and a service. sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). For example, if you take an intent for ACTION_BATTERY_CHANGED to get battery change events: When you call registerReceiver() for that action — even with a null BroadcastReceiver — you get the Intent that was last Broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.</p><p><br /></p><p><b>What is the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?</b></p><p>An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.</p><p>Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).</p><p>There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activity, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.</p><p>For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.</p><p>Note: AsyncTasks by default run on a single thread using a serial executor, meaning it has only 1 thread and each task runs one after the other.</p><p><br /></p><p><b>What is commit() and apply() in SharedPreferences?</b></p><p><span style="font-family: courier;">commit() </span>returns a boolean value of success or failure immediately by writing data synchronously.</p><p><span style="font-family: courier;">apply()</span> is asynchronous and it won't return any boolean response. If you have an apply() outstanding and you are performing commit(), then the commit() will be blocked until the apply() is not completed.</p><p><br /></p><p><b>What is a SpannableString?</b></p><p>A SpannableString has immutable text, but its span information is mutable. Use a SpannableString when your text doesn't need to be changed but the styling does. Spans are ranges over the text that include styling information like color, heighliting, italics, links, etc</p><p><br /></p><p><b>Differences between abstract classes and interfaces?</b></p><p>An <span style="font-family: courier;">abstract </span>class, is a class that contains both concrete and abstract methods (methods without implementations). An abstract method must be implemented by the abstract class sub-classes. Abstract classes cannot be instantiated and need to be extended to be used.</p><p>An <span style="font-family: courier;">interface</span> is like a blueprint/contract of a class (or it may be thought of as a class with methods, but without their implementation). It contains empty methods that represent, what all of its subclasses should have in common. The subclasses provide the implementation for each of these methods. Interfaces are implemented.</p><p><br /></p><p><b>What is the difference between iterator and enumeration in java?</b></p><p>In Enumeration we don't have remove() method and we can only read and traverse through a collection.</p><p>Iterators can be applied to any collection. In Iterator, we can read and remove items from a collection.</p><p><br /></p><p><b>What are the access modifiers you know? What does each one do?</b></p><p>There are four access modifiers in Java language (from strictest to the most lenient):</p><p><span style="font-family: courier;">private </span>variables, methods, constructors or inner classes are only visible to its' containing class and its' methods. This modifier is most commonly used, for example, to allow variable access only through getters and setters or to hide underlying implementation of classes that should not be used by user and therefore maintain encapsulation. Singleton constructor is also marked private to avoid unwanted instantiation from outside.</p><p><span style="font-family: courier;">Default </span>(no keyword is used) this modifier can be applied to classes, variables, constructors and methods and allows access from classes and methods inside the same package.</p><p><span style="font-family: courier;">protected</span> can be used on variables, methods and constructors therefore allowing access only to subclasses and classes that are inside the same package as protected members' class.</p><p><span style="font-family: courier;">public</span> modifier is widely-used on classes, variables, constructors and methods to grant access from any class and method anywhere. It should not be used everywhere as it implies that data marked with public is not sensitive and can not be used to harm the program.</p><p><br /></p><p><b>Can an Interface implement another Interface?</b></p><p>Yes, an interface can implement another interface (and more than one), but it needs to use extends, rather than implements keyword. And while you can not remove methods from parent interface, you can add new ones freely to your sub-interface.</p><p><br /></p><p><b>What does it means to say that a String is immutable?</b></p><p>It means that once created, String object's char[] (its' containing value) is declared final and, therefore, it can not be changed during runtime.</p><p><br /></p><p><b>What is String.intern()? When and why should it be used?</b></p><p>String.intern() is used to mange memory in Java code. It is used when we have duplicates value in different strings. When you call the String.intern(), then if in the String pool that string is present then the equals() method will return true and it will return that string only.</p><p><br /></p><p><b>What is garbage collector? How does it work?</b></p><p>All objects are allocated on the heap area managed by the JVM. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.</p><p><br /></p><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-64602583604423102122022-06-27T17:45:00.005+05:302022-06-27T17:46:09.063+05:30Android Interview Questions 2<p><span style="font-family: inherit;"><b> Describe different types of Services in Android ?</b></span></p><p><span style="font-family: inherit;">A Service is an application component that can perform long-running operations in the background, and it doesn't provide a user interface. It can run in the background, even when the user is not interacting with your application. These are the three different types of services:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit;"><b>Foreground Service</b>: A foreground service performs some operation that is noticeable to the user. For example, we can use a foreground service to play an audio track.</span></li><li><span style="font-family: inherit;"><b>Background Service</b>: A background service performs an operation that isn’t directly noticed by the user. In Android API level 26 and above, there are restrictions to using background services and it is recommended to use WorkManager in these cases.</span></li><li><span style="font-family: inherit;"><b>Bound Service</b>: A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results. A bound service runs only as long as another application component is bound to it.</span></li></ul><p></p><p><span style="font-family: inherit;"><br /></span></p><p><b><span style="font-family: inherit;">How can two distinct Android apps interact?</span></b></p><p><span style="font-family: inherit;">At the simplest level there are two different ways for apps to interact on Android: via Intents, passing data from one application to another; and through Services, where one application provides functionality for others to use.</span></p><p><span style="font-family: inherit;"><br /></span></p><p><b><span style="font-family: inherit;">What are Retained Fragments?</span></b></p><p><span style="font-family: inherit;">By default, Fragments are destroyed and recreated along with their parent Activity’s when a configuration change occurs.</span></p><p><span style="font-family: inherit;">Calling setRetainInstance(true) allows us to bypass this destroy-and-recreate cycle, signaling the system to retain the current instance of the fragment when the activity is recreated.</span></p><p><span style="font-family: inherit;"><br /></span></p><p><b><span style="font-family: inherit;">What are the permission protection levels in Android?</span></b></p><p><span style="font-family: inherit;"><b>Normal </b>— A lower-risk permission that gives requesting applications access to isolated application-level features, with minimal risk to other applications, the system, or the user. The system automatically grants this type of permission to a requesting application at installation, without asking for the user’s explicit approval.</span></p><p><span style="font-family: inherit;"><b>Dangerous </b>— A higher-risk permission. Any dangerous permissions requested by an application may be displayed to the user and require confirmation before proceeding, or some other approach may be taken to avoid the user automatically allowing the use of such facilities.</span></p><p><span style="font-family: inherit;"><b>Signature </b>— A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user’s explicit approval.</span></p><p><span style="font-family: inherit;"><b>SignatureOrSystem </b>— A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission.</span></p><p><span style="font-family: inherit;"><br /></span></p><p><b><span style="font-family: inherit;">What is a JobScheduler?</span></b></p><p><span style="font-family: inherit;">The JobScheduler API performs an operation for your application when a set of predefined conditions are met (such as when a device is plugged into a power source or connected to a Wi-Fi network). This allows your app to perform the given task while being considerate of the device's battery at the cost of timing control.</span></p><p><span style="font-family: inherit;">Unlike the AlarmManager class, the timing isn't exact. Compared to a custom SyncAdapter or the AlarmManager, the JobScheduler supports batch scheduling of jobs. The Android system can combine jobs so that battery consumption is reduced. JobManager makes handling uploads easier as it handles automatically the unreliability of the network. It also survives application restarts. Here are example when you would use this job scheduler:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit;">Tasks that should be done once the device is connect to a power supply</span></li><li><span style="font-family: inherit;">Tasks that require network access or a Wi-Fi connection.</span></li><li><span style="font-family: inherit;">Task that are not critical or user facing</span></li><li><span style="font-family: inherit;">Tasks that should be running on a regular basis as batch where the timing is not critical</span></li></ul><p></p><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><br /></span></div><div><b><span style="font-family: inherit;">What is the ViewHolder pattern? Why should we use it?</span></b></div><div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">Every time when the adapter calls getView() method, the findViewById() method is also called. This is a very intensive work for the mobile CPU and so affects the performance of the application and the battery consumption increases. ViewHolder is a design pattern which can be applied as a way around repeated use of findViewById(). A ViewHolder holds the reference to the id of the view resource and calls to the resource will not be required after you "find" them: Thus performance of the application increases.</span></div></div><div><span style="font-family: inherit;"><br /></span></div><div><b><span style="font-family: inherit;"><br /></span></b></div><div><div><b><span style="font-family: inherit;">What is the difference between Handler vs AsyncTask vs Thread?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The <b>Handler</b> class can be used to register to a thread and provides a simple channel to send data to this thread. A Handler allows you communicate back with the UI thread from other background thread.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The <b>AsyncTask </b>class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">And a <b>Thread </b>is basically the core element of multithreading which a developer can use with the </span></div><div><span style="font-family: inherit;">following disadvantage:</span></div><div><ul style="text-align: left;"><li><span style="font-family: inherit;">Handle synchronization with the main thread if you post back results to the user interface</span></li><li><span style="font-family: inherit;">No default for canceling the thread</span></li><li><span style="font-family: inherit;">No default thread pooling</span></li><li><span style="font-family: inherit;">No default for handling configuration changes in Android</span></li></ul></div></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><br /></span></div><div><div><b><span style="font-family: inherit;">What is the difference between compileSdkVersion and targetSdkVersion?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The <b>compileSdkVersion </b>is the version of the API the app is compiled against. This means you can use Android API features included in that version of the API (as well as all previous versions, obviously). If you try and use API 16 features but set compileSdkVersion to 15, you will get a compilation error. If you set compileSdkVersion to 16 you can still run the app on a API 15 device as long as your app's execution paths do not attempt to invoke any APIs specific to API 16.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The <b>targetSdkVersion </b>has nothing to do with how your app is compiled or what APIs you can utilize. The targetSdkVersion is supposed to indicate that you have tested your app on (presumably up to and including) the version you specify. This is more like a certification or sign off you are giving the Android OS as a hint to how it should handle your app in terms of OS features.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><br /></span></div><div><div><b><span style="font-family: inherit;">What is the difference between a Bundle and an Intent?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">A <b>Bundle </b>is a collection of key-value pairs.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">However, an <b>Intent </b>is much more. It contains information about an operation that should be performed. This new operation is defined by the action it can be used for, and the data it should show/edit/add. The system uses this information for finding a suitable app component (activity/broadcast/service) for the requested action.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><span style="font-family: inherit;"><br /></span></div><div><div><b><span style="font-family: inherit;">What is an Intent? Can it be used to provide data to a ContentProvider? Why or why not?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The Intent object is a common mechanism for starting new activity and transferring data from one activity to another. However, you cannot start a ContentProvider using an Intent.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">When you want to access data in a ContentProvider, you must instead use the ContentResolver object in your application’s Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><br /></span></div><div><div><b><span style="font-family: inherit;">What is the difference between Service and IntentService? How is each used?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><b>Service</b> is the base class for Android services that can be extended to create any service. A class that directly extends Service runs on the main thread so it will block the UI (if there is one) and should therefore either be used only for short tasks or should make use of other threads for longer tasks.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><b>IntentService </b>is a subclass of Service that handles asynchronous requests (expressed as “Intents”) on demand. Clients send requests through startService(Intent) calls. The service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. Writing an IntentService can be quite simple; just extend the IntentService class and override the onHandleIntent(Intent intent) method where you can manage all incoming requests.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><span style="font-family: inherit;"><br /></span></div><div><b><span style="font-family: inherit;"><br /></span></b></div><div><div><b><span style="font-family: inherit;">What is a broadcast receiver?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">The broadcast receiver communicates with the operation system messages such as “check whether an internet connection is available,” what the battery label should be, etc.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><b><span style="font-family: inherit;"><br /></span></b></div><div><div><b><span style="font-family: inherit;">Is it possible to create an activity in Android without a user interface ?</span></b></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;">Yes, an activity can be created without any user interface. These activities are treated as abstract activities.</span></div><div><span style="font-family: inherit;"><br /></span></div></div><div><br /></div><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-90588234430199313502022-06-27T17:29:00.002+05:302022-06-27T17:46:02.408+05:30Android Interview Questions 1<p><span style="font-family: inherit; font-size: medium;"><b>What’s Android?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Android is a Linux-based, open-sourced operating system commonly found on mobile devices, such as smartphones and tablets. It’s a kernel-based system that gives developers the flexibility to design and deploy simple and/or advanced apps.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>Explain the Android SDK?</b></span></p><p><span style="font-family: inherit; font-size: medium;">This is a set of tools that Android developers use in order to develop or write apps. It features a graphical user interface that emulates a handheld, Android-driven environment, making it easier for developers to create, test, and debug their code.</span></p><p><span style="font-family: inherit; font-size: medium;">The tools include:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">Dalvik Debug Monitoring Services</span></li><li><span style="font-family: inherit; font-size: medium;">Android Emulator</span></li><li><span style="font-family: inherit; font-size: medium;">Android Asset Packaging Tool</span></li><li><span style="font-family: inherit; font-size: medium;">Android Debug Bridge</span></li></ul><p></p><div><br /></div><p><span style="font-family: inherit; font-size: medium;"><b>What is use of AndroidManifest.xml file?</b></span></p><p><span style="font-family: inherit; font-size: medium;">AndroidManifest.xml file is responsible to protect the application from accessing any protected parts by providing the permissions. Every app project has an AndroidManifest.xml file which is also the root of the project source set. This file contains information about your package, Android operating system, the Android build tools, Google Play, and other including components of the application such as activities, content providers, services, broadcast receivers, etc.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What is an android application architecture?</b></span></p><p><span style="font-family: inherit; font-size: medium;">The Android architecture consists of:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">Android Framework</span></li><li><span style="font-family: inherit; font-size: medium;">Android Applications</span></li><li><span style="font-family: inherit; font-size: medium;">Linux Kernel</span></li><li><span style="font-family: inherit; font-size: medium;">Libraries</span></li></ul><p></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>Name the basic tools for developing an Android App?</b></span></p><p><span style="font-family: inherit; font-size: medium;">The tools used for development are:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">JDK</span></li><li><span style="font-family: inherit; font-size: medium;">SDK Tools</span></li><li><span style="font-family: inherit; font-size: medium;">Eclipse+ADT Plugin</span></li></ul><div><span style="font-size: medium;"><br /></span></div><p><span style="font-family: inherit; font-size: medium;"><b>What does APK mean?</b></span></p><p><span style="font-family: inherit; font-size: medium;">It’s short for Android Packaging Kit. Every file in the Android packaging key is compressed into a single file, the APK.</span></p><p><span style="font-family: inherit; font-size: large;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"><b>What are Activities?</b></span></p><p><span style="font-family: inherit; font-size: medium;">These are the parts of a mobile app that a user sees and interacts with. It represents a Graphic User Interface (GUI), representing one Android screen.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>State the life cycle methods of Android activities?</b></span></p><p><span style="font-family: inherit; font-size: medium;">There are seven lifecycle methods of Android activities. They are:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">On create()</span></li><li><span style="font-family: inherit; font-size: medium;">On start()</span></li><li><span style="font-family: inherit; font-size: medium;">On resume()</span></li><li><span style="font-family: inherit; font-size: medium;">On pause()</span></li><li><span style="font-family: inherit; font-size: medium;">On stop()</span></li><li><span style="font-family: inherit; font-size: medium;">On restart()</span></li><li><span style="font-family: inherit; font-size: medium;">On destroy()</span></li></ul><p></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What are the four essential activity states?</b></span></p><p><span style="font-family: inherit; font-size: medium;">The four states are:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;"><b>Active</b>: The activity is at the top of the stack, running in the foreground</span></li><li><span style="font-family: inherit; font-size: medium;"><b>Paused</b>: The activity is still visible but cannot receive user input events; it’s in the background</span></li><li><span style="font-family: inherit; font-size: medium;"><b>Stopped</b>: The activity is invisible and consequently is paused, and obscured or hidden by a different activity</span></li><li><span style="font-family: inherit; font-size: medium;"><b>Destroyed</b>: The activity’s process has been killed, completed, or terminated</span></li></ul><p></p><p><br /></p><p><span style="font-size: medium;"><b><span style="font-family: inherit;">What is a </span>fragment<span style="font-family: inherit;"> in Android?</span></b></span></p><p><span style="font-family: inherit; font-size: medium;">The fragment in Android is as follows.</span></p><p><span style="font-family: inherit; font-size: medium;">A fragment has its own layout and behavior with its own life cycle callbacks.</span></p><p><span style="font-family: inherit; font-size: medium;">Fragments can be added or removed in an activity while the activity is running and used in multiple activities.</span></p><p><span style="font-family: inherit; font-size: medium;">Multiple fragments can be combined in a single activity to build a multi-pane UI.</span></p><p><span style="font-family: inherit; font-size: medium;">The fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped</span></p><p><span style="font-family: inherit; font-size: medium;">Fragments were added to the Android API in the Honeycomb version of Android API version 11.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"><b>What’s a content provider?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Content providers share information between different Android applications. They allow users to access data within an application. Examples include contact information, images, video, and audio.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"><b>What’s an intent in the context of Android? Describe the different types?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Much what it sounds like, it’s the intention to perform an action, a message that is passed between components. Intents request actions from a different component, such as sending an email, opening a web page, or launch a given activity. The two types are:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;"><b>Implicit Intent </b>: </span>This is where the intent doesn’t define the target component, requiring the Android system to conduct an evaluation of the components.</li><li><span style="font-family: inherit; font-size: medium;"><b>Explicit Intent </b>: </span><span style="font-family: inherit; font-size: large;">On the other hand, the explicit intent directly identifies the target component.</span></li></ul><p></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>What’s a sticky intent?</b></span></p><p><span style="font-family: inherit; font-size: medium;">This is a broadcast using the send sticky broadcast() method. The intent sticks around after the broadcast, which allows others to collect data from it.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What is a Pending Intent?</b></span></p><p><span style="font-family: inherit; font-size: medium;">A Pending Intent is an action to require in the future. It allows you to pass a future Intent to a different application and permit that application to execute the Intent.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>What’s a “bundle” in Android?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Bundles are used to pass the required data to sub-folders</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What is ANR?</b></span></p><p><span style="font-family: inherit; font-size: medium;">This is an acronym for Application Not Responding, a pop-up or notification that kicks in when the application is experiencing lag time for the user due to too many functions being performed simultaneously.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What's the difference between onCreate() and onStart()?</b></span></p><p><span style="font-family: inherit; font-size: medium;">The major difference between onCreate() and onStart() is as follows.</span></p><p><span style="font-family: inherit; font-size: medium;"><b>onCreate() </b>is called when the Activity is created; that is, it is launched or started. (Additionally, this triggers when the orientation is changed.) It is called again when the process is killed, then returned to.</span></p><p><span style="font-family: inherit; font-size: medium;">while<b> onStart() </b>called following onCreate() at startup. Additionally, it is also called when the app is navigated back to after onStop() (and following onRestart()), which occurs after the Activity is no longer visible (which is during the time that "the user can see the activity on-screen, though it may not be in the foreground and interacting with the user").</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"><b> What is service in Android? List its types?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Services in Android are a component that acts as an application to run in the background in order to perform long-running operation tasks.</span></p><p><span style="font-family: inherit; font-size: medium;">The types of Services are listed below:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">Background Services</span></li><li><span style="font-family: inherit; font-size: medium;">Foreground Services</span></li><li><span style="font-family: inherit; font-size: medium;">Bound Services</span></li></ul><div><span style="font-size: medium;"><br /></span></div><p></p><p><span style="font-family: inherit; font-size: medium;"><b>Explian the use of AsyncTasks?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Android AsyncTask going to do background operation on the background thread and update on the main thread. In android, We can't directly touch the background thread to the main thread in android development. asyncTask helps us to make communication between the background thread to the main thread.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>Explain different launch modes in Android?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Different launch modes are explained below:</span></p><p><span style="font-family: inherit; font-size: medium;">Android Launch mode '<b>Standard</b>' — Multiple instances every time, A very default launch mode!</span></p><p><span style="font-family: inherit; font-size: medium;">Launch mode '<b>SingleTop</b>' — Multiple instances Conditionally</span></p><p><span style="font-family: inherit; font-size: medium;">Specialized Launch mode '<b>SingleTask</b>' — Single instance throughout System.</span></p><p><span style="font-family: inherit; font-size: medium;">Specialized Launch mode '<b>SingleInstance</b>' — Single instance throughout System.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>What is Proguard?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Proguard detects and removes unused classes, fields, methods, and attributes. Mobile app development companies use the Proguard in android, it optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods by using short meaningless names.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What is the difference between Serializable and Parseable?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Serializable is a standard Java interface. It is not a part of the Android SDK. Its simplicity is its biggest beauty. Just by implementing this interface the POJO will be ready to jump from one particular activity to another. There is another interface known as parseable. It is a part of the Android SDK. Paracelable was specifically designed in a way that there is no reflection left while using.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>Give a brief idea about Android application architecture?</b></span></p><p><span style="font-family: inherit; font-size: medium;">The architecture of Android application has few components, which have their functions to perform:</span></p><p><span style="font-family: inherit; font-size: medium;"><b>Service</b>: It performs the background functions</span></p><p><span style="font-family: inherit; font-size: medium;"><b>Intent</b>: Performs the interconnection function between activities and data passing mechanism</span></p><p><span style="font-family: inherit; font-size: medium;"><b>Resource</b>: Externalization it is used to perform strings and graphics functions</span></p><p><span style="font-family: inherit; font-size: medium;"><b>Notification</b>: This shows the lights, sound, icon, notification, incoming chats messages dialogue box, etc.</span></p><p><span style="font-family: inherit; font-size: medium;"><b>Content providers</b>: It shares the data between applications.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>What are few exceptions in Android?</b></span></p><p><span style="font-family: inherit; font-size: medium;">These are the following exceptions in Android:</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: inherit; font-size: medium;">Inflate exception</span></li><li><span style="font-family: inherit; font-size: medium;">Surface.out of resource exception</span></li><li><span style="font-family: inherit; font-size: medium;">Surface holder bad surface type exception</span></li><li><span style="font-family: inherit; font-size: medium;">Window manager took exception.</span></li></ul><p></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>Explain Orientation?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Orientation is set using set orientation (), dictates whether the linear layout is represented as a row or else as a column. There the values are set as HORIZONTAL or VERTICAL.</span></p><p><br /></p><p><span style="font-family: inherit; font-size: medium;"><b>what is the differentiate between Activities and Services?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Activities can be closed or can be terminated easily as and when the user wants to. Services are designed to run on the background or behind the scenes and can act independently. Most of the services run on a continuous basis regardless of their certain or no certain activities been executed.</span></p><p><span style="font-family: inherit; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: inherit; font-size: medium;"><b>What are runnable in android?</b></span></p><p><span style="font-family: inherit; font-size: medium;">Runnable is a concurrent unit of execution in Android. It is also a class that implements a Runnable interface. The Runnable interface is used extensively to execute code in Threads.</span></p><p><span style="font-family: inherit; font-size: medium;"><br /></span></p><p><br /></p><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-52946221719980715892022-01-31T12:30:00.003+05:302022-01-31T12:30:56.628+05:30Splash Screen in Kotlin<div style="text-align: left;"><span style="font-family: arial;"> <b> O</b>n this blog I am going to explain how to show a splash screen using Handler. You can find more about <a href="https://developer.android.com/reference/android/os/Handler">Handler</a> from <a href="https://developer.android.com/reference/android/os/Handler">Here</a>. On this I am going to show the redirection to new Activity page after the delay I set. Redirection will be from </span><span style="background-color: white; font-family: "Lucida Sans Typewriter", monospace; font-size: 9.8pt;">SplashActivity</span><span style="font-family: arial;"> which is my splash screen to </span><span style="background-color: white;"><span style="font-family: courier;">MainActivity </span></span><span style="font-family: arial;">which is my landing page</span></div><span style="font-family: arial;"><br />Here I am using a delay of 4000 MS (4 Seconds)<br /><br /><br />Here I am using postDelayed function for this,<br /><br /></span><pre><span style="font-family: courier;">fun postDelayed(r: Runnable!, delayMillis: Long): Boolean</span></pre><span style="font-family: arial;"><br /><br /><u><b>Sample Code</b></u><br /><br /></span><pre style="background-color: white;"><span style="font-family: "Lucida Sans Typewriter", monospace; font-size: 9.8pt;"><br /></span><span style="font-family: courier;"><span style="color: navy; font-weight: bold;">import </span>android.content.Intent<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Bundle<br /><span style="color: navy; font-weight: bold;">import </span>android.os.Handler<br /><span style="color: navy; font-weight: bold;">import </span>androidx.appcompat.app.AppCompatActivity<br /><span style="color: navy; font-weight: bold;">import </span>com.example.MainActivity</span></pre><pre style="background-color: white;"><pre><pre><span style="font-family: courier;"><span style="color: navy; font-weight: bold;">class </span>SplashActivity : AppCompatActivity() {<br /> <span style="color: navy; font-weight: bold;">override fun </span>onCreate(savedInstanceState: Bundle?) {<br /> <span style="color: navy; font-weight: bold;">super</span>.onCreate(savedInstanceState)<br /> setContentView(R.layout.<span style="color: #660e7a; font-style: italic;">activity_splash</span>)<br /><br /> Handler().postDelayed(<span style="font-weight: bold;">{</span></span></pre><pre><span style="font-family: courier;"><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;"> </span><span style="color: navy; font-weight: bold;">val </span>intent = Intent(<span style="color: navy; font-weight: bold;">this</span>, MainActivity::<span style="color: navy; font-weight: bold;">class</span>.<span style="color: #660e7a; font-style: italic;">java</span>)<br /> startActivity(intent)<br /> finish()<br /> </span></pre><pre><span style="font-family: courier;"><span style="font-weight: bold;"><span> </span><span> </span>}</span>, <span style="color: blue;">4000</span>)<br /><br /> }<br />}</span></pre></pre><div style="font-size: 9.6pt;"><br /></div></pre><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com1tag:blogger.com,1999:blog-7136242653797922563.post-53830278492793964042022-01-31T12:23:00.001+05:302022-01-31T12:23:20.186+05:30Setup new Kotlin project on Android Studio<p> <b> </b></p><div><b>O</b>n this blog I am going to explain How to setup new <a href="https://kotlinlang.org/">Kotlin</a> project on Android Studio. To setup new project on <a href="https://kotlinlang.org/">Kotlin</a> on Android project we need <a href="https://developer.android.com/studio/index.html">Android Studio</a> version 3.0 or more. For older version of studio we can user Pligins. You can able to download by selecting "<i>Install JetBrains plugin</i>". </div><div><br /></div><div>Here is the steps to start new Android Project with Kotlin Support.</div><div><br /></div><div><br /></div><div>Goto File => New => NewProject</div><div><br /></div><div>While select <b>New Project</b> you can see the <b>Create New Project window</b>, <br /> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggILbUqGPFWCngJXiRLMyHXyZQOZdflSkhhf2LiDEiIDcvrCPafZ1fQen4h6w3_woN_xiTdZ9klduptKv2l3N3geKbMnYLa9jrj3s3_bhGdXTTY2LgYSWBOcYC5iB5Mxb4xfnvItx0eQY/s1600/New+Project.PNG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="702" data-original-width="905" height="248" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggILbUqGPFWCngJXiRLMyHXyZQOZdflSkhhf2LiDEiIDcvrCPafZ1fQen4h6w3_woN_xiTdZ9klduptKv2l3N3geKbMnYLa9jrj3s3_bhGdXTTY2LgYSWBOcYC5iB5Mxb4xfnvItx0eQY/s320/New+Project.PNG" width="320" /></a><br /><br /> You can add the project details such as <b>Application Name</b>, <b>Company domain</b>, <b>Project Location</b>, <b>Package name </b>etc. And By selecting "<b>Include Kotlin Support</b>" you can enable the Kotlin support on your Project.<br /><br />After This Screen while tap on "Next" button it will show the screen to Select the Target Device,<br /> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WHKWf-n-nSAYiltx9CUQlzK6f_x4FoDaVjgTmO9ebl1rGWwy7jTb1NvOLlooWenPK9iMkBjUZt5kb5XMvSb2as2zrw-TQ-h_BTX2R_MIwTJiPPQgP5OZJT3u7TZvcidHOtMGIoIoQSk/s1600/target_device.PNG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="691" data-original-width="905" height="244" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WHKWf-n-nSAYiltx9CUQlzK6f_x4FoDaVjgTmO9ebl1rGWwy7jTb1NvOLlooWenPK9iMkBjUZt5kb5XMvSb2as2zrw-TQ-h_BTX2R_MIwTJiPPQgP5OZJT3u7TZvcidHOtMGIoIoQSk/s320/target_device.PNG" width="320" /></a><br /><br />This screen will allow you to choose the Android Target Device you need, While tap on Next button it will show the Choose Activity Screen<br /> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilpcOGDMv7xRayFdrnPyQqTMMtghfph8sBov7Gd3rEcbDTqBZwBQM7bll4grGgFdrF-MOp2dWAwHVNdRVPOXRuNyl96md3b1xCzg6aDTlmHapsS5YdGFGhAWONwYWkLIWd5eQbPLpWglI/s1600/add+activity.PNG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="697" data-original-width="907" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilpcOGDMv7xRayFdrnPyQqTMMtghfph8sBov7Gd3rEcbDTqBZwBQM7bll4grGgFdrF-MOp2dWAwHVNdRVPOXRuNyl96md3b1xCzg6aDTlmHapsS5YdGFGhAWONwYWkLIWd5eQbPLpWglI/s320/add+activity.PNG" width="320" /></a><br /><br />On this screen you can select the Type of Activity we need on the Application. While Tap on Next button it will show the screen to Configure the selected Activity page.<br /> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk268KOztQFmXGVaLtnzsnPwB73X3MjrHBTskiTPVNv1YYLnqQkrQ_oVHiFSVwhcoej9KVghrXiO10B02dzU2hhvLWyT39KZRQm6Y6I1c-PFqYRC2G_ENRC9tH_MKfZgmM4sFitqOJeA/s1600/configure+activity.PNG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="697" data-original-width="904" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk268KOztQFmXGVaLtnzsnPwB73X3MjrHBTskiTPVNv1YYLnqQkrQ_oVHiFSVwhcoej9KVghrXiO10B02dzU2hhvLWyT39KZRQm6Y6I1c-PFqYRC2G_ENRC9tH_MKfZgmM4sFitqOJeA/s320/configure+activity.PNG" width="320" /></a><br /><br />On this Screen we can Add the Activity Name and the layout name. And while Tap on Finish button it will Create the New Project with your Configurations. While tap on finish button it will setup your first project. Here is the project window.<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiNwEizH0lI2bF7SVlO1VBaqoIpvH8mSZj9NC3CyDRHclOb6AeLjqgLO6E_FfuKJlz6IE9m0QVJte_mCN5hTW8K4CN-vWrgVyfnk6GLHTYAKSpASWf-vW6jIEJpO54ELSePXwoCiCr2I8/s1600/new+project+done.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="861" data-original-width="1600" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiNwEizH0lI2bF7SVlO1VBaqoIpvH8mSZj9NC3CyDRHclOb6AeLjqgLO6E_FfuKJlz6IE9m0QVJte_mCN5hTW8K4CN-vWrgVyfnk6GLHTYAKSpASWf-vW6jIEJpO54ELSePXwoCiCr2I8/s400/new+project+done.PNG" width="400" /></a></div><br /><br /><br /></div><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-76527136374450508782021-11-29T18:01:00.003+05:302021-11-29T18:04:06.248+05:30How to avoid reverse engineering of an APK file<p><span style="font-family: helvetica;">If someone changes the <a href="http://en.wikipedia.org/wiki/APK_%28file_format%29">.apk</a> extension to .zip then they can unzip it and easily access all the app's resources and assets, and using <a href="http://code.google.com/p/dex2jar/wiki/Faq" target="_blank">dex2jar </a>and a Java </span><span style="font-family: courier;">decompiler</span><span style="font-family: helvetica;">, they can also access the source code. It's very easy to reverse engineer an Android APK file - for more details see Stack Overflow question </span><a href="https://stackoverflow.com/questions/12732882/reverse-engineering-from-apk-to-project" target="_blank"><span style="font-family: courier;">Reverse engineering from an APK file to a project</span></a><span style="font-family: helvetica;">.</span></p><p><span style="font-family: helvetica;">I have used the </span><span style="font-family: courier;">Proguard </span><span style="font-family: helvetica;">tool provided with the Android SDK. When I reverse engineer an APK file generated using a signed </span><span style="font-family: courier;">keystore</span><span style="font-family: helvetica;"> and </span><span style="font-family: courier;">Proguard</span><span style="font-family: helvetica;">, I get obfuscated code.</span></p><p><span style="font-family: helvetica;">However, the names of Android components remain unchanged and some code, like key-values used in the app, remains unchanged. As per </span><span style="font-family: courier;">Proguard </span><span style="font-family: helvetica;">documentation the tool can't obfuscate components mentioned in the Manifest file.</span></p><p><br /></p><p><br /></p><p><span style="font-family: verdana;"><b>*</b> How can I completely prevent reverse engineering of an Android APK? Is this possible?</span></p><p><span style="font-family: helvetica;">AFAIK, there is not any trick for complete avoidance of reverse engineering. And also Whatever you do to your code, a potential attacker is able to change it in any way she or he finds it feasible. You basically can't protect your application from being modified. And any protection you put in there can be disabled/removed.</span></p><p><span style="font-family: helvetica;"><br /></span></p><p><span style="font-family: helvetica;"><br /></span></p><p><span style="font-family: verdana;"><b>* </b>How can I protect all the app's resources, assets and source code so that hackers can't hack the APK file in any way?</span></p><p><span style="font-family: helvetica;">You can do different tricks to make hacking harder though. For example, use obfuscation (if it's Java code). This usually slows down reverse engineering significantly.</span></p><p><span style="font-family: helvetica;"><br /></span></p><p><span style="font-family: helvetica;"><br /></span></p><p><span style="font-family: verdana;"><b>*</b> Is there a way to make hacking more tough or even impossible? What more can I do to protect the source code in my APK file?</span></p><p><span style="font-family: verdana;"><br /></span></p><p><span style="font-family: helvetica;">As everyone says, and as you probably know, there's no 100% security. But the place to start for Android, that Google has built in, is ProGuard. If you have the option of <b>including shared libraries</b>, you can include the needed code in C++ to verify file sizes, integration, etc. If you need to add an external native library to your APK's library folder on every build, then you can use it by the below suggestion.</span></p><p><span style="font-family: helvetica;">Put the library in the native library path which defaults to "</span><span style="font-family: courier;">libs</span><span style="font-family: helvetica;">" in your project folder. If you built the native code for the '</span><span style="font-family: courier;">armeabi</span><span style="font-family: helvetica;">' target then put it under </span><span style="font-family: courier;">libs/armeabi</span><span style="font-family: helvetica;">. If it was built with armeabi-v7a then put it under libs/armeabi-v7a.</span></p><p><span style="font-family: courier;"><project>/libs/armeabi/libstuff.so</span></p><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-84662180131220156662021-03-31T12:51:00.013+05:302023-01-09T16:56:14.589+05:30How to Commit code to Git / Bitbucket from Android Studio<p><span style="font-size: medium;"> <b><span>O</span></b><span>n this blog I am going to explain how to commit the code to a git or bitbucket repository from android studio. I already created a got repository on bitbucket, Here I will show how to commit and push the code to the repository I created on bitbucket.<br /><br />I am doing this using the command on Android studio Terminal,</span></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><b>Step 1</b></span></p><p><span style="font-size: medium;">First we need to initialize the git on out code, <br /><br /><span style="font-family: courier;">git init</span></span></p><p><span style="font-size: medium;"> This command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.</span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><b>Step 2</b></span></p><p><span style="font-size: medium;"><b>Remove Existing git repository, </b></span></p><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><span style="font-family: courier;">git remote rm origin</span></pre><p><span style="font-size: medium;"></span></p><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium; white-space: normal;">If you want to remove the old or existing git repository you can run the above comment.</span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium; white-space: normal;"><br /></span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><span style="font-size: medium;"><span style="white-space: normal;">you can try this code if the above code is not working.</span></span></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-size: var(--fs-body1); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(1.5em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-size: var(--_pr-code-fs); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-family: courier;">rm -rf .git</span></code></pre></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman; font-size: medium; white-space: normal;">Here </span><span style="font-family: courier; font-size: medium; white-space: normal;">-r</span><span style="font-family: Times New Roman; font-size: medium; white-space: normal;"> means "</span><span style="font-family: courier; font-size: medium; white-space: normal;">recursive</span><span style="font-family: Times New Roman; font-size: medium; white-space: normal;">", so it will delete the entire contents of the folder instead of erroring out because the folder isn't empty, and </span><span style="font-family: courier; font-size: medium; white-space: normal;">-f</span><span style="font-family: Times New Roman; font-size: medium; white-space: normal;"> makes it not ask if you're really sure about deleting stuff.</span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium; white-space: normal;"><br /></span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium; white-space: normal;"><br /></span></code></pre><p><span style="font-size: medium;"><b>Adding a remote git repository, </b><br /><br /><span style="font-family: courier;">git remote add origin YOUR_GIT_REPOSITORY_HTTPS_LINK</span></span></p><p><span style="font-size: medium;"> To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A remote name, for example, origin.</span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-family: courier; font-size: medium;">git remote -v</span></p><p><span style="font-size: medium;"><span>This code will help us to knew did the previous code worked correctly, Is the </span><span><span style="font-family: courier;">fetch </span>and </span><span><span style="font-family: courier;">push </span>URL is applied correctly.</span></span></p><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><b>Switch Branch / Create New Branch </b></span></code></p><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-family: courier;"><br /></span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: courier;">git checkout BranchName</span></code></span></p><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: courier;"><br /></span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: times;">This comment will switch </span><span style="font-family: courier;">BranchName </span><span style="font-family: times;">branch which is already exist in the repository.</span></code></span></p><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman; font-size: medium;"><br /></span></code></p><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;"><br />
</span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;">
</span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: courier;">git checkout -b NewBranchName</span></code></span></p><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: courier;"><br /></span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;">
</span></code><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;">This code will create a new branch </span><span style="font-family: courier;">NewBranchName </span><span style="font-family: Times New Roman;">and switch to it at the same time.</span></code></span></p><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-family: courier; font-size: medium;">git branch –show-current</span></code></p><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><p style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); vertical-align: baseline; white-space: normal; width: auto;"><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;">This code will show you the current branch.</span></code></span></p><div><span style="font-size: medium;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: Times New Roman;"><br /></span></code></span></div></code></pre><pre style="border-radius: var(--br-md); border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: var(--ff-mono); font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: var(--lh-md); margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: var(--su12); text-align: left; vertical-align: baseline; width: auto;"><code style="border-radius: 0px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: var(--ff-mono); font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-size: medium;"><br /></span></code></pre><p style="text-align: left;"><span style="font-size: medium;"><b>Step 3</b></span></p><p><span style="font-size: medium;">Adding the files or changes to the repository, <br /><br /><span style="font-family: courier;">git add --all</span></span></p><p><span style="font-size: medium;"> If this code is not working with you you can try the below code also, </span></p><p><span style="font-family: courier; font-size: medium;">git add -A</span></p><p><span style="font-size: medium;"> If you are in any subdirectory of the working directory, <span style="font-family: courier;">git add -A</span> will add all files from the entire working directory, and git add . will add files from your current directory.</span></p><p><span style="font-family: courier; font-size: medium;">git add .</span></p><p><span style="font-size: medium;"><span> </span><span>If you are in any subdirectory of the working directory,</span><span> </span><span style="font-family: courier;">git add .</span><span> will add new files and modifications, without deletions (on the current directory and its subdirectories).</span></span></p><p><span style="font-family: courier; font-size: medium;">git add -u</span></p><p><span style="font-size: medium;"><span> If you are in any subdirectory of the working directory, </span><span style="font-family: courier;">git add -u</span><span> will add stages modifications and deletions, without new files.</span></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><b>Step 4</b></span></p><p><span style="font-size: medium;">Moves changes from the working directory to the </span><span style="font-size: medium;"><span>local repository</span><span>, </span></span></p><div><div><span style="font-family: courier; font-size: medium;">git commit -m "YOUR_COMMIT_MESSAGE"</span></div></div><div><span style="font-size: medium;"><br /></span></div><p><span style="font-size: medium;"> This command takes the staged snapshot and commits it to the project history. My passing the Message you can Identify the changes we made on that commit.</span></p><p><span style="font-size: medium;"><br /></span></p><p><b><span style="font-size: medium;">Step 5</span></b></p><p><span style="font-size: medium;">Upload local repository content to a remote repository, </span></p><p><span style="font-family: courier; font-size: medium;">git push origin master --force</span></p><p><span style="font-size: medium;"><span> This command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote </span><span>repository</span><span>.</span></span></p><p><span style="font-size: medium;"><span style="font-family: courier;">--force </span><span>This option overrides the “fast forward” restriction and matches our local branch to the remote branch.</span></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-698902250717345812021-03-28T12:31:00.001+05:302021-03-28T12:31:23.337+05:30Draw direction and by parsing direction API response<p> <span style="background-color: white;"><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px;">I want to draw direction with waypoints based on the result of google direction API, From this URL I want to parse the response and draw the route on the google map.</span><br /><br /><br /><span style="font-size: 15px;">I need to draw direction from <i>10.1849092, 76.3753045 </i> to 10.3080271 , 76.3336779 </span></span></span></p><p><span style="background-color: white;"><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px;">also I have some waypoints(this means need to reach destination through that points), Here is my waypoints <br /><i>10.2269749, 76.3750218<br />10.2632015, 76.3492569<br />10.2834370, 76.3420206</i><br /></span><br />For this first we need to generate the request URL, Here is the format</span></span></p><pre class="notranslate" dir="ltr" is-upgraded="" style="box-sizing: inherit; color: #37474f; direction: ltr !important; font-family: "Roboto Mono", monospace; font-size: 14px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: 20px; margin-bottom: 0px; margin-top: 0px; overflow-x: auto; padding: var(--devsite-code-padding,24px); position: relative;"><span class="pln" style="box-sizing: inherit;">https</span><span class="pun" style="box-sizing: inherit;">:</span><span class="com" style="box-sizing: inherit; color: #d81b60;">//maps.googleapis.com/maps/api/directions/json?</span><span class="pln" style="box-sizing: inherit;"><br style="box-sizing: inherit;" />origin</span><span class="pun" style="box-sizing: inherit;">=</span><span class="typ" style="box-sizing: inherit; color: #9c27b0;">Toronto</span><span class="pun" style="box-sizing: inherit;">&</span><span class="pln" style="box-sizing: inherit;">destination</span><span class="pun" style="box-sizing: inherit;">=</span><span class="typ" style="box-sizing: inherit; color: #9c27b0;">Montreal</span><span class="pln" style="box-sizing: inherit;"><br style="box-sizing: inherit;" /></span><span class="pun" style="box-sizing: inherit;">&</span><span class="pln" style="box-sizing: inherit;">key</span><span class="pun" style="box-sizing: inherit;">=</span><span style="box-sizing: inherit;"><devsite-credentials-dialog class="" credentials-loading="" data-modal-dialog-id="api-key-credentials-dialog" data-title="Click to insert your API key" projects-loading="" style="border: 1px dashed rgb(233, 30, 99); box-sizing: inherit; cursor: pointer; padding: 2px;" type="api_key">YOUR_API_KEY</devsite-credentials-dialog></span></pre><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;">This is is the format of URL without passing waypoints.</span></p><pre class="notranslate" dir="ltr" is-upgraded="" style="box-sizing: inherit; color: #37474f; direction: ltr !important; font-family: "Roboto Mono", monospace; font-size: 14px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: 20px; margin-bottom: 0px; margin-top: 0px; overflow-x: auto; padding: var(--devsite-code-padding,24px); position: relative;"><span class="pln" style="box-sizing: inherit;">https</span><span class="pun" style="box-sizing: inherit;">:</span><span class="com" style="box-sizing: inherit; color: #d81b60;">//maps.googleapis.com/maps/api/directions/json?</span><span class="pln" style="box-sizing: inherit;"><br style="box-sizing: inherit;" />origin</span><span class="pun" style="box-sizing: inherit;">=</span><span class="typ" style="box-sizing: inherit; color: #9c27b0;">Boston</span><span class="pun" style="box-sizing: inherit;">,</span><span class="pln" style="box-sizing: inherit;">MA</span><span class="pun" style="box-sizing: inherit;">&</span><span class="pln" style="box-sizing: inherit;">destination</span><span class="pun" style="box-sizing: inherit;">=</span><span class="typ" style="box-sizing: inherit; color: #9c27b0;">Concord</span><span class="pun" style="box-sizing: inherit;">,</span><span class="pln" style="box-sizing: inherit;">MA<br style="box-sizing: inherit;" /></span><devsite-var ready="" scope="&waypoints=via:Charlestown,MA|via:Lexington,MA&departure_time=now" style="box-sizing: inherit; position: relative; white-space: nowrap;" tabindex="0"><span class="devsite-var-wrapper" style="box-sizing: inherit;"><var data-title="Edit &waypoints=via:Charlestown,MA|via:Lexington,MA&departure_time=now" is-upgraded="" spellcheck="false" style="-webkit-font-smoothing: auto; border-bottom: 1px dotted var(--devsite-var-color,#ec407a); box-sizing: inherit; color: var(--devsite-var-color,#ec407a); cursor: pointer; font-weight: 700;">&waypoints=via:Charlestown,MA|via:Lexington,MA &departure_time=now<a class="material-icons devsite-icon-edit" role="button" style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; box-sizing: inherit; color: var(--devsite-var-color,#ec407a); cursor: default; direction: ltr; display: inline-block; font-family: "Material Icons"; font-feature-settings: "liga"; font-size: inherit; font-style: normal; font-weight: normal; height: inherit; line-height: 1; margin: 0px 0px 2px 5px; min-width: inherit; overflow-wrap: normal; padding: 0px; pointer-events: none; vertical-align: middle;" tabindex="-1"></a></var></span></devsite-var><span class="pln" style="box-sizing: inherit;"><br style="box-sizing: inherit;" /></span><span class="pun" style="box-sizing: inherit;">&</span><span class="pln" style="box-sizing: inherit;">key</span><span class="pun" style="box-sizing: inherit;">=</span><span style="box-sizing: inherit;"><devsite-credentials-dialog class="" credentials-loading="" data-modal-dialog-id="api-key-credentials-dialog" data-title="Click to insert your API key" projects-loading="" style="border: 1px dashed rgb(233, 30, 99); box-sizing: inherit; cursor: pointer; padding: 2px;" type="api_key">YOUR_API_KEY</devsite-credentials-dialog></span></pre><p><span style="background-color: white;"><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px;">Here is the final URL from my origin , destination and waypoint locations,</span></span></span></p><p><i><span class="pln" style="box-sizing: inherit; color: #37474f; font-family: "Roboto Mono", monospace; font-size: 14px;">https</span><span class="pun" style="box-sizing: inherit; color: #37474f; font-family: "Roboto Mono", monospace; font-size: 14px;">:</span><span class="com" style="box-sizing: inherit; color: #d81b60; font-family: "Roboto Mono", monospace; font-size: 14px;">//maps.googleapis.com/maps/api/directions/json?origin=0.1849092,76.3753045&destination=10.3080271 ,76.3336779&waypoints=10.2269749,76.3750218|10.2632015, 76.3492569|10.2834370, 76.3420206&key=YOUR_API_KEY</span></i></p><p><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px;"></span></span></p><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"><a href="https://developers.google.com/maps/documentation/directions/start" target="_blank">Here will be the response format from Google Direction Api</a>,<br /></span></p><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">From this Google Direction Api response </span><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">we use the value </span><span style="color: #e06666; font-family: courier;"><code style="border-radius: 3px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">"</code><code style="border-radius: 3px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">routes"</code><span style="background-color: white;"> -> </span><code style="border-radius: 3px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">"overview_polyline"</code><span style="background-color: white;">-> </span><code style="border-radius: 3px; border: 0px; box-sizing: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">"points"</code></span><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"> to drew the </span><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">direction,</span><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"> </span></p><pre class="default s-code-block hljs perl" style="border-radius: 5px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="color: #e06666;"><span class="hljs-string" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"overview_polyline"</span> : {
<span class="hljs-string" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"points"</span> : <span class="hljs-string" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">"yhd}@madqMD]CiA]eF[_HMaCKg@]]SUIUMq@Cw@Cy@M_AUkAsCqAuA{@Y]w@oAeA}BuAaDaAkCa<span class="hljs-subst" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">@{@g@<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">y</span>@eCsCi@g@kAs@o@OuBWwCUs@As@@cBJoAVaI<span class="hljs-string" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">`CiC|@iD~@mBf@gFjB{C`</span>AoBl@eEnA_HxBYHiEj@wDd@uEv@aEv@{BNkA@sACwEGy@?oAHiBVs@P}</span>@\\aEpBuHpDuG~CqDbB{FpCeDrAyCv@kIlBwLrCkCl@qF~@cDf@yEx@}BZ[DCSdBWzAWCMvCg@rF_AhAQC_@S}Ak@oAs@qAKg@EiCOuAEi@PqCj@}DBc@?_EMwB?{AJoAAoAgAEoE?aCEkACHv@AZm@XmA`@m@H@FF~@KrAYhAe@xCIhFCxC?jBDf@Bj@ZzDFn@Nt@TlBB~@N`BDd@@Jn@IlCe@t@MBLpKaB`Eu@pEeAxKoCvJ{BvBq@tAm@JT]L}B|@yCv@kIlBwLrCkCl@qF~@cDf@yEx@}BZmCb@gMtB{GbAiHhAeBR_CJaA@mACuG[gBGiABcDHwGRqQl@yANaBZ{Bl@qB~@qAv@aCjByCfCmA|@uBhAwCvAkAd@kBl@sDhA}HbCyH|BeBx@oErB}Br@sFlAoAZu@Zu@\\}A`AwAdAa@^m@n@i@x@i@dAe@tAkBpGoAjDgCtGqFfOyErL{@jBsAbCmAtBo@x@s@t@i@f@mE|C?HEJ_DjC{CbCmD`DMLFFLLv@g@f@WTE^AxBJrCRv@L\\Lf@^d@p@HZH\\Rc@L[@@PD`@J~@HR?@IBET@l@BBG@OH[Fc@dADDAE@eAEGb@IZANCFm@CUACDAHS?_AIs@QAAMZSb@I]I[e@q@g@_@]Mw@MsCSyBK_@@UDg@Vw@f@_@e@oAdAmAp@gChAwAb@qHxBsHfCcAd@sBjAoHrFgAt@}@j@w@b@cBl@eBd@aANuAJ}EBqCEuCFoD`@s@NcAXSHEHQNwG|BXbDYcDwDxAOFEOiAb@cBt@mBrA}@~@cAvAkApB_AfCuAvDy@zBi@tAq@nAk@v@g@h@aAp@wAr@}@ZaATyB\\w@B}@@wACs@EoAOuBk@cM{DaAQwAImA?uIZgDBwDCsA@_AF}@LkAX{@ZcCdA{JhEyAl@cEnBkCvAsEnCiBnAcEdCaDtB{D`CaClA_GlCsErBJPkHzCwSjJCk@`GmCzG}CtHcDzMeGlEwBlMcIvA{@MItCkB"</span>
}</span></code></pre><p><br /></p><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">We are using </span><code style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">drawDirectionToStop()</code><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"> to draw the route, I am passing to the value of "points" </span><code style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">decodeOverviewPolyLinePonts()</code><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"> for parse the response, Here is the functions</span></p><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"><br /></span></p><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-comment" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b>//This function is to parse the value of "points"</b></span><span style="font-weight: inherit;">
</span><span class="hljs-function" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span> List<LatLng> <span class="hljs-title" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">decodeOverviewPolyLinePonts</span>(<span class="hljs-params" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">String encoded</span>)</span><span style="font-weight: inherit;"> {
List<LatLng> poly = </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span style="font-weight: inherit;"> ArrayList<LatLng>();
</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">if</span><span style="font-weight: inherit;"> (encoded != </span><span class="hljs-literal" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">null</span><span style="font-weight: inherit;"> && !encoded.isEmpty() && encoded.trim().length() > </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">) {
</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span style="font-weight: inherit;"> index = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">, len = encoded.length();
</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span style="font-weight: inherit;"> lat = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">, lng = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">;
</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">while</span><span style="font-weight: inherit;"> (index < len) {
</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span style="font-weight: inherit;"> b, shift = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">, result = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">;
</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span><span style="font-weight: inherit;"> {
b = encoded.charAt(index++) - </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">63</span><span style="font-weight: inherit;">;
result |= (b & </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0x1f</span><span style="font-weight: inherit;">) << shift;
shift += </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span style="font-weight: inherit;">;
} </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">while</span><span style="font-weight: inherit;"> (b >= </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0x20</span><span style="font-weight: inherit;">);
</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span style="font-weight: inherit;"> dlat = ((result & </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">) != </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;"> ? ~(result >> </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">) : (result >> </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">));
lat += dlat;
shift = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">;
result = </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;">;
</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">do</span><span style="font-weight: inherit;"> {
b = encoded.charAt(index++) - </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">63</span><span style="font-weight: inherit;">;
result |= (b & </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0x1f</span><span style="font-weight: inherit;">) << shift;
shift += </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span style="font-weight: inherit;">;
} </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">while</span><span style="font-weight: inherit;"> (b >= </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0x20</span><span style="font-weight: inherit;">);
</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span style="font-weight: inherit;"> dlng = ((result & </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">) != </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span style="font-weight: inherit;"> ? ~(result >> </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">) : (result >> </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span style="font-weight: inherit;">));
lng += dlng;
LatLng p = </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span style="font-weight: inherit;"> LatLng((((</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">double</span><span style="font-weight: inherit;">) lat / </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1E5</span><span style="font-weight: inherit;">)),
(((</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">double</span><span style="font-weight: inherit;">) lng / </span><span class="hljs-number" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">1E5</span><span style="font-weight: inherit;">)));
poly.</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">add</span><span style="font-weight: inherit;">(p);
}
}
</span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">return</span><span style="font-weight: inherit;"> poly;
}</span></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span style="font-weight: inherit;"><br /></span></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><span class="hljs-function" style="border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">private</span> <span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">void</span> <span class="hljs-title" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">drawDirectionToStop</span>(<span class="hljs-params" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">DirectionData.Overview_polyline overviewPolyline</span>)</span><span style="color: var(--highlight-color); font-family: inherit; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"> {</span></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"> <span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">if</span> (overviewPolyline != <span class="hljs-literal" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">null</span>) {
List<LatLng> polyz = decodeOverviewPolyLinePonts(overviewPolyline.getPoints());
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">if</span> (polyz != <span class="hljs-literal" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">null</span>) {
PolylineOptions lineOptions = <span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span> PolylineOptions();
lineOptions.addAll(polyz);
lineOptions.width(<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span>);
lineOptions.color(ContextCompat.getColor(getActivity(), R.color.colorAccent));
mGoogleMap.addPolyline(lineOptions);
}
}
}</code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><pre class="default s-code-block hljs csharp" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><br /></code></pre><p><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"><br /></span></p><div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-52624253468202145592019-05-01T14:18:00.001+05:302019-05-01T14:18:47.017+05:30How to save an Android Activity state using save instance state<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
On this blog am going to explain how to save activity state when the activity is destroyed, I have been working on the android SDK platform, and it is a little unclear how to save an application's state.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
You're explicitly destroying your activity such as by pressing back. Actually, the scenario in which this <span style="font-family: "courier new" , "courier" , monospace;">savedInstanceState </span>is used, is when Android destroys your activity for recreation. For example, If you change the language of your phone while the activity was running (and so different resources from your project need to be loaded). Another very common scenario is when you rotate your phone to the side so that the activity is recreated and displayed in landscape. You can use this technique to store instance values for your application (selections, unsaved text, etc.).</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
To overcome this issue you need to override <span style="font-family: "courier new" , "courier" , monospace;">onSaveInstanceState(Bundle savedInstanceState)</span> and write the application state values you want to change to the Bundle parameter, This bundle will be passed to <span style="font-family: "courier new" , "courier" , monospace;">onCreate </span>if the process is killed and restarted.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">@Override</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public void onSaveInstanceState(Bundle savedInstanceState) {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> super.onSaveInstanceState(savedInstanceState);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> savedInstanceState.putBoolean("KeyBoolean", true);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> savedInstanceState.putDouble("KeyDouble", 1.9);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> savedInstanceState.putInt("KeyInt", 1);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> savedInstanceState.putString("KeyString", "Welcome back to Android");</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
The Bundle is essentially a way of storing the values as Name-Value Pair, and it will get passed in to <span style="font-family: "courier new" , "courier" , monospace;">onCreate() </span>and also <span style="font-family: "courier new" , "courier" , monospace;"> onRestoreInstanceState(Bundle savedInstanceState)</span> where you would then extract the values like this,</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">@Override</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public void onRestoreInstanceState(Bundle savedInstanceState) {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> super.onRestoreInstanceState(savedInstanceState);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> boolean myBoolean = savedInstanceState.getBoolean("KeyBoolean");</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> double myDouble = savedInstanceState.getDouble("KeyDouble");</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> int myInt = savedInstanceState.getInt("KeyInt");</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> String myString = savedInstanceState.getString("KeyString");</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com6tag:blogger.com,1999:blog-7136242653797922563.post-69687651560274244122019-04-29T14:14:00.000+05:302019-04-29T14:14:08.005+05:30Location Permission checking in Android <div dir="ltr" style="text-align: left;" trbidi="on">
<b> O</b>n this Blog I am going to explain about how to integrate Location Permission checking on your android application. Here is the steps,<br /><br /><br /><b><u><br />Step 1 </u></b><br />
First we need to add the permission for Location Access. For this we need to add the following permissions on our applications <span style="font-family: Courier New, Courier, monospace;">AndroidManifest.xml</span> file Here is the permissions we need to add, <br />
<br />
<span style="font-family: Courier New, Courier, monospace;"><uses-permission android:name="android.permission.INTERNET" /></span><br />
<span style="font-family: Courier New, Courier, monospace;"><permission android:name="android.permission.ACCESS_FINE_LOCATION" /></span><br />
<span style="font-family: Courier New, Courier, monospace;"><uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /></span><br />
<span style="font-family: Courier New, Courier, monospace;"><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /></span><br />
<br />
<b><u>Step 2</u></b><br />
<br />
New we are going to make a function <span style="font-family: Courier New, Courier, monospace;"><i>checkLocationPermission()</i> </span>to check the location permission, This function will return a Boolean value based on the location permission. If the user granted the permission it will return <span style="font-family: Courier New, Courier, monospace;">true </span>otherwise it will return <span style="font-family: Courier New, Courier, monospace;">false </span>and also it will request for the permission. Here is the function,<br /><span style="font-family: "Courier New", Courier, monospace;"><br />LOCATION_REQUEST_CODE </span><span style="font-family: inherit;">is one </span>constant<span style="font-family: inherit;"> variable, On your application you can define your own value, I defiled this variable on my </span><span style="font-family: "Courier New", Courier, monospace;">Constants.java</span><span style="font-family: inherit;"> Class file.</span><br /><br /><span style="font-family: Courier New, Courier, monospace;">public static final int LOCATION_REQUEST_CODE = 4505;</span><br />
<br />
<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">private boolean checkLocationPermission() {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> boolean permission = false;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> try {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> if (ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> permission = true;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> } else</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ActivityCompat.requestPermissions(HomeActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_REQUEST_CODE);</span><br />
<span style="font-family: Courier New, Courier, monospace;"> } catch (Exception e) {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="font-family: Courier New, Courier, monospace;"> return permission;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<b><u>Step 3</u></b><br />
<br />
Now we need to check the permission on the <span style="font-family: Courier New, Courier, monospace;">onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)</span> function. Which is a default function in Activity class. Here is the Method,<br /><br />
<span style="font-family: Courier New, Courier, monospace;">@Override</span><br />
<span style="font-family: Courier New, Courier, monospace;">public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {</span><br />
<span style="font-family: Courier New, Courier, monospace;">switch (requestCode) {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> case Constants.LOCATION_REQUEST_CODE:</span><br />
<span style="font-family: Courier New, Courier, monospace;"> if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {</span><br />
<span style="font-family: Courier New, Courier, monospace;"> // permission granted, Add your code after giving permission</span><br />
<span style="font-family: Courier New, Courier, monospace;"> }else{</span><br />
<span style="font-family: Courier New, Courier, monospace;"> //show some warning, or </span><span style="font-family: "Courier New", Courier, monospace;">Add your code</span><span style="font-family: "Courier New", Courier, monospace;"> permission not granded</span><br />
<span style="font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="font-family: Courier New, Courier, monospace;"> break;</span><br />
<span style="font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="font-family: Courier New, Courier, monospace;">} </span><br />
<br /><br /><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;">---------------</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Thanks For Reading, Wish you a Happy Coding....</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-3628939227075611172019-04-14T14:58:00.002+05:302019-04-29T14:21:34.523+05:30Android Location Tracking In Using Service<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<br />
<b> O</b>n this Blog I am going to explain about location update when the applications is running on background. In some cases we need to send location updated to our server when the application is on background. For this I am using <i><span style="font-family: "courier new" , "courier" , monospace;">startForeground()</span></i> function. </div>
<br />
Here <span style="font-family: "courier new" , "courier" , monospace;">LocationService.java</span> is my <span style="font-family: "courier new" , "courier" , monospace;">Service</span> Class which I used to get the location update on Background.<br />
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><b><u>LocationService.java</u></b></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">public class LocationService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> GoogleApiClient.OnConnectionFailedListener {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> LocationRequest mLocationRequest;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> GoogleApiClient mGoogleApiClient;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> private static final String TAG = "LocationService";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> private static final int LOCATION_INTERVAL = 10000;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> private static final int FASTEST_INTERVAL = 10000;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onCreate() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> if (isGooglePlayServicesAvailable()) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mLocationRequest = new LocationRequest();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mLocationRequest.setInterval(LOCATION_INTERVAL);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mLocationRequest.setFastestInterval(FASTEST_INTERVAL);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //mLocationRequest.setSmallestDisplacement(10.0f); /* min dist for location change, here it is 10 meter */</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mGoogleApiClient = new GoogleApiClient.Builder(this)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .addApi(LocationServices.API)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .addConnectionCallbacks(this)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .addOnConnectionFailedListener(this)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .build();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> mGoogleApiClient.connect();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> //Check Google play is available or not</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> private boolean isGooglePlayServicesAvailable() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return ConnectionResult.SUCCESS == status;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onConnected(Bundle bundle) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.d(TAG, "Connected");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> startLocationUpdates();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onConnectionSuspended(int i) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.d(TAG, "Connection suspended");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> protected void startLocationUpdates() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.d(TAG, "Start Location Updates");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (permissionCheck == PackageManager.PERMISSION_GRANTED) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } catch (IllegalStateException e) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onLocationChanged(Location location) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (location != null) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.d(TAG, "Location Changed: " + location.getLatitude() + "," + location.getLongitude());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //Send Broadcast</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Intent intent = new Intent(getPackageName());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> intent.putExtra(Constants.LocationUpdated, location);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> sendBroadcast(intent);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> /* LocationListener[] mLocationListeners = new LocationListener[]{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> new LocationListener(LocationManager.GPS_PROVIDER),</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> new LocationListener(LocationManager.NETWORK_PROVIDER)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> };*/</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public IBinder onBind(Intent arg0) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return null;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public int onStartCommand(Intent intent, int flags, int startId) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> super.onStartCommand(intent, flags, startId);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Notification.Builder builder = new Notification.Builder(this, Constants.LOCATION_CHANNEL_ID)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setContentTitle(getString(R.string.app_name))</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setContentText(Constants.LocationChange)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setAutoCancel(true);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> Notification notification = builder.build();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> startForeground(1, notification);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } else {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NotificationCompat.Builder builder = new NotificationCompat.Builder(this)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setContentTitle(getString(R.string.app_name))</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setContentText(Constants.LocationChange)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setPriority(NotificationCompat.PRIORITY_DEFAULT)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> .setAutoCancel(true);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Notification notification = builder.build();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> startForeground(1, notification);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return START_NOT_STICKY;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.d(TAG, "onConnectionFailed:: " + connectionResult.getErrorMessage());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Once you create the Service Class you need to add this service class to</span><span style="font-family: "courier new" , "courier" , monospace;"> AndroidManifest.xml </span><span style="font-family: inherit;">file, Also You need to add the Location permissions if you didn't added on your Manifest file, Here is my Manifest file,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<b><u><span style="font-family: inherit;">AndroidManifest.xml </span></u></b><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><?xml version="1.0" encoding="utf-8"?></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><manifest xmlns:android="http://schemas.android.com/apk/res/android"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> package="Your Package Here "></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <uses-permission android:name="android.permission.INTERNET" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <permission android:name="android.permission.ACCESS_FINE_LOCATION" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <uses-permission android:name="android.permission.WAKE_LOCK" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <application</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:name=".AppController"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:allowBackup="true"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:hardwareAccelerated="false"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:icon="@drawable/logo_squre"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:label="@string/app_name"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:largeHeap="true"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:launchMode="singleInstance"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:roundIcon="@drawable/logo_circle"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:supportsRtl="true"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:theme="@style/AppTheme"></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <activity</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:name=".HomeActivity"</span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> android:screenOrientation="portrait"></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <intent-filter></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <action android:name="android.intent.action.MAIN" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <category android:name="android.intent.category.LAUNCHER" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> </intent-filter></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> </activity></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> <service android:name=".service.LocationService" /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"> </application></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div style="background-color: white;">
<span style="font-family: "courier new" , "courier" , monospace;"></manifest></span></div>
<div style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: inherit;">On the next step you need to create the Notification channel on your application class. We are using </span><span style="font-family: "courier new" , "courier" , monospace;">createNotificationChannel()</span><span style="font-family: inherit;">method to Create Notification channel, Here us my Application class,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><b><u>AppController.java</u></b></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">public class AppController extends Application {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public static final String TAG = AppController.class.getSimpleName();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onCreate() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> super.onCreate();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> mInstance = this;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> createNotificationChannel();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected void attachBaseContext(Context base) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> super.attachBaseContext(base);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> MultiDex.install(this);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public static synchronized AppController getInstance() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return mInstance;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> private void createNotificationChannel() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NotificationChannel serviceChannel = new NotificationChannel(</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Constants.LOCATION_CHANNEL_ID,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> getString(R.string.app_name),</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NotificationManager.IMPORTANCE_DEFAULT</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> );</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> NotificationManager manager = getSystemService(NotificationManager.class);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> manager.createNotificationChannel(serviceChannel);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: inherit;">Next step is to integrate the service class on our Activity class. To handle the broadcast we need to register the Broadcast Receiver on the Activity class. Here I am going to use </span><span style="font-family: "courier new" , "courier" , monospace;">createBroadcastReceiver()</span> method <span style="font-family: inherit;">for Register the Broadcast Receiver.</span><span style="font-family: inherit;"> This method will Invoke in </span><span style="font-family: "courier new" , "courier" , monospace;">onCreate(Bundle savedInstanceState)</span> method like, Here is my method for register the receiver,<br />
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected void onCreate(Bundle savedInstanceState) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> super.onCreate(savedInstanceState);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> setContentView(R.layout.activity_home);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> //To Register the Receiver </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> broadcastReceiver = createBroadcastReceiver();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> registerReceiver(broadcastReceiver, new IntentFilter(getPackageName()));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> //Method for Register Broadcast receiver </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> private BroadcastReceiver createBroadcastReceiver() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return new BroadcastReceiver() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onReceive(Context context, Intent intent) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (intent != null) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (intent.hasExtra(Constants.LocationUpdated)){</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Location location = AppController.getInstance().getCurrentLocation();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (location != null) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.e("Broadcast: ", "New Location: " + location.getLatitude() + "," + location.getLongitude());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> };</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<br />
<br />
To start and stop the location location service am using <span style="font-family: "courier new" , "courier" , monospace;">startLocationService()</span> and <span style="font-family: "courier new" , "courier" , monospace;">stopLocationService() </span>method.<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> //Service Intent Decleration </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Intent locationServiceIntent;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> //Start Location Service</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void startLocationService() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (locationServiceIntent == null)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> locationServiceIntent = new Intent(this, LocationService.class);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (!Helper.isServiceRunning(HomeActivity.this, LocationService.class)) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> startForegroundService(locationServiceIntent);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } else {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> startService(locationServiceIntent);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> //Stop Location service</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void stopLocationService() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (locationServiceIntent != null && Helper.isServiceRunning(HomeActivity.this, LocationService.class))</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> stopService(locationServiceIntent);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;">---------------</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Thanks For Reading, Wish you a Happy Coding....</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-32698309875495550612019-04-04T13:57:00.000+05:302019-04-04T13:57:08.655+05:30How to get Notification on Back Stack Change On android<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">O</span>n this blog I am going to explain How to we can get notification on the fragment change on our Activity Class. In Some Cases we need to get the notification when the fragment is changed. We are using <i><span style="font-family: "courier new" , "courier" , monospace;">OnBackStackChangedListener </span></i>for this. Here is the way we are doing it,<br />
<br />
<br />
First we need to add the Listener on <i><span style="font-family: "courier new" , "courier" , monospace;">onCreate</span> </i>method of Activity Class . For this I have Created a function <i><span style="font-family: "courier new" , "courier" , monospace;">addPopBackStackListener()</span></i>. Here is the way we initializing the Listener<br />
<br />
<i><span style="font-family: "courier new" , "courier" , monospace;">@Override</span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"> protected void onCreate(Bundle savedInstanceState) {</span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"> super.onCreate(savedInstanceState);</span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"> setContentView(R.layout.activity_home);</span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"> </span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"> addPopBackStackListener();</span></i><br />
<i><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></i>
<i><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></i>
<i><span style="font-family: "courier new" , "courier" , monospace;"> }</span></i><br />
<br />
Here is my <span style="font-family: "courier new" , "courier" , monospace;">addPopBackStackListener()</span> function,<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">private void addPopBackStackListener() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> getSupportFragmentManager().addOnBackStackChangedListener(</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> new FragmentManager.OnBackStackChangedListener() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onBackStackChanged() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> FragmentManager fragmentManager = getSupportFragmentManager();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (fragmentManager != null && fragmentManager.getFragments() != null) { </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="white-space: pre;"> </span>int count = fragmentManager.getFragments().size();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (count > 0) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Fragment fragment = fragmentManager.getFragments().get(count- 1);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Log.v(TAG, "Class: " + fragment.getClass().getSimpleName());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: inherit;"> /* Using instanceof keyword you can find out which fragment is loaded*/</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (fragment instanceof HomeFragment) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: inherit;">/*Add your code to Perform Action when Home Fragment is Loaded*/ </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> });</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">---------------</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com1tag:blogger.com,1999:blog-7136242653797922563.post-64449161201710134292018-07-10T12:51:00.000+05:302019-04-04T13:54:39.554+05:30How to make an API call in android Kotlin using retrofit library<div dir="ltr" style="text-align: left;" trbidi="on">
On this blog I am going to explain How to make an API call with the Help of <i><b>Retrofit </b></i>Library on Kotlin. Here is the steps to integrated API call using retrofit library in android Kotlin. With the help of and Interface class I am calling this API.<br />
<br />
<br />
<span style="font-family: inherit;">First you need to add the dependency library on your app level <i>build.gradle</i> file.</span><br />
<br />
<pre style="background-color: white; font-family: "courier new";">compile 'com.google.code.gson:gson:2.8.1'</pre>
<pre style="background-color: white; font-family: "courier new";">compile 'com.squareup.okhttp3:okhttp:3.4.1'</pre>
<pre style="background-color: white; font-family: "courier new";">compile('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'</pre>
<pre style="background-color: white; font-family: "courier new";">}
compile 'com.squareup.retrofit2:converter-gson:2.2.0'</pre>
<br />
<br />
I am having a Constants.java class there I defiled my Base Url of my API like,<br />
<br />
<pre style="background-color: white; font-family: "courier new";"></pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "Courier New";">class Constants {
companion object AppConstants {
const val BaseUrl = "<a href="http://shidhin.net/" style="font-family: "courier new";">http://api.shidhin.net/</a>";
}
}</pre>
</pre>
<br />
<br />
Next Step, I Created a new class <i><b>ApiClient.java</b></i> class, which we are used to invoke the API Call. Here is My ApiClient.java class,<br />
<br />
<br />
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "Courier New";">class ApiClient {
companion object {
fun create(): ApiInterface {
val TIMEOUT:Long = 2 * 60 * 1000
val retrofit = Retrofit.Builder()
.client(OkHttpClient().newBuilder().connectTimeout(TIMEOUT, TimeUnit.SECONDS).readTimeout(TIMEOUT, TimeUnit.SECONDS).writeTimeout(TIMEOUT, TimeUnit.SECONDS).build())
.<span style="background-color: #e4e4ff;">addConverterFactory</span>(GsonConverterFactory.create()).baseUrl(Constants.AppConstants.BaseUrl)
.build()
return retrofit.create(ApiInterface::class.<span style="font-style: italic;">java</span>)
}
}
val apiServices by <span style="font-style: italic;">lazy </span>{
ApiClient.create()
}
}</pre>
</pre>
<br />
<br />
Next Step, I Created a new <i>Interface </i><b><i>ApiInterface</i></b><i><b>.java</b></i> , which which will helps to API Call. On this Class we will define the API functions. On this I have POST method with Header value, Here is My ApiInterface.java class,<br />
<div>
<br /></div>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "Courier New";">interface ApiInterface {
@POST("api/shops/AddNewShopDetails")<pre style="font-family: "Courier New";"> @Headers("Content-Type: application/json")
fun addShopDetails(@Body body: JsonObject): Call<CommonServiceResponse></pre>
}</pre>
</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<br />
For the above API calls we used a class <span style="font-family: "courier new"; font-size: 9.6pt;">CommonServiceResponse</span> to parse the API response, Here is those classes, Based on the API response the structure of these class can be changed,<br />
<br />
<u><b><span style="font-family: "times" , "times new roman" , serif;">Class to parse </span></b></u><u><b><span style="font-family: "times" , "times new roman" , serif;"><i>addShopDetails</i></span></b></u><u><b><span style="font-family: "times" , "times new roman" , serif;"><i> </i>API</span></b></u><br />
<br />
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "Courier New";">class CommonServiceResponse {
@SerializedName("status")
public var status: Boolean? = null
@SerializedName("message")
public var message: String? = null
}</pre>
</pre>
<br />
Now we added all the Class files we needed to the API Integration. Now I will show you How to invoke API calls on our Application.<br />
<br />
<br />
<br />
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white;"><pre><u><b><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">API Call: </span><i>addShopDetails</i></span></b></u></pre>
</pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "courier new";"><pre style="font-family: "Courier New";">val request = JsonObject()
request.addProperty("token", token)
request.addProperty("shopName", shopName)
request.addProperty("shopAddress", shopAddress)
ApiClient().apiServices.addShopDetails(request).enqueue(object : Callback<CommonServiceResponse> {
override fun onResponse(call: Call<CommonServiceResponse>?, response: Response<CommonServiceResponse>?) {
if (response != null && <span style="background-color: #dbffdb;">response</span>.body() != null ) {
</pre>
<pre style="font-family: "Courier New";"><pre style="font-family: "courier new";"> //Here Comes the code to parse the result</pre>
}
}
override fun onFailure(call: Call<CommonServiceResponse>?, t: Throwable?) {
Log.v("ApiFailure", "api Call Failure")
}
})</pre>
</pre>
<span style="font-size: 9.6pt;">
</span></pre>
<pre style="background-color: white; font-family: "courier new";"><span style="font-size: 9.6pt;"><div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">
</span></div>
<div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">---------------</span></div>
<div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</span></pre>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-46855167593577492142018-07-09T13:12:00.006+05:302022-01-31T12:21:42.690+05:30How to make API call using Retrofit on Android java class file<div dir="ltr" style="text-align: left;" trbidi="on">
On this blog I am going to explain How to make an API call with the Help of <i><b>Retrofit </b></i>Library. Here is the steps to integrated API call using retrofit library in Java. With the help of and Interface class I am calling this API.<br />
<br />
<br />
<span style="font-family: inherit;">First you need to add the dependency library on your app level <i>build.gradle</i> file.</span><br />
<br />
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">compile 'com.google.code.gson:gson:2.8.1'</span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">compile 'com.squareup.okhttp3:okhttp:3.4.1'</span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">compile('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'</span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">}
compile 'com.squareup.retrofit2:converter-gson:2.2.0'</span></pre>
<br />
<br />
I am having a Constants.java class there I defiled my Base Url of my API like,<br />
<br />
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">public class Constants {
public static final String <span style="font-style: italic;">BaseUrl </span>= "<a href="http://shidhin.net/">http://api.shidhin.net/</a>"; <span style="font-style: italic;">
</span>}</span></pre>
<br />
<br />
Next Step, I Created a new class <i><b>ApiClient.java</b></i> class, which we are used to invoke the API Call. Here is My ApiClient.java class,<br />
<br />
<br />
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">public class ApiClient {
static OkHttpClient <span style="font-style: italic;">client </span>= new OkHttpClient.Builder()
.connectTimeout(50, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.writeTimeout(50, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.readTimeout(200, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.build();
static Retrofit <span style="font-style: italic;">retrofit </span>= new Retrofit.Builder()
.baseUrl(Constants.<span style="font-style: italic;">BaseUrl</span>)
.addConverterFactory(GsonConverterFactory.<span style="font-style: italic;">create</span>())
.client(<span style="font-style: italic;">client</span>)
.build();
static ApiInterface <span style="font-style: italic;">apiService </span>= ApiClient.<span style="font-style: italic;">getClient</span>().create(ApiInterface.class);
private static Retrofit getClient() {
if (<span style="font-style: italic;">retrofit </span>== null) {
<span style="font-style: italic;">retrofit </span>= new Retrofit.Builder()
.baseUrl(Constants.<span style="font-style: italic;">BaseUrl</span>)
.addConverterFactory(GsonConverterFactory.<span style="font-style: italic;">create</span>())
.build();
}
return <span style="font-style: italic;">retrofit</span>;
}
}</span></pre>
<br />
<br />
Next Step, I Created a new <i>Interface </i><b><i>ApiInterface</i></b><i><b>.java</b></i> , which which will helps to API Call. On this Class we will define the API functions. On this I have one GET method and two POST methods(One is with Header and another without Header), Here is My ApiInterface.java class,<br />
<div>
<br />
<br /></div>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">public interface ApiInterface {
</span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"> <span style="color: olive;">@GET</span>("api/CountryList")
Call<CountryListResponse> getCountryList();
<span style="color: olive;">@FormUrlEncoded</span><span style="color: olive;"> </span></span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"> @POST("api/Login")
Call<LoginResponse> login(@Field("username") String username, </span></pre><pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"> @Field("password") String password);
</span><pre><span style="color: olive; font-family: "courier new" , "courier" , monospace;"> </span></pre>
<pre><span style="font-family: "courier new" , "courier" , monospace;"><span style="color: olive;"> @FormUrlEncoded</span><span style="color: olive;"> </span></span></pre>
<pre><span style="font-family: "courier new" , "courier" , monospace;"><span style="color: olive;"> @POST</span>("api/shops/AddNewShopDetails")
Call<CommonServiceResponse> addShopDetails(<span style="color: olive;">@Header</span>("Authorization") String authorization, </span></pre><pre><span style="font-family: "courier new" , "courier" , monospace;"><span style="color: olive;"> @Field</span>("name") String shopName, @Field("address") String shopAddress);</span></pre>
<span style="font-family: "courier new" , "courier" , monospace;">
}</span></pre>
<br />
On the above <b><i>ApiInterface</i></b><i><b>.java</b></i> class I added 3 functions 1 GET method and 2 POST method (1 Post method is without passing Header value and another method is by passing Header value). Here <span style="background-color: white; font-family: "courier new";">getCountryList</span> is a GET method, <span style="background-color: white; font-family: "courier new";">login </span>is a POST method without passing Header Value and is also a POST method and on this method I am passing the Header value.<br />
<br />
Now I need to add these functions on my <i><b>ApiClient.java</b></i> class. We can add the function Like,<br />
<br />
<br />
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"><pre style="font-family: "Courier New"; font-size: 9.6pt;"></pre>
</pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">public class ApiClient {
static OkHttpClient <span style="font-style: italic;">client </span>= new OkHttpClient.Builder()
.connectTimeout(50, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.writeTimeout(50, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.readTimeout(200, TimeUnit.<span style="font-style: italic;">SECONDS</span>)
.build();
static Retrofit <span style="font-style: italic;">retrofit </span>= new Retrofit.Builder()
.baseUrl(Constants.<span style="font-style: italic;">BaseUrl</span>)
.addConverterFactory(GsonConverterFactory.<span style="font-style: italic;">create</span>())
.client(<span style="font-style: italic;">client</span>)
.build();
static ApiInterface <span style="font-style: italic;">apiService </span>= ApiClient.<span style="font-style: italic;">getClient</span>().create(ApiInterface.class);
private static Retrofit getClient() {
if (<span style="font-style: italic;">retrofit </span>== null) {
<span style="font-style: italic;">retrofit </span>= new Retrofit.Builder()
.baseUrl(Constants.<span style="font-style: italic;">BaseUrl</span>)
.addConverterFactory(GsonConverterFactory.<span style="font-style: italic;">create</span>())
.build();
}
return <span style="font-style: italic;">retrofit</span>;
}</span></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"><pre><span style="font-family: "courier new" , "courier" , monospace;"> public static Call<CountryListResponse> getCountryList() {
Call<CountryListResponse> call = apiService.getCountryList();
return call;
}
public static Call<LoginResponse> login(String username, String password) {
Call<LoginResponse> call = apiService.login(username, password);
return call;
}</span></pre>
<span style="font-family: "courier new" , "courier" , monospace;">
</span><div style="white-space: normal;">
</div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span><pre><span style="font-family: "courier new" , "courier" , monospace;"> public static Call<CommonServiceResponse> addShopDetails(String token, String shopName, String shopAddress) {</span></pre><pre><span style="font-family: "courier new" , "courier" , monospace;"> Call<CommonServiceResponse> call = apiService.addShopDetails(token, shopName, shopAddress);
return call;
}</span></pre>
<span style="font-family: "courier new" , "courier" , monospace;">
}</span></pre>
<br />
For the above API calls we used 3 class (<span style="font-family: "courier new";">CountryListResponse, </span><span style="font-family: "courier new";">LoginResponse and </span><span style="font-family: "courier new";">CommonServiceResponse</span>) to parse the API response, Here is those classes, Based on the API response the structure of these class can be changed,<br />
<br />
<u><b><span style="font-family: "times" , "times new roman" , serif;">Class to parse <i>getCountryList </i>API</span></b></u><br />
<br />
<pre style="background-color: white; font-family: "courier new";">public class CountryListResponse {
@SerializedName("data")
private ArrayList<Country> data;
@SerializedName("count")
private int count;
@SerializedName("message")
private String message;</pre>
<pre style="background-color: white; font-family: "courier new";"></pre>
<pre style="background-color: white; font-family: "courier new";"> //Here you can add Getter and Setter for this class if you need</pre>
<pre style="background-color: white; font-family: "courier new";">}</pre>
<br />
<pre style="background-color: white; font-family: "courier new";">public class Country {
<span style="color: olive;">@SerializedName</span>("id")
private int id;
<span style="color: olive;">@SerializedName</span>("name")
private String name;</pre>
<pre style="background-color: white; font-family: "courier new";">}</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white;"><b><u><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">Class to parse </span><i>login </i><span style="white-space: normal;">API</span></span></u></b></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"><b style="font-family: "Times New Roman"; font-size: medium; white-space: normal;"><u>
</u></b></pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "courier new"; font-size: 9.6pt;"><span style="color: navy; font-weight: bold;">
</span></pre>
<pre style="font-family: "courier new";">public class LoginResponse {
@SerializedName("data")
private Data data;
@SerializedName("message")
private String message;</pre>
<pre style="font-family: "courier new";"><pre style="font-family: "courier new";"> //Here you can add Getter and Setter for this class if you need</pre>
<pre style="font-family: "courier new";"></pre>
public static class Data{
<span style="color: olive;">@SerializedName</span>("token")
private String token;</pre>
<pre style="font-family: "courier new";"></pre>
<pre style="font-family: "courier new";"><pre style="font-family: "courier new";"> //Here you can add Getter and Setter for this class if you need</pre>
}
</pre>
<pre style="font-family: "courier new";">}</pre>
<span style="font-size: 9.6pt;">
</span></pre>
<br />
<br />
<pre style="background-color: white;"><u><b><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">Class to parse </span><i>addShopDetails </i><span style="white-space: normal;">API</span></span></b></u></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"><b><u><span style="font-family: "times new roman"; font-size: small; white-space: normal;">
</span></u></b></pre>
<pre style="background-color: white; font-family: "courier new";">public class CommonServiceResponse {
@SerializedName("data")
private Object data;
@SerializedName("count")
private int count;
@SerializedName("message")
private String message;
//Here you can add Getter and Setter for this class if you need
}</pre>
<pre style="background-color: white; font-family: "courier new";"></pre>
<br />
Now we added all the Class files we needed to the API Integration. Now I will show you How to invoke API calls on our Application.<br />
<br />
<br />
<u><b><span style="font-family: "times" , "times new roman" , serif;">API Call : <i>getCountryList</i></span></b></u><br />
<div>
<u><b><span style="font-family: "times" , "times new roman" , serif;"><br /></span></b></u></div>
<pre style="background-color: white; font-family: "courier new";">ApiClient.<span style="font-style: italic;">getCountryList</span>().enqueue(new Callback<CountryListResponse>() {
@Override public void onResponse(Call<CountryListResponse> call, </pre><pre style="background-color: white; font-family: "courier new";"> retrofit2.Response<CountryListResponse> <span style="background-color: #e4e4ff;">response</span>) {
if (<span style="background-color: #e4e4ff;">response</span> != null && <span style="background-color: #e4e4ff;">response</span>.body() != null) {
if (<span style="background-color: #e4e4ff;">response</span>.body().getData() != null) {
ArrayList<Country> countries = response.body().getData();</pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "courier new";"> //Here Comes the code to parse the result</pre>
<pre style="font-family: "courier new";"></pre>
}
}
}
@Override public void onFailure(Call<CountryListResponse> call, Throwable t) {
Log.<span style="font-style: italic;">v</span>("API", "API Error : " + t.getMessage());
}
});</pre>
<br />
<br />
<pre style="background-color: white;"><b><u><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">
</span></span></u></b></pre>
<pre style="background-color: white;"><b><u><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">API Call : </span><i>login</i></span></u></b></pre>
<div>
<b><u><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;"><br /></span></span></u></b></div>
<pre style="background-color: white; font-family: "courier new";">ApiClient.<span style="font-style: italic;">login</span>(username, password).enqueue(new Callback<LoginResponse>() {
@Override public void onResponse(Call<LoginResponse> call, retrofit2.Response<LoginResponse> response) {
if (response != null && response.body() != null) {</pre>
<pre style="background-color: white; font-family: "courier new";"></pre>
<pre style="background-color: white; font-family: "courier new";"> //Here Comes the code to parse the result
}
}
@Override public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.<span style="font-style: italic;">v</span>("API", "API Error : " + t.getMessage());
}
});</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white;"><pre><u><b><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">API Call: </span><i>addShopDetails</i></span></b></u></pre>
</pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"></pre>
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "courier new";">ApiClient.addShopDetails(token, shopName, shopAddress).enqueue(new Callback<CommonServiceResponse>() {
@Override public void onResponse(Call<CommonServiceResponse> call, retrofit2.Response<CommonServiceResponse> response) {
if (response != null && response.body() != null) {
</pre>
<pre style="font-family: "courier new";"> //Here Comes the code to parse the result</pre>
<pre style="font-family: "courier new";"></pre>
<pre style="font-family: "courier new";"> }
}
@Override public void <span style="background-color: #e4e4ff;">onFailure</span>(Call<CommonServiceResponse> call, Throwable t) {
Log.<span style="font-style: italic;">v</span>("API", "API Error : " + t.getMessage());
}
});</pre>
<span style="font-size: 9.6pt;">
</span></pre>
<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">---------------</span><br />
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0tag:blogger.com,1999:blog-7136242653797922563.post-81381089216750241932018-06-21T18:06:00.003+05:302019-04-04T13:55:02.661+05:30SharedPreferences in Kotlin<div dir="ltr" style="text-align: left;" trbidi="on">
On this blog I am going to explain how to use SharedPreferences in Android Kotlin. As you knew SharedPreferences is used for saving application data as key-value pair. You can find more about shared prederance from <a href="https://developer.android.com/training/data-storage/shared-preferences">Here</a>.<br />
<br />
<br />
Here I am creating a new class <i>AppSettings</i>. With the help of this class we are going to save a String, Boolean, Int values..<br />
<br />
<b><u><br /></u></b>
<b><u>Java Code</u></b><br />
<br />
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt;"><span style="color: navy; font-weight: bold;">
</span></pre>
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">class AppSettings(context: Context) {
val PREFS_FILENAME = "BMatesTechnologies"
val UserID = "UserID" //Int value
val LoginStatus = "LoginStatus" //Boolean Value
val UserEmail = "Email" //String Value
val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME,
Context.<span style="font-style: italic;">MODE_PRIVATE</span>);
var userId: Int
get() = prefs.getInt(UserID,0 )
set(value) = prefs.edit().putInt(UserID, 0).apply()
var loginStatus: Boolean
get() = prefs.getBoolean(LoginStatus, false)
set(value) = prefs.edit().putBoolean(LoginStatus, value).apply()
var email: String
get() = prefs.getString(UserEmail, null)
set(value) = prefs.edit().putString(UserEmail, value).apply()
}</span></pre>
<br />
<br />
I am using the following method to save or get values from the shared Preferance.<br />
<div>
<br /></div>
<div>
<u><br /></u></div>
<div>
<u>Initilize AppSettings</u></div>
<div>
<pre style="background-color: white;"><span style="font-family: "courier new"; font-size: 9.6pt;">
</span><span style="font-family: "courier new" , "courier" , monospace;"> var appSettings: AppSettings? = null</span></pre>
</div>
<div>
<br /></div>
<div>
<u><br /></u></div>
<div>
<u>Declaration</u></div>
<div>
<u><br /></u></div>
<div>
<pre style="font-family: "Courier New";"><span style="background-color: white;"> appSettings = AppSettings(applicationContext)</span></pre>
</div>
<div>
<br /></div>
<div>
<u><br /></u></div>
<div>
<u>Assigning Values</u></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<pre><span style="background-color: white; font-family: "courier new" , "courier" , monospace;">
appSettings!!.loginStatus = true</span></pre>
<pre><pre><span style="background-color: white; font-family: "courier new" , "courier" , monospace;">
appSettings!!.email = "shidhi.shidhin@gmail.com"</span></pre>
<pre><span style="font-family: "courier new" , "courier" , monospace;"><span style="background-color: white;">
appSettings!!.</span>userId <span style="background-color: white;">= 1</span></span></pre>
</pre>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">---------------</span><br />
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-79974505552186529702018-06-20T18:02:00.000+05:302019-04-04T13:55:16.761+05:30Capture Image Using Camera<div dir="ltr" style="text-align: left;" trbidi="on">
On this blog I am going to Explain How to capture Image. Here I am using FileProvider for Capture Image. Here is the steps to integrate Image Capture using Camera on your application.<br />
<br />
<u><b>Add Camera Permission</b></u><br />
<br />
We need to Add the Camera permission on <i>AndroidManifest.xml</i> file like,<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<pre><span style="background-color: white; font-family: "courier new" , "courier" , monospace;"><uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /></span></pre>
<br />
<br />
<b><u>Add File Provider</u></b><br />
<br />
We need to Add <i>provider </i>inside the <i>application </i>tag on <i>AndroidManifest.xml</i> file like,<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><provider<br /> android:name="android.support.v4.content.FileProvider"<br /> android:authorities="${applicationId}.fileprovider"<br /> android:exported="false"<br /> android:grantUriPermissions="true"><br /> <meta-data<br /> android:name="android.support.FILE_PROVIDER_PATHS"<br /> android:resource="@xml/file_paths"></meta-data><br /></provider></span><br />
<br />
<br />
<br />
<b><u>Add file path</u></b><br />
<br />
Inside the <i>provider</i> tag we added <i><span style="font-family: inherit;">android:resource="@xml/file_paths"</span><span style="font-family: "courier new" , "courier" , monospace;">,</span></i> we need to add new folder <b>xml</b>(If the folder not exist inside the res folder) and inside the xml folder we need to add new resource file <b>file_paths</b><i> </i>with the below content,<br />
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><?xml version="1.0" encoding="utf-8"?></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><paths xmlns:android="http://schemas.android.com/apk/res/android"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <external-path name="my_images" path="Android/data/com.android.myapplication/files/Pictures" /></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></paths></span><br />
<br />
<br />
<b><u>Java Code</u></b><br />
<br />
Here I am capturing the image using my function <span style="font-family: "courier new" , "courier" , monospace;">takePicture(),</span> Before calling this function I am checking the camera permission using my function <span style="font-family: "courier new" , "courier" , monospace;">checkCameraPermissionGranded()</span>, On the Camera Button Click I am using,<br />
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">if (!checkCameraPermissionGranded())</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> requestCameraPermission();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">else </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> takePicture();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: inherit;"><u>Check Camera Permission</u></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private boolean checkCameraPermissionGranded() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return true;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return false;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: inherit;"><u><br /><br />Request Camera Permission</u></span><br />
<br />
If the permission is not given we will request permission for access camera for our application using <span style="font-family: "courier new" , "courier" , monospace;">requestCameraPermission()</span><span style="font-family: inherit;"> methid. Once user grand permission it will automatically execute override method</span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;">onRequestPermissionsResult.</span><span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> private void requestCameraPermission() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ActivityCompat.requestPermissions(HomeActivity.this, new String[]{android.Manifest.permission.CAMERA,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}, Constants.CAPTURE_REQUEST_CODE);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (requestCode == Constants.CAPTURE_REQUEST_CODE) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (checkCameraPermissionGranded())</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> takePicture();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> else {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> requestCameraPermission();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } else if (requestCode == Constants.LOCATION_REQUEST_CODE) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (!checkLocationPermissionGranded()) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> requestCameraPermission();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> location = getUserLocation();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<u>Capture Image</u><br />
<br />
Using <span style="font-family: "courier new" , "courier" , monospace;">takePicture() </span>method we will capture Image , Once capture the image it will execute the override method <span style="font-family: "courier new" , "courier" , monospace;">onActivityResult</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<pre style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;">private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.<span style="font-style: italic;">ACTION_IMAGE_CAPTURE</span>);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
ProfilePicUri = FileProvider.<span style="font-style: italic;">getUriForFile</span>(HomeActivity.this, "com.android.myapplication.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.<span style="font-style: italic;">EXTRA_OUTPUT</span>, ProfilePicUri);
startActivityForResult(takePictureIntent, Constants.<span style="font-style: italic;">IMAGE_CAPTURE_CAMERA</span>);
}
}
}</span></pre>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<pre style="background-color: white; font-family: "Courier New";">@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.<span style="font-style: italic;">IMAGE_CAPTURE_CAMERA </span>&& resultCode == <span style="font-style: italic;">RESULT_OK</span>) {
if (ProfilePicPath != null && ProfilePicPath.trim().length() > 0 && !ProfilePicPath.isEmpty()) {
File image_file = new File(ProfilePicPath);
if (image_file.exists()) {
Bitmap newProfilePic = BitmapFactory.<span style="font-style: italic;">decodeFile</span>(ProfilePicPath);
// Code to manage the bitmap
}
}
}
}</pre>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">---------------</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-46144431118820677592018-06-20T17:21:00.000+05:302019-04-04T13:55:28.482+05:30API call using Volley StringRequest <div dir="ltr" style="text-align: left;" trbidi="on">
On this blog I am going to explain the API call using volley library, Here the API call is using a StringRequest. For the API integration we are using Volley Library , So first we need to add the volley library dependency on the application's build.gradle file like,<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> dependencies { <br /> compile 'com.android.volley:volley:1.1.0' <br /> }</span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<b><u>StringRequest </u></b><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> String serviceUrl = "<a href="http://shidhin.net/">http://shidhin.net/api/ApiTest</a>";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> StringRequest strReq = new StringRequest(Request.Method.POST, serviceUrl, new Response.Listener<String>() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onResponse(String responseString) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> // Successfull request</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } catch (Exception e) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }, new Response.ErrorListener() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onErrorResponse(VolleyError error) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }){</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected String getParamsEncoding() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return "utf-8";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> };</span><br />
<br />
<br />
This is the simple request, If we want to pass the request parameters, Header or request encoding type etc, we can do it like,<br />
<b><u>Request Parameters</u></b><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public Map<String, String> getHeaders() throws AuthFailureError {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Map<String, String> params = new HashMap<String, String>();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("Accept","application/json");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>params.put("Content-Type","application/json");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return params;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<br />
<br />
<b><u>Request Header</u></b><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">@Override </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public Map<String, String> getHeaders() throws AuthFailureError {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Map<String, String> params = new HashMap<String, String>(); </span><span style="font-family: "courier new" , "courier" , monospace;">params.put("Accept","application/json"); </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("Content-Type","application/json"); </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return params; </span><span style="font-family: "courier new" , "courier" , monospace; white-space: pre;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> } </span><br />
<br />
<br />
<br />
<b><u><br /></u></b>
<b><u>Final Reqest</u></b><br />
<br />
This is an example StringRequest with Request Parameters and Request Header ( Request Parameters and Request Header are optional on a request).<br />
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">StringRequest strReq = new StringRequest(Request.Method.POST, serviceUrl, new Response.Listener<String>() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onResponse(String responseString) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> // Successfull request</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> } catch (Exception e) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }, new Response.ErrorListener() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void onErrorResponse(VolleyError error) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }){</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected Map<String, String> getParams() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Map<String, String> params = new HashMap<String, String>();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("username", userName);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("Password", password);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return params;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected Map<String, String> getParams() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Map<String, String> params = new HashMap<String, String>();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("username", userName);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> params.put("Password", password);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return params;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Override</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected String getParamsEncoding() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return "utf-8";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> });</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">requestQueue.add(strReq);</span><br />
<br />
<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">---------------</span><br />
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-71930286687081517562018-06-19T16:54:00.000+05:302019-04-04T13:55:42.795+05:30Simple Date functions<div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;">On this blog I am going to explain some date functionality's which we are commonly used in our android application.</span></span><span style="color: black; font-family: "times new roman"; font-size: small; font-weight: 400; white-space: normal;"> </span><span style="color: navy; font-family: "courier new";"><span style="font-size: 9pt;"><b>
</b></span></span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"><span style="color: navy; font-weight: bold;">
</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"><span style="color: navy; font-weight: bold;">
</span></pre>
<h3 style="background-color: white; font-family: "courier new"; text-align: left;">
<span style="font-size: small;"><u>Current Date Time</u></span></h3>
<blockquote class="tr_bq">
public static String getCurrentDate() {<br />
String DATE_FORMAT_NOW = "dd/MM/yyyy";<br />
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);<br />
return sdf.format(new Date());<br />
}</blockquote>
<span style="font-family: inherit;"><span style="white-space: normal;"> </span></span><br />
<span style="font-family: inherit;"><span style="white-space: normal;"> This function will return the current system date and time on </span><span style="font-size: small; white-space: normal;"> </span></span><span style="background-color: white; color: green; font-family: "courier new"; font-size: 12px; font-weight: 700;">dd/MM/yyyy HH:mm </span>this format. There is so many other different Date time formats are available, <span style="background-color: white;">You can replace the current format (</span><span style="background-color: white; color: green; font-family: "courier new"; font-size: 12px; font-weight: 700;">dd/MM/yyyy HH:mm</span><span style="background-color: white;">) with your date format if you need.</span><br />
<br />
<br />
<br />
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">Compare two Date Time</u></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;">
</span></span></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"><blockquote class="tr_bq">
<pre><span style="font-family: "courier new" , "courier" , monospace;">public static boolean checkDateBeforeFirstDate(String first, String second) {
try {
String DATE_FORMAT_NOW = "dd/MM/yyyy HH:mm";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
Date firstDate = sdf.parse(first);
Date secondDate = sdf.parse(second);
if(secondDate.before(firstDate))
return true;
else
return false;
}catch (Exception e){
return false;
}
}</span></pre>
</blockquote>
</pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;">
</span></span></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;">
</span></span></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;"> This function will return true or false by comparing the given 2 dates, Here I am used date time format as <i>dd/MM/yyyy HH:mm</i> this. There is so many other different Date time formats are available, You can replace the current format (<i>dd/MM/yyyy HH:mm</i>) with your date format if you need.</span></span> </pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman"; white-space: normal;">
</span></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman"; white-space: normal;">
</span></pre>
<pre style="background-color: white;"><pre style="font-family: "courier new"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">
</u></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">
</u></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">Change Date Time Format</u></pre>
<pre style="background-color: white; font-family: "courier new";"><blockquote class="tr_bq">
<pre style="font-family: "Courier New";">public static String changeToServerFormat(String time) {
String inputPattern = "dd/MM/yyyy HH:mm";
String outputPattern = "yyyy-MM-dd HH:mm";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null; String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}</pre>
</blockquote>
</pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;"> This function will return the converted format date string. On the above function will convert <i>dd/MM/yyyy HH:mm</i> format date time string to <i>yyyy-MM-dd HH:mm</i>. There is so many other different Date time formats are available, You can replace the input or output date time format with your date format if you need.</span></span> </pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">
</u></pre>
<pre style="font-family: "courier new"; font-size: 9pt;"><u style="font-size: medium; font-weight: 700; white-space: normal;">Current Week Days</u></pre>
</pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"><pre><span style="font-family: "courier new" , "courier" , monospace;">public static ArrayList<String> currentWeekDaysOfMonth() {
ArrayList<String> weekDays = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
SimpleDateFormat date = new SimpleDateFormat("EEE dd MMMM yyyy", Locale.ENGLISH);
for (int i = 0; i < 7; i++) {
String <span style="background-color: #ffe4ff;">weekDay</span> = date.format(cal.getTime());
Log.v("WeekDays", "Date: " + <span style="background-color: #e4e4ff;">weekDay</span>);
weekDays.add(<span style="background-color: #e4e4ff;">weekDay</span>);
cal.add(Calendar.DAY_OF_WEEK, 1);
}
return weekDays;
}</span></pre>
</pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"><pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"></pre>
<pre style="background-color: white;"><span style="font-family: "times new roman";"><span style="white-space: normal;"><span style="font-family: "times new roman";"> This function will return the current weeks days as a String ArrayList. On the above function will return the date on </span><i><span style="font-family: "courier new" , "courier" , monospace;">EEE dd MMMM yyyy</span></i><span style="font-family: "times new roman";"> format. On the above function it will return the current weekdays starting from </span><i><span style="font-family: "courier new" , "courier" , monospace;">Sunday</span> </i><span style="font-family: "times new roman";">(We can specify it using </span></span></span><i><span style="font-family: "courier new" , "courier" , monospace;">.setFirstDayOfWeek(Calendar.<span style="color: #660e7a;">SUNDAY</span>)</span></i><span style="font-family: "times new roman"; white-space: normal;"><span style="white-space: normal;"> code).</span></span><span style="font-family: "times new roman"; white-space: normal;"> </span></pre>
</pre>
<pre style="background-color: white;">
</pre>
<pre style="background-color: white;">
</pre>
<pre style="background-color: white;"><div style="font-family: "Times New Roman"; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">
</span></div>
<div style="font-family: "Times New Roman"; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">---------------</span></div>
<div style="font-family: "Times New Roman"; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</pre>
<pre style="background-color: white;"><span style="font-family: "times new roman"; white-space: normal;">
</span></pre>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9pt;"></pre>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-51475611706293171192018-06-19T14:56:00.001+05:302019-04-04T13:55:58.074+05:30Custom Dialog in Kotlin<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<b> O</b>n this blog I am going to explain How to create and display a custom Dialog on Kotlin. Here I am going to display a dialog with custom Title and Message which I pass to the dialog. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<u style="font-weight: 700;">colors.xml </u></div>
<blockquote class="tr_bq">
<?xml version="1.0" encoding="utf-8"?><br />
<resources><br />
<color name="colorPrimary">#DB3236</color><br />
<color name="colorPrimaryDark">#C52D30</color><br />
<color name="colorAccent">#111111</color><br />
<color name="iconColor">#9a9999</color><br />
<color name="Black">#000000</color><br />
<color name="White">#fff</color><br />
<color name="LightBlack">#111111</color><br />
<color name="Gray">#777777</color><br />
<color name="DarkGray">#444444</color><br />
<color name="MediumGray">#dddddd</color><br />
<color name="LightGray">#f5f5f2</color><br />
</resources></blockquote>
<div>
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<u style="font-weight: 700;">styles.xml </u></div>
<blockquote class="tr_bq">
<resources><br />
<style name="App_TextView_Normal"><br />
<item name="android:textColor">@color/DarkGray</item><br />
<item name="android:textSize">14sp</item><br />
<item name="android:fontFamily">@font/biryani</item><br />
<item name="android:gravity">center</item><br />
<item name="android:layout_width">match_parent</item><br />
<item name="android:layout_height">match_parent</item><br />
</style><br />
<style name="App_TextView_Title"><br />
<item name="android:textColor">@color/DarkGray</item><br />
<item name="android:textSize">16sp</item><br />
<item name="android:fontFamily">@font/biryani</item><br />
<item name="android:gravity">center</item><br />
<item name="android:textStyle">bold</item><br />
<item name="android:layout_width">match_parent</item><br />
<item name="android:layout_height">match_parent</item><br />
</style><br />
<br />
<style name="App_TextView_Button"><br />
<item name="android:textColor">@color/DarkGray</item><br />
<item name="android:textSize">16sp</item><br />
<item name="android:fontFamily">@font/fjalla_one</item><br />
<item name="android:gravity">center</item><br />
<item name="android:layout_width">wrap_content</item><br />
<item name="android:layout_height">match_parent</item><br />
<item name="android:minHeight">40dp</item><br />
</style><br />
</resources></blockquote>
<div>
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u>rounded_white_bg.xml </u></b></div>
<blockquote class="tr_bq">
<?xml version="1.0" encoding="utf-8"?><br />
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><br />
<item><br />
<shape android:shape="rectangle"><br />
<stroke android:width="1px" android:color="@color/White" /><br />
<solid android:color="@color/White" /><br />
<corners android:radius="10dp"/><br />
</shape><br />
</item><br />
</layer-list></blockquote>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt; text-align: left;"></pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u>info_dialog.xml </u></b></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
This is the dialog layout file. You can remove the background colors, text colors and styles.</div>
<div style="text-align: left;">
<br /></div>
<blockquote class="tr_bq">
<?xml version="1.0" encoding="utf-8"?><br />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />
android:layout_width="match_parent"<br />
android:layout_height="wrap_content"<br />
android:orientation="vertical"<br />
android:background="@drawable/rounded_white_bg"<br />
android:paddingTop="5dp"><br />
<TextView<br />
android:id="@+id/txtTitle"<br />
style="@style/App_TextView_Title"<br />
android:layout_width="match_parent"<br />
android:layout_height="wrap_content"<br />
android:layout_marginRight="10dp"<br />
android:layout_marginLeft="10dp"<br />
android:paddingTop="10dp"<br />
android:text="@string/Title" /><br />
<TextView<br />
android:id="@+id/txtMessage"<br />
style="@style/App_TextView_Normal"<br />
android:layout_width="match_parent"<br />
android:layout_height="wrap_content"<br />
android:text="@string/Description"<br />
android:gravity="center|top"<br />
android:minLines="2"<br />
android:padding="5dp"/><br />
<br />
<TextView<br />
style="@style/App_TextView_Normal"<br />
android:layout_width="match_parent"<br />
android:layout_height="1px"<br />
android:layout_marginTop="1px"<br />
android:layout_marginBottom="1px"<br />
android:background="@color/MediumGray"/><br />
<br />
<Button<br />
android:id="@+id/btnOK"<br />
style="@style/App_TextView_Button"<br />
android:layout_width="match_parent"<br />
android:layout_height="wrap_content"<br />
android:background="@color/White"<br />
android:layout_marginRight="10dp"<br />
android:layout_marginLeft="10dp"<br />
android:textColor="@color/colorPrimary"<br />
android:text="@string/OK" /><br />
</LinearLayout></blockquote>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u>InfoMessageDialog.kt</u></b></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
This is the kotlin class file, We will pass the "Title" and "Message" to the Dialog class and it will be displayed on the class. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<pre style="background-color: white; font-family: "courier new"; text-align: left;"><pre style="font-family: "courier new";">class InfoMessageDialog(context: Context, private var title : String, private var message : String) : Dialog(context) {
var mContext: Context
var mTitle : String
var mMessage :String
init{
this.mContext = context
this.mTitle = title this.mMessage = message }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.<span style="font-style: italic;">FEATURE_NO_TITLE</span>)
setContentView(R.layout.<span style="font-style: italic;">info_dialog</span>)
<span style="font-style: italic;">window</span>!!.setLayout(WindowManager.LayoutParams.<span style="font-style: italic;">MATCH_PARENT</span>, WindowManager.LayoutParams.<span style="font-style: italic;">WRAP_CONTENT</span>)
setCanceledOnTouchOutside(true)
var txtTitle = findViewById<TextView>(R.id.<span style="font-style: italic;">txtTitle</span>)
var txtMessage = findViewById<TextView>(R.id.<span style="font-style: italic;">txtMessage</span>)
var btnOK = findViewById<Button>(R.id.<span style="font-style: italic;">btnOK</span>)
txtTitle.setText(mTitle)
txtMessage.setText(mMessage)
btnOK.setOnClickListener(View.OnClickListener { dismiss()</pre>
<pre style="font-family: "courier new";"> })
}
}</pre>
<span style="font-size: 9.6pt;">
</span></pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
We can show this dialog using,</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u>Fragment Page,</u></b></div>
<pre style="background-color: white; font-family: "Courier New"; font-size: 9.6pt; text-align: left;"></pre>
<pre style="background-color: white; font-family: "courier new"; text-align: left;"></pre>
<pre style="background-color: white; font-family: "courier new"; text-align: left;">InfoMessageDialog(<span style="font-style: italic;">activity</span>, getString(R.string.<span style="font-style: italic;">No_Network_Title</span>), getString(R.string.<span style="font-style: italic;">No_Network_Message</span>)).show();</pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u><br /></u></b></div>
<div style="text-align: left;">
<b><u>Activity Page,</u></b></div>
<div style="text-align: left;">
<br /></div>
<pre style="background-color: white; font-family: "courier new"; text-align: left;">InfoMessageDialog(this, getString(R.string.<span style="font-style: italic;">No_Network_Title</span>), getString(R.string.<span style="font-style: italic;">No_Network_Message</span>)).show();</pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">---------------</span><br />
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-73645927866722999142018-06-19T14:21:00.000+05:302019-04-04T13:56:09.494+05:30Splash Screen on Kotlin language<div dir="ltr" style="text-align: left;" trbidi="on">
<b> O</b>n this blog I am going to explain how to show a splash screen using Handler. You can find more about <a href="https://developer.android.com/reference/android/os/Handler">Handler</a> from <a href="https://developer.android.com/reference/android/os/Handler">Here</a>.<br />
<br />
Here I am using a delay of 4000 MS (4 Seconds)<br />
<br />
<br />
Here I am using postDelayed function for this,<br />
<br />
<pre style="font-family: "Courier New"; font-size: 9.6pt;">fun postDelayed(r: Runnable!, delayMillis: Long): Boolean</pre>
<br />
<br />
<u><b>Sample Code</b></u><br />
<br />
<pre style="background-color: white; font-family: "courier new";"><pre style="font-family: "courier new";">override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.<span style="font-style: italic;">activity_splash</span>)
Handler().postDelayed({
</pre>
<pre style="font-family: "courier new";"> <span style="font-family: "courier new";">//</span><span style="font-family: "courier new";">Here Comes the code to execute after the time delay</span></pre>
<pre style="font-family: "courier new";"> }, <span style="color: blue;">4000</span>)
</pre>
<pre style="font-family: "courier new";">}</pre>
<span style="font-size: 9.6pt;">
</span><pre style="font-family: "courier new"; font-size: 9.6pt;"></pre>
<span style="font-size: 9.6pt;">
</span><pre style="font-family: "courier new"; font-size: 9.6pt;"></pre>
<span style="font-size: 9.6pt;">
</span></pre>
<pre style="background-color: white; font-family: "courier new";"><span style="font-size: 9.6pt;"><div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">
</span></div>
<div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">---------------</span></div>
<div style="font-family: "Times New Roman"; font-size: medium; white-space: normal;">
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</span><pre style="font-family: "courier new"; font-size: 9.6pt;"></pre>
<span style="font-size: 9.6pt;">
</span><pre style="font-family: "courier new"; font-size: 9.6pt;"></pre>
<span style="font-size: 9.6pt;">
</span></pre>
<br />
<br /></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-9632718303355905832018-06-19T14:19:00.000+05:302019-04-04T13:56:23.559+05:30Create new Kotlin Project<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<b> </b><br />
<b>O</b>n this blog I am going to explain How to setup new <a href="https://kotlinlang.org/">Kotlin</a> project on Android Studio. To setup new project on <a href="https://kotlinlang.org/">Kotlin</a> on Android project we need <a href="https://developer.android.com/studio/index.html">Android Studio</a> version 3.0 or more. For older version of studio we can user Pligins. You can able to download by selecting "<i>Install JetBrains plugin</i>". </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Here is the steps to start new Android Project with Kotlin Support.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Goto File => New => NewProject</div>
<div style="text-align: left;">
<br /></div>
<br />
<div style="text-align: left;">
</div>
<br />
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="margin: 0px;">
While select <b>New Project</b> you can see the <b>Create New Project window</b>, </div>
<div style="margin: 0px;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggILbUqGPFWCngJXiRLMyHXyZQOZdflSkhhf2LiDEiIDcvrCPafZ1fQen4h6w3_woN_xiTdZ9klduptKv2l3N3geKbMnYLa9jrj3s3_bhGdXTTY2LgYSWBOcYC5iB5Mxb4xfnvItx0eQY/s1600/New+Project.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="702" data-original-width="905" height="248" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggILbUqGPFWCngJXiRLMyHXyZQOZdflSkhhf2LiDEiIDcvrCPafZ1fQen4h6w3_woN_xiTdZ9klduptKv2l3N3geKbMnYLa9jrj3s3_bhGdXTTY2LgYSWBOcYC5iB5Mxb4xfnvItx0eQY/s320/New+Project.PNG" style="cursor: move;" width="320" /></a></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
You can add the project details such as <b>Application Name</b>, <b>Company domain</b>, <b>Project Location</b>, <b>Package name </b>etc. And By selecting "<b>Include Kotlin Support</b>" you can enable the Kotlin support on your Project.</div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
After This Screen while tap on "Next" button it will show the screen to Select the Target Device,</div>
<div style="margin: 0px;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WHKWf-n-nSAYiltx9CUQlzK6f_x4FoDaVjgTmO9ebl1rGWwy7jTb1NvOLlooWenPK9iMkBjUZt5kb5XMvSb2as2zrw-TQ-h_BTX2R_MIwTJiPPQgP5OZJT3u7TZvcidHOtMGIoIoQSk/s1600/target_device.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="691" data-original-width="905" height="244" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3WHKWf-n-nSAYiltx9CUQlzK6f_x4FoDaVjgTmO9ebl1rGWwy7jTb1NvOLlooWenPK9iMkBjUZt5kb5XMvSb2as2zrw-TQ-h_BTX2R_MIwTJiPPQgP5OZJT3u7TZvcidHOtMGIoIoQSk/s320/target_device.PNG" style="cursor: move;" width="320" /></a></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
This screen will allow you to choose the Android Target Device you need, While tap on Next button it will show the Choose Activity Screen</div>
<div style="margin: 0px;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilpcOGDMv7xRayFdrnPyQqTMMtghfph8sBov7Gd3rEcbDTqBZwBQM7bll4grGgFdrF-MOp2dWAwHVNdRVPOXRuNyl96md3b1xCzg6aDTlmHapsS5YdGFGhAWONwYWkLIWd5eQbPLpWglI/s1600/add+activity.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="697" data-original-width="907" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilpcOGDMv7xRayFdrnPyQqTMMtghfph8sBov7Gd3rEcbDTqBZwBQM7bll4grGgFdrF-MOp2dWAwHVNdRVPOXRuNyl96md3b1xCzg6aDTlmHapsS5YdGFGhAWONwYWkLIWd5eQbPLpWglI/s320/add+activity.PNG" style="cursor: move;" width="320" /></a></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
On this screen you can select the Type of Activity we need on the Application. While Tap on Next button it will show the screen to Configure the selected Activity page.</div>
<div style="margin: 0px;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk268KOztQFmXGVaLtnzsnPwB73X3MjrHBTskiTPVNv1YYLnqQkrQ_oVHiFSVwhcoej9KVghrXiO10B02dzU2hhvLWyT39KZRQm6Y6I1c-PFqYRC2G_ENRC9tH_MKfZgmM4sFitqOJeA/s1600/configure+activity.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="697" data-original-width="904" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFk268KOztQFmXGVaLtnzsnPwB73X3MjrHBTskiTPVNv1YYLnqQkrQ_oVHiFSVwhcoej9KVghrXiO10B02dzU2hhvLWyT39KZRQm6Y6I1c-PFqYRC2G_ENRC9tH_MKfZgmM4sFitqOJeA/s320/configure+activity.PNG" style="cursor: move;" width="320" /></a></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
On this Screen we can Add the Activity Name and the layout name. And while Tap on Finish button it will Create the New Project with your Configurations. While tap on finish button it will setup your first project. Here is the project window.</div>
<div class="separator" style="clear: both; margin: 0px; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiNwEizH0lI2bF7SVlO1VBaqoIpvH8mSZj9NC3CyDRHclOb6AeLjqgLO6E_FfuKJlz6IE9m0QVJte_mCN5hTW8K4CN-vWrgVyfnk6GLHTYAKSpASWf-vW6jIEJpO54ELSePXwoCiCr2I8/s1600/new+project+done.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="861" data-original-width="1600" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiNwEizH0lI2bF7SVlO1VBaqoIpvH8mSZj9NC3CyDRHclOb6AeLjqgLO6E_FfuKJlz6IE9m0QVJte_mCN5hTW8K4CN-vWrgVyfnk6GLHTYAKSpASWf-vW6jIEJpO54ELSePXwoCiCr2I8/s400/new+project+done.PNG" style="cursor: move;" width="400" /></a></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<br /></div>
<div style="margin: 0px;">
<br />
<div style="font-family: "Times New Roman";">
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div style="font-family: "Times New Roman";">
<span style="font-family: Courier New, Courier, monospace;">---------------</span></div>
<div style="font-family: "Times New Roman";">
<span style="font-family: Courier New, Courier, monospace;">Thanks For Reading, Wish you a Happy Coding....</span></div>
</div>
</div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-31604093838276941412018-03-14T15:25:00.003+05:302018-03-14T15:26:23.088+05:30How to check Network Availability<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
On this blog I am going to explain how to check internet connection is present or not. For this we are using <span style="font-family: "courier new";">ConnectivityManager </span>and <span style="font-family: "courier new";">NetworkInfo</span>. For this first you need to add the permission on your <i>AndroidManifest.xml</i> file. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><u>Adding Permission</u></b></div>
We required INTERNET and ACCESS_NETWORK_STATE. We need to add these permisions inside <application> </application> tag. Here is the permission code.<br />
<br />
<uses-permission android:name="android.permission.INTERNET" /><br />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><br />
<b><u><br /></u></b>
<b><u>Java Code</u></b><br />
I created a function <i>isOnline(Context con) </i>to check the internet is there or not. This function will return the network status if the internet is connected or not.<br />
<br />
<span style="font-family: "courier new";">public static boolean isOnline(Context con) {</span><br />
<span style="font-family: "courier new";"> boolean connected = false;</span><br />
<span style="font-family: "courier new";"> ConnectivityManager connectivityManager;</span><br />
<span style="font-family: "courier new";"> try {</span><br />
<span style="font-family: "courier new";"> connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);</span><br />
<span style="font-family: "courier new";"> NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();</span><br />
<span style="font-family: "courier new";"> connected = networkInfo != null && networkInfo.isConnected();</span><br />
<span style="font-family: "courier new";"><br /></span>
<span style="font-family: "courier new";"> } catch (Exception e) {</span><br />
<span style="font-family: "courier new";"> e.printStackTrace();</span><br />
<span style="font-family: "courier new";"> }</span><br />
<span style="font-family: "courier new";"> return connected;</span><br />
<span style="font-family: "courier new";">}</span></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-69481435769858965502017-08-15T22:37:00.000+05:302017-08-15T22:56:38.959+05:30How to get onBackPressed() in a Fragment page.<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "times" , "times new roman" , serif;"> <br /> <span style="font-size: large;"> I</span>n this Blog I am going to explain how to trigger Back button press (<span style="background-color: white;"><i>onBackPressed()</i> </span>method<span style="background-color: white;">)</span> event on my fragment page. I am doing this with the help of an "<span style="background-color: white;">interface</span>" in java. For this I am creating an Activity Class and a Fragment Class. </span><br />
<span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "times" , "times new roman" , serif;"><br /></span>
<u><b><span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;">HomeScreen (</span>Activity Class)</span></b></u></span><br />
<span style="font-family: "times" , "times new roman" , serif;"><span style="font-family: "times" , "times new roman" , serif;"><br /></span><span style="font-family: "times" , "times new roman" , serif;"> In this class we created an interface "<i>OnBackPressedListener</i>" to detect the back press. When it execute <i>onBackPressed() </i>event it will execute the interface method "<i>doBack()</i>" which will trigger the event on the Fragment page.</span></span><br />
<br /><br /><div>
<i>public class HomeScreen extends Activity {</i></div>
<div>
<i><br /></i></div>
<div>
<i> protected OnBackPressedListener onBackPressedListener;</i></div>
<div>
<i><br /></i></div>
<div>
<i> public interface OnBackPressedListener {</i></div>
<div>
<i> void doBack();</i></div>
<div>
<i> }</i></div>
<div>
<i><br /></i></div>
<div>
<i> public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {</i></div>
<div>
<i> this.onBackPressedListener = onBackPressedListener;</i></div>
<div>
<i> }</i></div>
<div>
<i><br /></i></div>
<div>
<i><br /></i></div>
<div>
<i> @Override</i></div>
<div>
<i> protected void onCreate(Bundle savedInstanceState) {</i></div>
<div>
<i> super.onCreate(savedInstanceState);</i></div>
<div>
<i> setContentView(R.layout.home_screen);</i></div>
<div>
<i><br /></i></div>
<div>
<i> }</i></div>
<div>
<i><br /></i></div>
<div>
<i><br /></i></div>
<div>
<i> @Override</i></div>
<div>
<i> public void onBackPressed() {</i></div>
<div>
<i> if (onBackPressedListener != null)</i></div>
<div>
<i> onBackPressedListener.doBack(); </i></div>
<div>
<i> }</i></div>
<div>
<i><br /></i></div>
<div>
<i>}</i></div>
<div>
<br /></div>
<div>
<pre><pre><u><b><span style="font-family: "times" , "times new roman" , serif;">
</span></b></u></pre>
<pre><u><b><span style="font-family: "times" , "times new roman" , serif;">MyFragment (Fragment Class)</span></b></u></pre>
<span style="background-color: white;">
</span></pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"> <span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">In this Class it will automatically create the override method "doBack()" while we implements "HomeScreen.OnBackPressedListener" with the fragment page. Also we need to initialize the interface on "onCreateView" of the fragment page to get the event notification about "onBackPressed()".</span></span></pre>
<pre style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;"><span style="white-space: normal;">
</span></span></pre>
<pre style="background-color: white;"></pre>
<div>
<div>
<br />
<i>public class MyFragment extends Fragment implements HomeScreen.OnBackPressedListener {</i></div>
<div>
<i><br /></i></div>
<div>
<i> @Override</i></div>
<div>
<i> public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {</i></div>
<div>
<i> View rootView = inflater.inflate(R.layout.fragment_profile, container, false);</i></div>
<div>
<i><br /></i></div>
<div>
<i> ((HomeScreen) getActivity()).setOnBackPressedListener(this);</i></div>
<div>
<i><br /></i></div>
<div>
<i> }</i></div>
<div>
<i><br /></i></div>
<div>
<i><br /></i></div>
<div>
<i> @Override</i></div>
<div>
<i> public void doBack() {</i></div>
<div>
<i> //Here Comes your Code</i></div>
<div>
<i> } </i></div>
</div>
<div>
<i><br /></i></div>
<div>
<i>}</i></div>
<pre style="background-color: white;"></pre>
<pre style="background-color: white; font-family: Menlo; font-size: 9pt;"></pre>
</div>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-83020606718923324052014-11-12T16:57:00.000+05:302018-06-19T17:00:16.227+05:30How to call a webservice in android.<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">I</span>n this blog I am going to explain How to call a .Net xml webservice in android with the help of an login WebService.<br />
<h4 style="text-align: left;">
<u>XML Request</u></h4>
<i>POST /Service.asmx HTTP/1.1</i><br />
<i>Host:<a href="http://shidhin.net/" target="_blank"> shidhin.net</a></i><br />
<i>Content-Type: text/xml; charset=utf-8</i><br />
<i>Content-Length: length</i><br />
<i>SOAPAction: "<a href="http://shidhin.net/">http://shidhin.net</a>/Login"</i><br />
<i><br /></i>
<i><?xml version="1.0" encoding="utf-8"?></i><br />
<i><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></i><br />
<i> <soap:Body></i><br />
<i> <Login xmlns="<a href="http://shidhin.net/">http://shidhin.net/</a>"></i><br />
<i> <Email>string</Email></i><br />
<i> <Password>string</Password></i><br />
<i> </Login></i><br />
<i> </soap:Body></i><br />
<i></soap:Envelope></i><br />
<i><br /></i>
In this WebService request Our Function name is "<i>Login </i>" and the parameters we are passing are "<i>Email" </i>which is<i> "string"</i> format and<i> "</i><i>Password" </i>which is also in<i> "string" </i>format<i>.</i><br />
<h4 style="text-align: left;">
<u>XML Responc</u></h4>
<i>HTTP/1.1 200 OK</i><br />
<i>Content-Type: text/xml; charset=utf-8</i><br />
<i>Content-Length: length</i><br />
<i><br /></i>
<i><?xml version="1.0" encoding="utf-8"?></i><br />
<i><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></i><br />
<i> <soap:Body></i><br />
<i> <LoginResponse xmlns="<a href="http://shidhin.net/">http://shidhin.net/</a>"></i><br />
<i> <LoginResult></i><br />
<i> <IsValidLogin>boolean</IsValidLogin></i><br />
<i> <ErrorOnFailure>string</ErrorOnFailure></i><br />
<i> <Token>string</Token></i><br />
<i> </LoginResult></i><br />
<i> </LoginResponse></i><br />
<i> </soap:Body></i><br />
<i></soap:Envelope></i><br />
<br />
<div style="text-align: left;">
In this WebService response we are getting result through the parameters "<i>IsValidLogin</i>" which is<i> </i>a<i> "</i><i>boolean" </i>value and<i> "</i><i>ErrorOnFailure</i><i>" </i>which is a<i> "string" </i>value and<i> "</i><i>Token</i><i>" </i>which is also a <i>"string" </i>value. Here if the "<i>IsValidLogin</i>" is true then it will pass the "<i>Token</i>" otherwise it will pass "<i>ErrorOnFailure</i>" which contains the reason of failure.</div>
<h3 style="text-align: left;">
<u>Android Code</u></h3>
In Android I am calling this WebService with the help of a Library called <b><a href="http://kobjects.org/ksoap2/index.html" target="_blank">Ksoap</a>. </b>You can get more about <a href="http://kobjects.org/ksoap2/index.html" target="_blank">Ksoap </a>form <a href="http://kobjects.org/ksoap2/index.html" target="_blank">Here</a>. First we need to download the Ksoap .Jar file and add that library to your Android project.<br />
<h4 style="text-align: left;">
<b><u>Java Code</u></b></h4>
private static String NAMESPACE = "<a href="http://shidhin.net/">http://shidhin.net/</a>";<br />
private static String URL = "http://<i>shidhints</i>.com/Service.asmx?wsdl";<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
<i>public LoginToken login(String Email, String Password ) throws Exception {</i><br />
<i> String METHOD_NAME = "Login";</i><br />
<i> String SOAP_ACTION = "<a href="http://shidhin.net/">http://shidhin.net</a>/Login";</i><br />
<i><br /></i>
<i> SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);</i><br />
<i> Request.addProperty("Email", Email); // Adding Email </i><i>to request</i><br />
<i> Request.addProperty("Password", Password );</i><i> // Adding </i><i>Password to request</i><br />
<i><br /></i>
<i> LoginResult login</i><i>Result</i><i>;</i><br />
<i> try</i><br />
<i> {</i><br />
<i> SoapObject response = executeRequest(Request, SOAP_ACTION);</i><br />
<i> if(response != null){</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span></i><i>login</i><i>Result</i><i>= new </i><i>LoginResult</i><i>((SoapObject) response.getProperty(0));</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span>return </i><i>login</i><i>Result</i><i>;</i><br />
<i> }</i><br />
<i> }</i><br />
<i> catch(Exception e)</i><br />
<i> {</i><br />
<i> e.printStackTrace();</i><br />
<i> }</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return null;</i><br />
<i> }</i><br />
<br />
// This function is to call the webservice<br />
<i>private SoapObject executeRequest(SoapObject Request, String SOAP_ACTION) throws IOException{</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span>System.setProperty("http.keepAlive", "false");</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.dotNet = true;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.setOutputSoapObject(Request);</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.implicitTypes=true;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.dotNet = true; </i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.setAddAdornments(false);</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.encodingStyle = SoapSerializationEnvelope.XSD;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.enc = SoapSerializationEnvelope.ENC;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.xsd = SoapEnvelope.XSD;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> envelope.xsi = SoapEnvelope.XSI;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> </i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> androidHttpTransport.debug = true; </i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> SoapObject response;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> try</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> {</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> androidHttpTransport.call(SOAP_ACTION, envelope);</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> response = (SoapObject) envelope.bodyIn;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> return response;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> }</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> catch(Exception e)</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> {</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> e.printStackTrace();</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> }</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> return null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<br />
<i> </i>Here I am executing the request through <i>"</i><i>executeRequest</i><i>"</i> function. The result will be in <i>"</i><i>SoapObject</i><i>"</i> format. Now I will pass this result to my <i>"</i><i>LoginResult </i><i>"</i> class. In that class It will parse the response.<br />
<h4 style="text-align: left;">
<b><u>Parsing <i>SoapObject </i></u></b></h4>
<i> public LoginResult (SoapObject response){</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span>Boolean LoginStatus= false;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span>String Token = "" , ErrorOnFailure = "";</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span>ValidLogin = response.getProperty("IsValidLogin").toString();</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> if(ValidLogin.equalsIgnoreCase("true")){</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> LoginStatus=true;</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> Token = response.getProperty("Token").toString();</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> }else{</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span> ErrorOnFailure = response.getProperty("ErrorOnFailure").toString();</i><br />
<i> <span class="Apple-tab-span" style="white-space: pre;"> </span></i><i> } </i><br />
<br />
<i>}</i><br />
<i><br /></i>
<i><br /></i>
<i><br /></i>
</div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.comtag:blogger.com,1999:blog-7136242653797922563.post-59925648956484639622014-11-11T17:47:00.001+05:302015-08-12T15:23:43.632+05:30SQLite Database in android <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<span style="font-size: large;"> I</span>n this blog I am going to explain SQLite database in android. It is an Open Source Database, Also Its is embedded to all the android devices so there is no need to add any library to the application. You are need to import SQLiteDatabase and SQLiteOpenHelper for that.<br />
<br />
<i>import android.database.sqlite.SQLiteDatabase;<br />import android.database.sqlite.SQLiteOpenHelper;</i><br />
<br />
Also I an going to explain How to create a Database, Tables, Inserting values, Updating values, Deleting values from the Database operations in Android.<br />
<br />
More Information about SQLite : <a href="http://www.sqlite.org/" target="_blank"> http://www.sqlite.org</a><br />
More Information about SQLite Data-types: <a href="http://www.sqlite.org/datatype3.html">http://www.sqlite.org/datatype3.html</a><br />
<br />
<u><b>Creating Database</b></u><br />
<br />
First you are needed to create a class for database. Here I am going to create a class "<i>DbHelper</i>".<br />
<br />
<i>public class DbHelper {</i><br />
<i><br /></i>
<i>}</i><br />
<br />
Now I need to extend <i>SQLiteOpenHelper </i>on this class<i>. </i>While extending<i> </i>this we need to add constructor and some unimplemented methods. Also I am going to declare the "<i>DATABASE_VERSION" </i>and <i>"DATABASE_NAME" </i>on this class.<i> </i><br />
<br />
<i>import android.content.Context;</i><br />
<i>import android.database.sqlite.SQLiteDatabase;<br />import android.database.sqlite.SQLiteDatabase.CursorFactory;<br />import android.database.sqlite.SQLiteOpenHelper;<br /><br />public class DbHelper extends SQLiteOpenHelper {</i><br />
<br />
<i></i>
<i></i>
<i> private static final int DATABASE_VERSION = 3;<br /> public static final String DATABASE_NAME = "MyExample.sqlite";<br /> private Context context;<br /><br /> public DbHelper(Context myContext) {<br /> super(myContext, DATABASE_NAME, null, DATABASE_VERSION);<br /> this.context = myContext;<br /> }<br /><br /> @Override<br /> public void onCreate(SQLiteDatabase db) {<br /> // TODO Auto-generated method stub<br /> }<br /><br /> @Override<br /> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br /> // TODO Auto-generated method stub<br /> }<br /><br />}</i><br />
<br />
Here <i>"DATABASE_VERSION" </i>is the version if the database. If there is a version change it will automatically call the unimplemented method <i>"onUpgrade"</i>. In this method we need to specify the modification we need to done on the database.<br />
<br />
<i>"DATABASE_NAME" </i>is the name which the database is to be saved. While Initializing the database class id we add a path here "<i>DbPath</i>" is the path of Db to be stored, It will save the database on that location, like,<br />
<br />
<i>public DbHelper(Context myContext) {<br /> super(myContext, DbPath+DATABASE_NAME, null, DATABASE_VERSION);<br /> this.context = myContext;<br /> }</i><br />
<h4 style="text-align: left;">
<u>Table Creation:</u></h4>
Here I am going to create a table for storing user details,<br />
<br />
<i> public static final String USERS_TABLE_NAME = "Users"; //User Table Name</i><br />
<br />
/<i>/User Table Fields.</i><br />
<i> private static final String UserID = "UserID";</i><br />
<i> private static final String Email = "Email";</i><br />
<i> private static final String Name = "Name";</i><br />
<i> private static final String Role = "Role"; </i><br />
<br />
<i> //Table Creation Query</i><br />
<i> private static final String USERS_TABLE_CREATE = "CREATE TABLE " + USERS_TABLE_NAME + " (" + UserID + " INTEGER PRIMARY KEY, " + Email + " TEXT , "+ Name + " TEXT , "+ Role + " INTEGER );";</i><br />
<br />
<h4 style="text-align: left;">
<u>Insertion:</u></h4>
Here is a function to insert the details, Here I am passing an object of User and after inserting it will return the Id of inserted object.<br />
<br />
<i>public long SaveUsers(Users users){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = this.getWritableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>long id = 0;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try { </i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ContentValues values = new ContentValues();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(UserID, users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Email, users.getEmail());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Name, users.getName());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Role, users.getRole());</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>id = ceilDb.insert(USERS_TABLE_NAME, null, values);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Saved ID : "+id);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><br /></i>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return id;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<br />
<b><u>Deletion:</u></b><br />
<br />
Here is a function to insert the details, Here I am passing the id of User first it will check if the user is exist if the user exist then it will delete the row.<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><i>public long DeleteUsers(long Id){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = getReadableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID, Email, Name, Role};<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try{</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + Id, null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.delete(USERS_TABLE_NAME, UserID + "=" + Id , null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Deleted ID : "+Id);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return id;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<br />
<b><u>Updation:</u></b><br />
<br />
Here is a function to Update the details, Here I am passing an object of User first it will check if the user is exist if the user exist then it will delete the row.<br />
<br />
<i>public long UpdateUsers(Users users){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = this.getWritableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>long id = 0;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try { </i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ContentValues values = new ContentValues();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(UserID, users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Email, users.getEmail());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Name, users.getName());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Role, users.getRole());</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID , Email};</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + users.getUserID(), null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>id = ceilDb.update(USERS_TABLE_NAME, values, UserID + "=" + users.getUserID() , null);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Updated ID : "+users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return id;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<br />
<h4 style="text-align: left;">
<u>Retrive:</u></h4>
<div>
Here is a function to insert the details, Here I am passing the id of User first it will check if the user is exist if the user exist then it will take the details from db and return it.</div>
<div>
<br /></div>
<div>
<br /></div>
<i>public Users getUser(long Id){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = getReadableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Users users = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID, Email, Name, Role};<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try{</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + Id, null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users = new Users();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setUserID(c.getInt(0));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setEmail(c.getString(1));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setName(c.getString(2));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setRole(c.getInt(3));</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :>Get Users ");</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Retrive Users Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return users;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<br />
<br />
<h3 style="text-align: left;">
<u>Implementation to <i>DbHelper class.</i></u></h3>
<i>public class DbHelper extends SQLiteOpenHelper {</i><br />
<i><br /></i>
<i> private static final int DATABASE_VERSION = 3;<br /> public static final String DATABASE_NAME = "MyExample.sqlite";<br /> private Context context;</i><br />
<i><br /></i>
<i> public static final String USERS_TABLE_NAME = "Users"; //User Table Name</i><br />
<i><br /></i>
<i> //User Table Fields.</i><br />
<i> private static final String UserID = "UserID";</i><br />
<i> private static final String Email = "Email";</i><br />
<i> private static final String Name = "Name";</i><br />
<i> private static final String Role = "Role"; </i><br />
<i><br /></i>
<i> //User Table Creation Query</i><br />
<i> private static final String USERS_TABLE_CREATE = "CREATE TABLE " + USERS_TABLE_NAME + " (" + UserID + " INTEGER PRIMARY KEY, " + Email + " TEXT , "+ Name + " TEXT , "+ Role + " INTEGER );";</i><br />
<i><br /></i>
<i><br /> public DbHelper(Context myContext) {<br /> super(myContext, DATABASE_NAME, null, DATABASE_VERSION);<br /> this.context = myContext;<br /> }<br /><br /> @Override<br /> public void onCreate(SQLiteDatabase db) {<br /> db.execSQL(USERS_TABLE_CREATE);<br /> }<br /><br /> @Override<br /> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br /> // TODO Auto-generated method stub<br /> <br /> }</i><br />
<i><br /></i>
<i>//Function to insert User details</i><br />
<i> public long SaveUsers(Users users){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = this.getWritableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>long id = 0;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try { </i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ContentValues values = new ContentValues();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(UserID, users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Email, users.getEmail());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Name, users.getName());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Role, users.getRole());</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>id = ceilDb.insert(USERS_TABLE_NAME, null, values);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Saved ID : "+id);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><br /></i>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return id;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><br /></i>
<i>//Function to get user details</i><br />
<i>public Users getUser(long Id){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = getReadableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Users users = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID, Email, Name, Role};<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try{</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + Id, null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users = new Users();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setUserID(c.getInt(0));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setEmail(c.getString(1));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setName(c.getString(2));</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>users.setRole(c.getInt(3));</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :>Get Users ");</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Retrive Users Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return users;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><br /></i>
<i>//Function to Update Table</i><br />
<i>public long UpdateUsers(Users users){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = this.getWritableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>long id = 0;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try { </i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ContentValues values = new ContentValues();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(UserID, users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Email, users.getEmail());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Name, users.getName());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>values.put(Role, users.getRole());</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID , Email};</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + users.getUserID(), null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>id = ceilDb.update(USERS_TABLE_NAME, values, UserID + "=" + users.getUserID() , null);<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Updated ID : "+users.getUserID());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return id;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><br /></i>
<i>//Function to Delete User</i><br />
<i>public long DeleteUsers(long Id){</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>SQLiteDatabase ceilDb = getReadableDatabase();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>Cursor c = null;</i><br />
<span class="Apple-tab-span" style="white-space: pre;"><i> </i></span><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>String[] cols = { UserID, Email, Name, Role};<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>try{</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c = ceilDb.query(USERS_TABLE_NAME, cols, UserID + "=" + Id, null, null, null, null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>int k=c.getCount();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (k!=0) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>if (c.moveToFirst()) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>do {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.delete(USERS_TABLE_NAME, UserID + "=" + Id , null);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} while (c.moveToNext());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> User Deleted ID : "+Id);</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Db :> Failed to Save User, Error is: "+ e.getMessage().toString());</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>} <span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>c.close();</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>ceilDb.close();<span class="Apple-tab-span" style="white-space: pre;"> </span></i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>return </i><i>Id</i><i>;</i><br />
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>}</i><br />
<div>
<i><br /></i></div>
<i>}</i><br />
<h3 style="text-align: left;">
<i><u><br /></u></i></h3>
<h3 style="text-align: left;">
<i><u>Calling DB Functions</u></i></h3>
<span class="Apple-tab-span" style="white-space: pre;"> </span>When you initialize the object of <i>DbHelper </i>class it will check if the Db is exist if it doesn't exist it will create the Db and table by executing the methods <i> DbHelper </i>and <i>onCreate. </i>If there is a version change it will automatically call the unimplemented method <i>"onUpgrade"</i>. In this method we need to specify the modification we need to done on the database.<br />
<div>
<br /></div>
<div>
<b><u>Database Initialization</u></b></div>
<br />
<i> private DbHelper dbHelper = new DbHelper(context);</i><br />
<br />
<b><u>Calling Database Operations</u></b><br />
<br />
<b><u>Insertion</u></b><br />
<i>private DbHelper dbHelper = new DbHelper(context);</i><br />
<i>dbHelper.</i><i>SaveUsers(users) // here user is an object to insert.</i><br />
<br />
<b><u>Deletion</u></b><br />
<i>private DbHelper dbHelper = new DbHelper(context);</i><br />
<i>dbHelper.</i><i>DeleteUsers</i><i>(</i><i>Id</i><i>) </i><i>// here Id is an id of the user to be deleted</i><br />
<i><br /></i>
<b><u>Updation</u></b><br />
<i>private DbHelper dbHelper = new DbHelper(context);</i><br />
<i>dbHelper.</i><i>UpdateUsers</i><i>(users) // here user is an object to be updated.</i><br />
<i><br /></i>
<b><u>Retrive</u></b><br />
<i>private DbHelper dbHelper = new DbHelper(context);</i><br />
<i>users = </i><i>dbHelper.</i><i>getUser(Id)</i><i>// here Id is an id of the user</i><br />
<br /></div>
<div class="blogger-post-footer">For More http://shidhin.net
Please check our Company Website B-Mates Technologies
http://b-mates.com</div>SHIDHIN TShttp://www.blogger.com/profile/12751742912041627665noreply@blogger.com0