0% found this document useful (0 votes)
34 views

MobileApplicationDevelopment Codes

This document discusses various Android concepts including: 1. The activity lifecycle diagram and different states an activity can be in. 2. How to send a broadcast using an intent with specific action and category. 3. The SQLite database code with functions like onCreate, add, get all, update, delete and get count to manage contacts data. 4. The Volley library basics to make HTTP requests and parse JSON response. 5. Example Android code to add contact values to SQLite database and retrieve the list of contacts.

Uploaded by

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

MobileApplicationDevelopment Codes

This document discusses various Android concepts including: 1. The activity lifecycle diagram and different states an activity can be in. 2. How to send a broadcast using an intent with specific action and category. 3. The SQLite database code with functions like onCreate, add, get all, update, delete and get count to manage contacts data. 4. The Volley library basics to make HTTP requests and parse JSON response. 5. Example Android code to add contact values to SQLite database and retrieve the list of contacts.

Uploaded by

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

ACTIVITY LIFE CYCLE DIAGRAM:

BROADCAST RE RECEIVER:

BROADCAST SENDER:
<action android:name=”android.intent.action.SEND”/>
<category android:name=”android.intent.category.DEFAULT”/>
<data android:mimeType=”image/*”/>
DATABASE CODES (MyDbHelper)

1. //On Create Function


public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create="CREATE TABLE "+Params.TName+"("+
Params.key_id+" INTEGER PRIMARY KEY,"+
Params.key_name+" TEXT,"+
Params.key_phone+" TEXT"+ ")";
sqLiteDatabase.execSQL(create);
}

2. //Add Function
public void AddContact(Contact contact){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Params.key_name,contact.getName());
values.put(Params.key_phone,contact.getPhonenumber());
db.insert(Params.TName,null,values);
Log.d("MyDb","Successfully inserted");
db.close();
}
3. //Get All Contact Function
public List<Contact> getAllContact(){
List<Contact> contactList=new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
String query="SELECT * FROM "+ Params.TName;
Cursor cursor=db.rawQuery(query,null);
if(cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhonenumber(cursor.getString(2));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
4. //Update Function
public int updateContact(Contact contact){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Params.key_name,contact.getName());
values.put(Params.key_phone,contact.getPhonenumber());
return db.update(Params.TName,values, Where Caluse: Params.key_id+"=?",
new String[]{String.valueOf(contact.getId())});
}

5. //Delete Function
public void Delete(int id){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(Params.TName, Where Caluse: Params.key_id+"=?",
new String[]{String.valueOf(id)});
db.close();
}
6. //Count Function
public int getcount(){
String query="SELECT * FROM "+Params.TName;
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery(query,null);
return cursor.getCount();
}
}
VOLLEY LIBRARY:
1.Add Dependecy to build.gradle file:
dependencies {
implementation 'com.android.volley:volley:1.2.1'
}
2.Give Permission To Access Internet:
<uses-permission android:name="android.permission.INTERNET" />

3.The Main Code And Displaying The Response In The Terminal :

TextView tv=(TextView) findViewById(R.id.textview3);


String url="https://jsonplaceholder.typicode.com/todos/1";

JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(url, null, new


Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {
int userId = response.getInt("userId");
int id = response.getInt("id");
String title = response.getString("title");
boolean completed = response.getBoolean("completed");
tv.setText(userId+"/n"+id+"/n"+title+"/n"+completed);
}
catch (JSONException e) {
throw new RuntimeException(e);
}}},

new ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
tv.setText("error");
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);


requestQueue.add(jsonObjectRequest);
Database (Main Activity)
Add Contact Values in SQLiteDataBase:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDBHelper myDBHelper=new MyDBHelper(MainActivity.this);

Contact user1=new Contact();


user1.setName("Umar");
user1.setPhonenumber("0983094832");
myDBHelper.AddContact(user1);

Contact user2=new Contact();


user2.setName("Ali");
user2.setPhonenumber("0983094833");
myDBHelper.AddContact(user2);

Contact user3=new Contact();


user3.setName("Asad");
user3.setPhonenumber("0983094833");
myDBHelper.AddContact(user3);
Log.d("mydb","the values of id="+user1.getName()+" "+user2.getId()+"
"+user3.getId());
// Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();

List<Contact> allContact=myDBHelper.getAllContact();
for (Contact contact:allContact){

Log.d("my database", contact.getId()+" "+contact.getName()+"


"+contact.getPhonenumber());
}
}
}

You might also like