[go: up one dir, main page]

0% found this document useful (0 votes)
4 views9 pages

Programs 3,10,6

The document outlines the implementation of three Android applications: a text file open/save program, a JSON parsing program, and a message receiving program. Each application includes XML layout files and Java classes that handle user interactions, file operations, and JSON data processing. The manifest files specify permissions and application settings necessary for each app's functionality.

Uploaded by

jocox65721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Programs 3,10,6

The document outlines the implementation of three Android applications: a text file open/save program, a JSON parsing program, and a message receiving program. Each application includes XML layout files and Java classes that handle user interactions, file operations, and JSON data processing. The manifest files specify permissions and application settings necessary for each app's functionality.

Uploaded by

jocox65721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Create open save program

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"
android:id="@+id/btn_create" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OPEN"
android:id="@+id/btn_open" />
</LinearLayout>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:height="200dp"
android:id="@+id/txt_inp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/btn_save" />

</LinearLayout>

MainActivity.java
package com.example.textbox;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
public static final String FILE_NAME = "Example.txt";
Button btncreate, btnopen, btnsave;
EditText txtinp;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncreate=findViewById(R.id.btn_create);
btncreate.setOnClickListener(this);
btnopen=findViewById(R.id.btn_open);
btnopen.setOnClickListener(this);
btnsave=findViewById(R.id.btn_save);
btnsave.setOnClickListener(this);
txtinp=findViewById(R.id.txt_inp);

@Override
public void onClick(View v) {
if(v.equals(btncreate)) {
String text=txtinp.getText().toString();
FileOutputStream fos = null;

try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());

txtinp.setText(" ");
Toast.makeText(this, "File Saved "+ getFilesDir().getName()+ "
"+FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else if(v.equals(btnsave)) {
String text=txtinp.getText().toString();
FileOutputStream fos = null;

try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());

txtinp.setText(" ");
Toast.makeText(this, "File Saved"+ getFilesDir().getName()
+FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else if(v.equals(btnopen)) {
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

String text;

while ((text = br.readLine()) != null){


sb.append(text +"\n");
}
txtinp.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}
}

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.textbox"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Textbox"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>

Json Program
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_1"
android:text="Parse JSON" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textColor="@color/black"
android:id="@+id/txt_result" />

</LinearLayout>

Mainactivity.java
package com.example.json_prg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
Button btnjson;
TextView txtresult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnjson = findViewById(R.id.btn_1);
btnjson.setOnClickListener(this);
txtresult = findViewById(R.id.txt_result);
}

public void onClick(View v) {


if (v.equals(btnjson)) {
try {
InputStream is = getAssets().open("example.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String json = new String(buffer, "UTF-8");
JSONArray obj = new JSONArray(json);
txtresult.setText("");
for (int i = 0; i < obj.length(); i++) {
JSONObject ob1 = obj.getJSONObject(i);
String s1 = ob1.getString("name");
String s2 = ob1.getString("latitude");
String s3 = ob1.getString("longitude");
String s4 = ob1.getString("temperature");
txtresult.setText(txtresult.getText() + "Name:" + s1 +
" Latitude:" + s2 + " Longitude:" + s3 + " Temperature:" + s4 + "\
n"+"--------"+"\n");
}
} catch (Exception e) { }

}
}
}

example.json
[
{
"name":"Mysore" ,
"latitude": "92",
"longitude": "74",
"temperature":"24"
},
{
"name":"Bangalore" ,
"latitude": "36",
"longitude": "69",
"temperature":"20"
},
{
"name":"Udupi" ,
"latitude": "24",
"longitude": "42",
"temperature":"25"
},
{
"name":"Goa" ,
"latitude": "23",
"longitude": "63",
"temperature":"22"
}
]

Message program
Activity_main
[
{
"name":"Mysore" ,
"latitude": "92",
"longitude": "74",
"temperature":"24"
},
{
"name":"Bangalore" ,
"latitude": "36",
"longitude": "69",
"temperature":"20"
},
{
"name":"Udupi" ,
"latitude": "24",
"longitude": "42",
"temperature":"25"
},
{
"name":"Goa" ,
"latitude": "23",
"longitude": "63",
"temperature":"22"
}
]

MainActivity.java
package com.example.message_num;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


TextView txtnum, txtmsg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnum=findViewById(R.id.txt_num);
txtmsg=findViewById(R.id.txt_msg);
Bundle b = getIntent().getBundleExtra("data");
if(b!=null)
{
String s1= b.getString("num");
String s2= b.getString("msg");
txtnum.setText(s1);
Toast.makeText(this, "Message Received",
Toast.LENGTH_LONG).show();
txtmsg.setText(s2);
}
}
}

MySMSReceiver
package com.example.message_num;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class MySMSReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Object[] objmsg = (Object[])intent.getExtras().get("pdus");
for(int i=0; i<objmsg.length; i++) {
SmsMessage m = SmsMessage.createFromPdu((byte[])objmsg[i]);
Bundle b1 = new Bundle();
b1.putString("num",m.getOriginatingAddress());
b1.putString("msg",m.getMessageBody());
Intent it = new Intent(context, MainActivity.class);
it.putExtra("data", b1);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
break;
}
}
}

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message_num"
android:versionCode="1"
android:versionName="1.0"
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="32" />

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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Message_num"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<receiver android:name=".MySMSReceiver"
android:exported="true">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>

</manifest>

You might also like