Nothing Special   »   [go: up one dir, main page]

Practical:13: 2ceit509 Mobile Application Development

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

[ 2CEIT509 MOBILE APPLICATION DEVELOPMENT]

Practical:13

AIM - Create an application to receive sms, send sms and


display all inbox message into listview.

Submitted By : Prince Kalariya


Enrollment number : 19012021017

Department of Computer
Engineering/Information Technology

19012021017 Prince Kalariya


Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="4dp"
tools:context=".ui.MainActivity">

<EditText
android:id="@+id/editTextNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter mobile number"
android:textColor="@color/black"
tools:ignore="TouchTargetSizeCheck" />

<EditText
android:id="@+id/editTextMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message"
android:textColor="@color/black"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/btnSendMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android: > android:text="Send Message" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation1"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="470dp"
android:background="@color/cardview_light_background"
app:menu="@menu/bottom_nav_menu" />

</LinearLayout>

19012021017 Prince Kalariya


MainActivity.kt

package com.example.practical_13_19012021017.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.telephony.SmsManager
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.practical_13_19012021017.R
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {


lateinit var button: Button
lateinit var editTextNumber: EditText
lateinit var editTextMessage: EditText
private val permissionRequest = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomnav: BottomNavigationView =
findViewById(R.id.bottom_navigation1)

bottomnav.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.list1 -> {

Intent(this, SMS_list::class.java).also {
startActivity(it)
}
return@setOnNavigationItemSelectedListener true
}
else -> return@setOnNavigationItemSelectedListener false
}
}

editTextNumber = findViewById(R.id.editTextNum)
editTextMessage = findViewById(R.id.editTextMsg)
button = findViewById(R.id.btnSendMsg)
}
fun sendMessage(view: View) {
val permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
myMessage()

19012021017 Prince Kalariya


} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.SEND_SMS),
permissionRequest)
}
}
private fun myMessage() {
val myNumber: String = editTextNumber.text.toString().trim()
val myMsg: String = editTextMessage.text.toString().trim()
if (myNumber == "" || myMsg == "") {
Toast.makeText(this, "Field cannot be empty",
Toast.LENGTH_SHORT).show()
} else {
if (TextUtils.isDigitsOnly(myNumber)) {
val smsManager: SmsManager = SmsManager.getDefault()
smsManager.sendTextMessage(myNumber, null, myMsg, null,
null)
Toast.makeText(this, "Message Sent",
Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Please enter the correct number",
Toast.LENGTH_SHORT).show()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions:
Array<out String>, grantResults:
IntArray) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults)
if (requestCode == permissionRequest) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
myMessage();
} else {
Toast.makeText(this, "You don't have required permission to
send a message",
Toast.LENGTH_SHORT).show();
}
}
}
}

Activity_sms_list.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"
tools:context=".ui.SMS_list">

19012021017 Prince Kalariya


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="4dp">

<ListView
android:id="@+id/all_sms_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/cardview_light_background"
app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

SMS_list.kt

package com.example.practical_13_19012021017.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.practical_13_19012021017.R
import com.google.android.material.bottomnavigation.BottomNavigationView

class SMS_list : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
if(ContextCompat.checkSelfPermission(getBaseContext(),
"android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {}
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.READ_SMS), 1)

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sms_list)
var msgData = ""

19012021017 Prince Kalariya


val bottomnav: BottomNavigationView =
findViewById(R.id.bottom_navigation)

bottomnav.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.sms1 -> {
Intent(this, MainActivity::class.java).also {
startActivity(it)
}
return@setOnNavigationItemSelectedListener true
}
else -> return@setOnNavigationItemSelectedListener false
}
}
val sms = ArrayList<Any>()

val uriSms: Uri = Uri.parse("content://sms/inbox")


val cursor: Cursor? = contentResolver.query(
uriSms,
arrayOf("_id", "address", "date", "body"),
null,
null,
null
)

if(cursor != null) {
cursor.moveToFirst()
}
if (cursor != null) {
while (cursor.moveToNext()) {
val address: String = cursor.getString(1)
val body: String = cursor.getString(3)
sms.add("Address = " + address + "\nSMS = " + body+"\n")
}
}

val adapter = ArrayAdapter(this, R.layout.simple_list_item_1, sms


as List<Any?>)

val listView:ListView = findViewById(R.id.all_sms_list)


listView.setAdapter(adapter)
}
}

Simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"

19012021017 Prince Kalariya


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical_13_19012021017">

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


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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.practical_13_19012021017">
<activity
android:name=".ui.SMS_list"
android:exported="true" />

<activity
android:name=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

19012021017 Prince Kalariya


Bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/sms1"
android:icon="@drawable/ic_baseline_message_24"
android:title="New" />
<item
android:id="@+id/list1"
android:icon="@drawable/ic_baseline_list_24"
android:title ="All" />

</menu>

String.xml

<resources>
<string name="app_name">practical_13_19012021017</string>
<string
name="title_activity_bottom_navigation_menu">Bottom_Navigation_Menu</string
>
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="title_notifications">Notifications</string>
<string
name="title_activity_simple_list_item1">simple_list_item_1</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="next">Next</string>
<string name="previous">Previous</string>

<string name="hello_first_fragment">Hello first fragment</string>


<string name="hello_second_fragment">Hello second fragment. Arg:
%1$s</string>
</resources>

19012021017 Prince Kalariya


Output :

19012021017 Prince Kalariya

You might also like