Wednesday 1 July 2015

Debugging Android Apps on an Actual Device.

Hi!

Before I start to share my experience in setting up my Samsung Galaxy Note II with IntelliJ IDEA (my IDE), kindly find other resources which might help you connect your android device to your IDE.

http://developer.android.com/tools/device.html

http://developer.android.com/tools/extras/oem-usb.html

Here we go!

There are essentially 3 things you need to do:
  1. Enable Debugging Mode on your android phone.
  2. Install usb drivers for adb on your computer.
  3. Amend ApplicationManifest.xml file (IntelliJ IDEA) or build.gradle file (Android Studio)

Step 1 - Enabling Debugging Mode

The Developer options is required to enable Debugging Mode, however, in Android 4.2 and above it is hidden by default.

In Android 4.3, go to Settings > More > About device and tap Build number seven times to make the Developer options visible. Return to the previous screen to find the Developer options visible at the bottom.

Ensure the Developer options is ON and USB debugging enabled 

Step 2 - Install USB Driver

Kindly refer to the link http://developer.android.com/tools/extras/oem-usb.html for usb driver specific to your phone or check your manufacturers website.

In my case, although Samsung was listed on the developer.android.com page, I use Samsung Kies to install the required drivers.

Connect your phone to your computer through a usb cable and Windows would automatically install basic usb drivers.
Launch Samsung Kies on your computer then select Tools > Reinstall device driver to install the all drivers.
On your computer, click on the Start icon then type "Device Manager". Select Device Manager from the list.
Your device should be identified under the Portable Devices with no warning.

You may also find an RSA key message on your phone asking to allow debugging through this computer, just accept that.


Step 3 - Amend AndroidManifest

In the ApplicationManifest.xml file, add android:debuggable ="true" in the <application> element and suppress the warning by adding xmlns:tools="http://schemas.android.com/tools" in the <manifest> element.

In build.gradle file, the approach is slightly different (for those using Android Studio).

Kindly find an illustration below.


ApplicationManifest.xml file

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="course.examples.maplocation" android:versionCode="1" android:versionName="1.0" > <application android:debuggable="true" ... >

Build.gradle file

android { buildTypes { debug { debuggable true }




That's it!

You should find your device listed when you try to run your application, however, ensure in your application configuration, the Target Device is either set to Show chooser dialog or USB device else your application would run on the default emulator.

A physical Samsung Note II device shown on the list of devices

Thanks for finding the time to read this.

If you find this article helpful, share it, follow me and drop a comment - you could also suggest other ways to improve our lives just in case I missed anything. :)

Hearty Regards

Thursday 25 June 2015

C Programming - FILE "rw+" Mode Not Working

Hi!

I have decided to share a project which I created in C language just for some demonstration.

There is a particular scenario addressed in this code, which has to do with overwriting some information in the existing filestream.

In C, you have the syntax as fopen("myfile", "rw+" ) - or "r+w" in other models - to open the file for Read and Write operation then you use the fseek() method to move the file pointer position in file and state the write size to overwrite that record.

However, I discovered that Visual Studio does not allow you to open a file in "rw+" mode and neither does Codeblocks. Probably earlier compilers supported this mode, which I did not experiment.

Should this be the end of the world as far as my project is concerned? Absolutely not!

Find a workaround below - I opened the FILE pointer in "a+" mode and modified the code to refer to the last corresponding record to my test criteria.

Kindly review, share and comment - we all will learn.


#include <stdio.h>
#include <string.h>
#define clrscr() system("cls")
struct customer {
 long no;
 char name[30];
 double bal;
};

struct transaction {
 char type;
 long custno;
 double amount;
 double prvbal;
};

void rectrans() {
 FILE *fp, *fpt;
 long custNumber, size = sizeof(struct customer), sizet = sizeof(struct transaction);
 double custBalance;
 char custName[30];
 struct customer cust;
 struct transaction trnx;
 char flag = 'y', transtype;
 unsigned int recordfound;
 double namount;


 if ((fp = fopen("customer", "a+")) == NULL) {
  printf("\nSorry, server cannot be accessed at this time.\n");
  fclose(fp);
  return;
 }

 if ((fpt = fopen("transaction", "a+")) == NULL) {
  printf("\nSorry, server cannot be accessed at this time.\n");
  fclose(fpt);
  return;
 }
 while (flag == 'y') {
  clrscr();

  recordfound = 0;
  printf("\nEnter customer account number: ");
  scanf(" %ld", &custNumber);

  while ((fread(&cust, size, 1, fp)) != NULL) {
   // set customer Balance and Name to the last matching record.
   if (custNumber == cust.no) {
    recordfound++;
    custBalance = cust.bal;
    strcpy(custName, cust.name);
   }
  }

  if (recordfound < 1) {
   printf("\nInvalid account number.");
  }
  else {
   // Print the customer details
   printf(" %-s \nBalance: %20.2lf\n", custName, custBalance);

   // Request transaction information
   do {
    printf("\nWithdrawal or Deposit? (w/d): ");
    scanf(" %c", &transtype);
    lcase(&transtype);
   } while (transtype != 'w' && transtype != 'd');

   printf("\nEnter transaction amount: ");
   scanf(" %lf", &namount);

   switch (transtype) {
   case 'd':
    printf("\n\Deposit processing ...");

    // preloading transaction data
    trnx.custno = custNumber;
    trnx.prvbal = custBalance;
    trnx.amount = namount;
    trnx.type = 'D';

    // preloading customer data
    cust.bal = custBalance + trnx.amount;
    strcpy(cust.name, custName);
    cust.no = custNumber;

    // write the customer transaction to transaction file
                                // and update customer file
    fseek(fp, -size, SEEK_CUR);
    fwrite(&cust, size, 1, fp);
    fwrite(&trnx, sizet, 1, fpt);
    break;
   case 'w':
    printf("\n\Withdrawal processing ...");
    if ((custBalance - namount) >= 200) {
     // retrieve information relevant for trnx from cust
     trnx.custno = custNumber;
     trnx.prvbal = custBalance;
     trnx.amount = namount;
     trnx.type = 'W';
     cust.bal = custBalance - trnx.amount;
     strcpy(cust.name, custName);
     cust.no = custNumber;

     // write the customer transaction to transaction file
                                        // and update customer file
                                        // (long)(-size) typecast -size to long. 
                                        // fseek() returns position to a struct 
                                        // size backward from the current (1) cursor position.  

     fseek(fp, -size, SEEK_CUR); 
     fwrite(&cust, size, 1, fp);
     fwrite(&trnx, sizet, 1, fpt);
    }
    else {
     printf("\nInsufficient fund in this account.");
    }
    break;
   }
  }

  // rewind customer file fp to start search over for the next account. 
  rewind(fp);
  flag = 'n'; // this line stops the loop.
 }
 // close all fp and fpt
 fclose(fp);
 fclose(fpt);
 return;
}

Wednesday 12 June 2013

Visual Studio .NET: Rules are meant to be broken (SQL)

Hi Friends!

It is obvious that connecting to a database using Visual Studio .NET (C# or VB) requires two methods:

1.    Design-Time
2.    Runtime Objects.

In this case, we would stick to the Runtime Objects using a Data Command and an OleDbConnection.

{
string connString="Provider=Microsoft.ACE.OleDB.12.0;Data Source=C:\\myFolder\\sampledb.accdb";
public OleDbConnection dbConn = new OledbConnection(connString);
try
{
    dbConn.Open();
}
Catch (OleDbException e)
{
    // errors goes here
}
}

Here is my point, you may get your update or insert queries not working exactly as planned even after checking through your codes. Simply bend the rule. An example is provided below:

{
    string cmdString="UPDATE table SET col1=@val1,col2=@val2 WHERE id=@id";
    OleDbCommand oleCmd = new OleDbCommand();
    oleCmd.Connection =dbConn;
    oleCmd.CommandType =CommandType.Text;
    oleCmd.CommandText =cmdString;
    oleCmd.Parameters.Add("@val1",OleDbType.Char).Value=Textbox1.Text;
    oleCmd.Parameters.Add("@val2",OleDbType.Char).Value=Textbox2.Text;
    oleCmd.Parameters.Add("@id",OleDbType.Char).Value=id.Text;

    try
    {
        int i=oleCmd.ExecuteNonQuery();
        if (int i==0)
        {
             //failed update statement
            MessageBox.Show("Failed Statement");
        }
    }
    finally
    {
        oleCmd.Dispose()
        dbConn.Close()
    }
}

First, I would advice you add command parameters in the same order you use them in your command string so as to avoid inserting data into the wrong field. I also find that the WHERE clause does not work when you do not add that field as the last parameter in your command.

An alternative to the above rule is to use the traditional method, say your update failed with the message "Failed Statement" as coded.

string cmdString="UPDATE table SET col1=@val1,col2=@val2 WHERE id=" + refID
...
oleCmd.Parameters.Add("@val1",OleDbType.Char).Value=Textbox1.Text;
oleCmd.Parameters.Add("@val2",OleDbType.Char).Value=Textbox2.Text;

Now I do not use a parameter for the WHERE clause, rather I use a variable which must have been initiated from another event or method.
I find this approached very helpful, especially after debugging my code and I don't seem to discover the error on time.
Sometimes your programm is unable to interpret the value of your WHERE parameter @ID due to data type inconsistency with that defined in your database or program just being funny.

It's better to deliver a working application than to be late.

Hearty Regards,
AdeLeke

Thursday 13 October 2011

Current .ASP Project

Hi All!

I have been so occupied with some work lately, but it's never enough for an excuse for not posting my blogs.

You may find yourself in my kind of situation where you need to programme a web site for a client and you have got deadline plus an unfinished work for another client at hand. Never worry; you do not have to go crazy. Just stay focused and try the best you can. Your work may get delivered after your supposed deadline, but give your clients something to marvel at.

Keep your client in awe of you. That is the secret.

I have this project I am working on right now and it takes so much of my time. Here is another job I need to do but I hardly get time to work on it with a stable mind so I am looking forward to pausing the former for the latter.

You could try this. Just make sure you don't put yourself in the furnace: it helps.

Cheers.

L :-)

Friday 22 July 2011

VS IDE

Morning!

It's a mild morning here and i have this cold that makes me sneeze now and then. With a towel on my lap to wipe the catarrh off my nose and clearing my throat intermittently, I write you this blog about the Visual Studio IDE.

Before you can fully enjoy the capabilities of VS (referring to Visual Studio) you'll need to understand its environment. I'll introduce you to the IDE (Integrated Development Environment) and the code structure of VS.NET. Just like VB 6 to BASIC, VB.NET has a different code structure to its predecessors. Although, if you are a VB 6 user getting to know the VB.NET, you may find that the IDE relates to you.

VB.NET comes with an IDE that is enhanced for effective coding and design. Depending on what platform you'll be working on, you can customise your IDE to suite your programming language need or application development need. That is, for example, you may want the IDE to be displayed to meet either your vb.net programming needs or your web development needs.

The Toolbox and other explorers/windows within VS now have the autohide fuction; this allows the objects to clear off the screen to give you more space in the Document Area (this is where you do your actual design or coding).

You also need to know that Visual Studio .NET groups your projects in a Solution (.sln). This imlies that you can have more than one (1) Project (.vbproj / .csproj), of different platforms, co-exist in a Solution. Any project removed from a solution is not actually deleted. Also you may decide to include an existing project from one Solution to another.

This is especially advantageous when developing applications of multiple platforms, which will require different teams to work with their programming language of specialty.

Take for instance, you and a friend are trying to develop this application for a school library. The school library wants to have an application running on the library's frontdesk that registers a borrowed or returned book just by student identification number. Then the application will pull students informaion from the school's web server. You are the "genius" in VB.NET but have no programming knowledge about servers. Now your friend creates ASP.NET 4 web sites in C# and is so grounded in server programming. And he says: "that's a brilliant idea my friend; let's develope a solution with two (2) projects in it - vb.net and c# projects."


Generally, the code window comes up when you double-click an object in Design view. You may also click on the form in the Solution Explorer; in the toolbox that appear at the top of the Solution Explorer press view code button.


VS .NET is so rooted in the OOP (Object Oriented Programming) concept. This implies that one object/class inherits properties from its parent class just like you inherit traits from your parents.


I hope by now you have some glimpse about VS and you have fell in love with it. Look out further for my blogs on VS as I expose you to many tricks you never knew or thought could be so awesome.


This cold is driving me nuts, so i'll simply say goodbye!

Thursday 21 July 2011

lean .NET Skill

Morning!
 
You feel unprofessional when it comes to programming and say to yourself: "this BASIC knowledge is not getting me anywhere." Actually, you don't have to panic. Every professional out there now once felt like that so it would be alien-like not to feel that way.
I'm happy to tell you that your BASIC or C++ skill is not lost, since .NET is just an upgrade of the BASICs in programming. Just have in mind that the .NET comes with greater advantage and if you're patient enough you'll reap of the fruit.

First, get a VisualStudio 2005, 2008, or 2010 application. Any version at all is okay for a start, be it Enterpise, Professional, or Ultimate. As you progress you may find a need to go for a higher version. This could be as a result of program features such as Cloud programming that is around now or .NET 4 for web site development. Actually, the needs are endless.

Second, prepare your mind to be a great programmer and you'll have to keep practicing.
Remember: "Rome was not built in a day".

Third, get yourself some books to read (www.wrox.com). All information you need is hid in some books and the experience, of course, is hid in some brains. You'll have to read works of others to, such as my blogs and blogs of other programmers to help.

You may know how to solve a problem one way, but when you learn from others, you may find out that you've been doing things the hard way or rather wasting resources with your programs.

In my next editions of this blog, i'll be introducing you to ASP.NET 4 and the general concept of programming in Visual Studio. I'll rather call it "VS".