Think Android

A Place for Android geeks to get solution and Exchange knowledge.

How to register my own Application subclass in Xamarin.Android?

by admin
In Java We would add a line to the manifest and pass it the namespace and name of my application: In Xamarin, there is the [Application] attribute but the documentation states that the Name member is not supported. So how do I get my subclass registered? Where to apply the attribute to? If I add the attribute to my subclass, I get: System.NotSupportedException: Unable to activate instance of type Android.MyApplication from native handle 14d00019 After Reading so many blogs I have resolved it, I have Gone through the below official documentaion and found some important informations. https://docs.microsoft.com/en-us/xamarin/android/internals/architecture#Java_Activation There are two scenarios in which the (IntPtr, JniHandleOwnership) constructor must be manually provided on a Managed Callable Wrapper subclass: Android.App.Application is subclassed. Application is special; the default Applicaton constructor will never be invoked, and the (IntPtr, JniHandleOwnership) constructor must instead be provided. Virtual method invocation from a base class constructor. Note that (2) is a leaky abstraction. In Java, as in C#, calls to virtual methods from a constructor always invoke the most derived method implementation. For example, the TextView(Context, AttributeSet, int) constructor invokes the virtual method TextView.getDefaultMovementMethod(), which is bound as the TextView.DefaultMovementMethod property. Thus, if a type LogTextBox were to (1) subclass TextView, (2) override TextView.DefaultMovementMethod, and (3) activate an instance of that class via XML, the overridden DefaultMovementMethod property would be invoked before the ACW constructor had a chance to execute, and it would occur before the C# constructor had a chance to execute. This is supported by instantiating an instance LogTextBox through the LogTextView(IntPtr, JniHandleOwnership) constructor when the ACW LogTextBox instance first enters managed code, and then invoking the LogTextBox(Context, IAttributeSet, int) constructor on the same instance when the ACW constructor executes. Solution: [Application] public class MyApplication : Application { public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip) { } }

Building Apk through Jenkins.

by admin
This post will show how to automate the deployment process of a Android Application using Jenkins Continuous Integration – to build the project And Archive the built artifacts(APK) and publish the Android lint reports. 1. Download and Install Jenkins as a Windows Service Navigate to https://jenkins.io/index.html and download jenkins zip file and install it. After installing you can Click here to download the jenkins

Send and Receive SMS using arduino and GSM shield sim900.

by admin
Hi Please prepare the circuit as shown in image. code:
#include "SIM900.h"
#include 
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup()
{
     //Serial connection.
     Serial.begin(9600);
     Serial.println("GSM Shield testing.");
     //Start configuration of shield with baudrate.
     //For http uses is raccomanded to use 4800 or slower.
     if (gsm.begin(2400)) {
          Serial.println("\nstatus=READY");
          started=true;
     } else Serial.println("\nstatus=IDLE");

     if(started) {
          //Enable this two lines if you want to send an SMS.
          if (sms.SendSMS("+919036634994", "Arduino SMS"))
          Serial.println("\nSMS sent OK");
     }

};

void loop()
{
     if(started) {
          //Read if there are messages on SIM card and print them.
        /**  deprecated method
        if(gsm.readSMS(smsbuffer, 160, n, 20)) {
               Serial.println(n);
               Serial.println(smsbuffer);
          }
          **/
          //get 1st sms
          sms.GetSMS(1,n,20,smsbuffer,160);
          Serial.println(n);
          Serial.println(smsbuffer);          
          delay(1000);
     }
};
    

There is this special kind of ropes made from some non-homogeneous material. Because of this special material, the rope takes exactly one hour to burn if lighted from one end. However, as the material is not uniform across the rope, different parts of rope may take different amount of time. So for example, one half may take 1 minute where as other half may take 59 minutes to burn. 1) You are given two such ropes and you need to measure 45 minutes. 2) You are given only one such rope and you need to measure 15 minutes.

by admin
Solution for first question pretty simple comparision to sencond. Solution 1: Burn rope A from both end and rope B from one end, Once A fully burned up till that time we burned rope for 30 minutes and then start fire from another side of B so remained rope will take to complete burn 15 minutes more Thus, we have got 30+15 = 45 minutes. Solution 2: We are given a rope of 1-hour burn time and we need to measure 15 minutes. Doubling the speed is easy by burning the rope from both the ends but the same is not true when you need to increase the speed four fold. For example, if you cut a rope into two halves H1 and H2 with burning time 10 and 50 respectively. Now, burning both of them from both the ends does not help as H1 will burn in 5 minutes where as H2 will take 25 minutes. Anyways, the solution is , you have to cut the rope into two pieces ( need not be half, because of non-uniformity it does not matter whether you cut at the middle point or not ) P1 and P2. Note that we need to measure 15 minute with 60-minutes burn time rope. Or in short, given a rope of length L( in time when burned from one end ) we need to measure L/4. Burn, P1 and P2 from both the ends. P1 = 30-x and P2 = 30+x where x \in [0,30). Because we are burning from both the ends, P1 will get burnt in 15-(x/2) minutes. For P2 , 30+x – 2*(15-(x/2)) = 2x, because P2 is also burning from both the ends. At the end of 15-(x/2) minutes P2 will have length 2x remaining. To measure exactly 15 minutes we need to measure time x/2 after 15-(x/2) has elapsed. We are again back to the same problem. We have a rope of length 2x and we need to measure x/2 which is increasing the speed four fold. So, we can keep on repeating this experiment. How long do we need to repeat this? Well, until both the pieces finish burning exactly at the same time. Why? Say we have a rope of length y and we need to measure y/4. Two pieces P1 and P2 measures y-z and z. When both are lighted from both the ends time taken will be (y-z)/2 and z/2 respectively. If they finish burning at the same time then (y-z)/2 = z/2 => y = 2z Burning time was z/2 = y/4 which is what we wanted.

Difference between Parcelable and Serializable?

by admin
In order to develop an Android App, we all learn that we cannot just pass object references to activities and fragments, we have to put those in an Intent / Bundle. Passing primitive data types like string, integer, float, etc. through intents is quite easy in Android. All you have to do is put the data with unique key in intents and send it to another activity. If a user wants to send Java objects through intent, Java class should be implemented using the Parcelable interface. Serialization, on the other hand, is a Java interface that allows users to implement the interface which gets marked as Serializable. public class User { String UserName; String Reg; public User(String name,String reg){ UserName=name; Reg=reg; } } Above is the simple User class with three private fields and getters and setters. Here we can not pass objects of this class between activities/services. To do this we need to make this class parcelable or serializable. Here taking an example of Parcable public class User implements Parcelable{ String UserName; String Reg; public User(String name,String reg ){ UserName=name; Reg=reg; } //parceling part public User(Parcel in){ String[] data= new String[2]; in.readStringArray(data); this.UserName= data[0]; this.Reg= data[1]; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeStringArray(new String[]{this.UserName,this.Reg}); } public static final Parcelable.Creator CREATOR= new Parcelable.Creator() { @Override public User createFromParcel(Parcel source) { // TODO Auto-generated method stub return new User(source); //using parcelable constructor } @Override public User[] newArray(int size) { // TODO Auto-generated method stub return new User[size]; } }; }

You have three jug whose capacity are 8 ltr.,5 ltr. and 3 ltr. and no one jug is calibrated then how can you divide 8 lt. water in two equal parts.?

by admin
Fill the 5 ltr. Jug with water and then pour 3 ltr. In the jug having capacity of 3 lts. So 5 lt jug will have 2 lts in it. Now empty the 3 lts jug and fill these 2 lts in that. The 3 ltr jug is still having the capacity of 1 ltr more. Again fill the 5 ltr jug with water and pour 1 ltr to 3 ltr jug. Now the 5 ltr Jug will contains 4 ltr in it. Put these 4 ltr water in 8 ltr Jug and follow the same procedure to fill the 5 ltr with 4 ltr water. Now let we make it more clear. follow the below steps: 8ltr 5ltr 3ltr ------------------------------------ 8 0 0 3 5 0 3 2 3 6 2 0 6 0 2 1 5 2 1 4 3 4 4 0 Done  

Get the temperature and humidity from dht22 sensor(Part 1).

by admin
1.Connect pin 1 (on the left) of the sensor to +5V *NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 to 3.3V instead of 5V! 2.Connect pin 2 of the sensor to whatever your DHTPIN is 3. Connect pin 4 (on the right) of the sensor to GROUND 4.Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor(As shown in image )

Send Data to IOT Event Hub using http request

by admin
Event Hubs is a service that processes large amounts of event data (telemetry) from connected devices and applications. In order to complete this blog you must have a azure account. Also you should know the following things about event hub. Event Hub is service (kind of a service bus) which collect large amount of data and store data for transforming it to real-time Analytics. Two cons over IOT event hub 1) does not provide more secure connection. 2) data communication not bi-directional (only used for device-to-cloud scenarios). Please follow the following steps to send the telemetry data to Azure event hub. 1.Log on to the Azure classic portal, and click NEW at the bottom of the screen. 2.Click App Services, then Service Bus, then Event Hub, then Quick Create. 3.follow the steps given https://azure.microsoft.com/en-in/documentation/articles/event-hubs-csharp-ephcs-getstarted/

How AsyncTask works in Android?

by admin
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask. How many AsyncTask can be started at a time in an Android app? Ans. The ThreadPoolExecutor has a corePoolSize, a maxPoolSize and a QueueCapacity. If all of your threads from the corePoolSize are used, any other task are put in the queue of each of the threads from the core pool. After these queues are full, a new thread is being initialized, as long as it is within the limits of the maxPoolSize.

CORE_POOL_SIZE = CPU_COUNT + 1;   

CPU_COUNT = Runtime.getRuntime().availableProcessors();

MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; For more information you refer the source code of Async http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.4_r1-robolectric-1/android/os/AsyncTask.java Note: It also depends on android api level.