Akram

This user hasn't shared any biographical information


Posts by Akram

Using Android NDK to Call Native Code From Android Application

 

The Android NDK is a set of tools that allows Android application developers to embed native machine code compiled from C and/or C++ source files into their application packages.
While you were able to access native code via Java Native Interface (JNI) all along, You would’ve typically had to compile everything on your host computer for the target architecture, requiring you to have the entire toolchain on your development machine.
Android NDK (Native Development Kit) simplifies working with native code. It includes the entire toolchain needed to build for your target platform (ARM). It is designed to help you to create that shared library.

To do’s:

1. Create the Java class that represents the native code
2. Create header file for the native code.
3. Implement the native code by writing your C code
4. Compile everything and build you Shared Library
5. Use your native code inside Android activity
1. Create Native Library

Create a Native Library in src directorary in your Eclipse project.
/src/com.mobisoftinfotech/NativeLib.java

2. Create C Header File

In your project bin directory (in my case, /<workspace>/NDKDemo/bin), run javah tool to create the JNI header file.
NDKDemo/bin$ javah -jni com.mobisoftinfotech.NativeLib
Next, create a jni directory in your project directory ( in my case , <EclipseWorkspace>/NDKDemo/jni).

Next, copy the JNI header from <EclipseWorkspace>/NDKDemo/bin to <EclipseWorkspace>/NDKDemo/jni

3. Write the C Code

In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndkMathdemo.c file. This is where we’ll implement the native code. To start, copy the function signatures from the header file, and provide the implementation for those functions. In this example, the header file looks like this:

<EclipseWorkspace>/NDKDemo/jni/com_mobisoftinfotech_NativeLib.h

4. Build The Library

To build the library, first we need to create a makefile for how to compile the C code:
<EclipseWorkspace>/NDKDemo/jni/Android.mk

Next, we need to tell NDK how to build the shared library and put it in the correct place inside the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndkMathsdemo/ and inside this folder create the Application file:

<NDKHOME>/apps/ndkMathsdemo/Application

You can now to to your <NDKHOME> and run make APP=ndkMathsdemo

The output should look like this on terminal:-

android-ndk-r4$ make APP=ndkMathsdemo

Android NDK: Building for application ‘ndkMathsdemo’

Compile thumb : ndkMathsdemo <= sources/ndkMathdemo/ndkMathdemo.c

SharedLibrary : libndkMathdemo.so

Install : libndkMathsdemo.so => apps/ndk_demo/project/libs/armeabi

You can now refresh your Eclipse project and you should /lib/ directory containing your libndkMathsdemo.sofile.

5. Calling Native Code from Java

So now that we have the native C library implemented, compiled, and placed in the right place, let’s see how we can call it from our Activity. It’s actually rather simple – you just have to instantiate the instance of your NativeLib class and from there on, it’s just a regular Java object.

Source Code: NDKDemo.tar

Call log deletion in Android

In order to delete a call log entry in android device/ Emulator, first of all we must know where the log information is stored. Android stores the log information in its internal database as a content provider – “content://call_log/calls”. 

Call log Basics 

A content provider is used to share data between multiple applications. In Android, a content provider is a specialized type of data store that exposes standardized ways to retrieve and manipulate the stored data. 

The call log data is used by multiple applications and hence android stores it as a content provider. The Call Log provider contains information about placed and received calls. 

To do:

  1. Get uri for call log content provider.
  2. Query the content provider.
  3. Search the row in result set(curser) .
  4. Delete the row from content provider.

URI 

The call log content provider exposes a public URI “content://call_log/calls” for recent phone calls that uniquely identifies its data set. 

Uri allCalls = Uri.parse(“content://call_log/calls”); 

Querying a Content Provider 

To access database we  need to query the content provider by using the uri which the given content provider exposes. 

Cursor c = managedQuery(allCalls, null, null, null, null); 

Reading retrieved data for deleting particular log entry 

Cursor c stores record set of results regarding the log details. So, now in order to delete any record single record, call ContentResolver.delete() with the URI of a specific row. 

To delete multiple rows, call ContentResolver.delete() with the URI of the type of record to delete (for example, android.provider.CallLog.Calls.CONTENT_URI) and an SQL WHERE clause defining which rows to delete. 

Here we will delete the row by comparing ‘NUMBER’ column with the number entered by user. 

while(c.moveToNext()) 

    // get particular number for which log entry is to be deleted. 

    String strNumber= etNumberForCall.getText().toString(); 

    // make a selection clause. 

    String queryString= “NUMBER=’” + strNumber + “‘”; 

    Log.v(“Number”, queryString); 

    CallLogActivity.this.getContentResolver().delete(UriCalls,  queryString, null); 

Screen Shorts: 

  

enterNumber

DeleteScreen

ResultScreen

Following is complete source code: 

package com.mobisoftinfotech; 

import android.app.Activity;

import android.database.Cursor; 

import android.net.Uri;

import android.os.Bundle; 

import android.util.Log;

import android.view.View; 

import android.view.View.OnClickListener;

import android.widget.Button; 

import android.widget.EditText;

import android.widget.Toast; 

/** 

* @author Mohd. Akram 

*/ 

public class CallLogActivity extends Activity {

    /** Called when the activity is first created. */ 

    EditText etNumberForCall; 

    Button btnDeleteNumberFromCallLog; 

@Override 

 public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    etNumberForCall=(EditText)findViewById(R.id.EditText01); 

    btnDeleteNumberFromCallLog=(Button)findViewById(R.id.Button01); 

    btnDeleteNumberFromCallLog.setOnClickListener(new OnClickListener() { 

       @Override 

       public void onClick(View v) { 

           // TODO Auto-generated method stub 

          String strUriCalls=”content://call_log/calls”; 

          Uri UriCalls = Uri.parse(strUriCalls); 

          Cursor c = CallLogActivity.this.getContentResolver().query(UriCalls, null, null, null, null); 

          if (c.getCount()<=0) 

              { 

               Toast.makeText(getApplicationContext(), “Call log empty”,Toast.LENGTH_SHORT).show(); 

               etNumberForCall.setText(“”); 

               } 

          while (c.moveToNext()) 

              { 

               String strNumber= etNumberForCall.getText().toString(); 

               String queryString= “NUMBER=’” + strNumber + “‘”; 

                Log.v(“Number”, queryString); 

                int i=CallLogActivity.this.getContentResolver().delete(UriCalls, queryString, null); 

                etNumberForCall.setText(“”); 

                 if(i>=1) 

                     { 

                      Toast.makeText(getApplicationContext(), “Number deleted”, Toast.LENGTH_SHORT).show(); 

                      } 

                  else 

                      { 

                       Toast.makeText(getApplicationContext(), “No such number in call logs”, Toast.LENGTH_SHORT).show(); 

                       } 

                   } 

               } 

           }); 

       }