Sunday, 14 February 2016

Android Programming Tutorial 66: Displaying All Messages From Mobile Inbox Example in Android Studio

Displaying All Messages From Mobile Inbox In Android:

In the below example we are discussing, how we display the total messages from the phone inbox.


Message Inbox Example in Android:

File >> New >> Android Application
Enter Project name: InboxMessageExample
Package: com.listviewwithdb
Keep other default selections, click Next until you reach Finish

                                    


Copy and Past the following code in the MainActivity.java
package com.listviewwithdb;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ListView phoneBook;
    Context context;
    TextView txt_SMSSend;
    TextView txt_MsgBody;
    Cursor cursor;
    String[] arrayBody;
    int[] arrayID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context =this;

        phoneBook = (ListView)findViewById(R.id.listViewSMS);
        arrayBody = new String[]{"address","body"};
        arrayID = new int[]{R.id.textSMSSend,R.id.textMsgBody};

        cursor = getContentResolver().query(Uri.parse("content://sms/inbox"),null,null,null,null);
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.content_main,cursor,arrayBody,arrayID);
        Log.v("add","add"+simpleCursorAdapter);
        phoneBook.setAdapter(simpleCursorAdapter);
        Log.v("Phone","Phone"+phoneBook);

        phoneBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            txt_SMSSend = (TextView)view.findViewById(R.id.textSMSSend);
            txt_MsgBody = (TextView)view.findViewById(R.id.textMsgBody);
            String smsSend = txt_SMSSend.getText().toString();
            String msgBody = txt_MsgBody.getText().toString();

                final AlertDialog dialog = new AlertDialog.Builder(context).create();
                dialog.setTitle("SMS Form:"+smsSend);
                dialog.setIcon(android.R.drawable.ic_dialog_info);
                dialog.setMessage(msgBody);
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        return;
                    }
                });
                dialog.show();
            }
        });
    }
}
Copy and Past the following code in the layout file called activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D1FFFF"
    tools:context="com.listviewwithdb.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewSMS"
        android:dividerHeight="0.1dp"
        android:divider="#0000CC" />
</RelativeLayout>



 Copy and Past the following code in the
Manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.listviewwithdb">

    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Copy and Past the following code in the layout file called item_data.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:id="@+id/textSMSSend"
        android:paddingLeft="2dp"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textColor="#0000FF"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:id="@+id/textMsgBody"
        android:paddingLeft="2dp"
        android:textStyle="bold"
        android:layout_below="@+id/textSMSSend"
        android:textSize="20dp"
        android:textColor="#5C002C"/>
</RelativeLayout>


Run Your Project as the below by clicking on shift+10 or like the below-->Android Application and observe the output
 
Output:

                        
 Note:

Here iam not displaying Other screen of the out put because of security reasons, because i do not want expose my messages

This Example will works on the devices(mobiles) and if the messages are there in the emulators our application also works in emulator.
  

Please Send Your Comments To ambilpura.sunil@gmail.com


Stay Tune For Next Tutorial... In Android:

No comments:

Post a Comment