· 7 years ago · Dec 05, 2018, 07:26 PM
14 : life
2MainActivity.java
3package com.aman2218.lab3q1;
4
5import android.app.Activity;
6import android.os.Handler;
7import android.os.PersistableBundle;
8import android.support.v7.app.AppCompatActivity;
9import android.os.Bundle;
10import android.view.View;
11import android.widget.TextView;
12
13public class MainActivity extends AppCompatActivity
14{
15 private int seconds = 0;
16 private boolean isRunning = false;
17 private boolean flag = false;
18
19 private void runTimer()
20 {
21 final TextView timeText = findViewById(R.id.time_text);
22
23 final Handler handler = new Handler();
24
25 handler.post(new Runnable()
26 {
27 @Override
28 public void run()
29 {
30 int hours = seconds / 3600;
31 int mins = (seconds % 3600) / 60;
32 int secs = seconds % 60;
33
34 String time = String.format("%d:%02d:%02d", hours, mins, secs);
35 timeText.setText(time);
36
37 if(isRunning)
38 {
39 seconds++;
40 }
41
42 handler.postDelayed(this,1000);
43 }
44 });
45 }
46
47 @Override
48 protected void onStop()
49 {
50 flag = isRunning;
51 isRunning = false;
52 super.onStop();
53 }
54
55 @Override
56 protected void onRestart()
57 {
58 isRunning = flag;
59 super.onRestart();
60 }
61
62 @Override
63 protected void onCreate(Bundle savedInstanceState)
64 {
65 super.onCreate(savedInstanceState);
66 setContentView(R.layout.activity_main);
67
68 if(savedInstanceState != null)
69 {
70 seconds = savedInstanceState.getInt("seconds");
71 isRunning = savedInstanceState.getBoolean("flag");
72 }
73
74 runTimer();
75 }
76
77 @Override
78 protected void onSaveInstanceState(Bundle outState)
79 {
80 outState.putInt("seconds",seconds);
81 outState.putBoolean("flag",flag);
82
83 super.onSaveInstanceState(outState);
84 }
85
86 public void onClickStart(View view)
87 {
88 isRunning = true;
89 }
90
91 public void onClickStop(View view)
92 {
93 isRunning = false;
94 }
95
96 public void onClickReset(View view)
97 {
98 isRunning = false;
99 seconds = 0;
100 }
101}
102
1035 : Fragments
104Activity_main.xml
105<?xml version="1.0" encoding="utf-8"?>
106<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
107 xmlns:tools="http://schemas.android.com/tools"
108 android:layout_width="match_parent"
109 android:layout_height="match_parent"
110 android:orientation="vertical"
111 tools:context=".Main5Activity">
112
113 <Button
114 android:id="@+id/button1"
115 android:layout_width="fill_parent"
116 android:layout_height="wrap_content"
117 android:onClick="selectFrag"
118 android:text="Fragment No.1" />
119
120 <Button
121 android:id="@+id/button2"
122 android:layout_width="fill_parent"
123 android:layout_height="wrap_content"
124 android:onClick="selectFrag"
125 android:text="Fragment No.2" />
126
127 <fragment
128 android:id="@+id/fragment_place"
129 android:name="com.android.hackslash.lab.FragmentOne"
130 android:layout_width="match_parent"
131 android:layout_height="match_parent" />
132
133</LinearLayout>
134
135MainActivity.java
136package com.android.hackslash.lab;
137
138import android.app.Fragment;
139import android.app.FragmentManager;
140import android.app.FragmentTransaction;
141import android.support.v7.app.AppCompatActivity;
142import android.os.Bundle;
143import android.view.View;
144
145public class Main5Activity extends AppCompatActivity {
146
147 @Override
148 protected void onCreate(Bundle savedInstanceState) {
149 super.onCreate(savedInstanceState);
150 setContentView(R.layout.activity_main5);
151 }
152
153 public void selectFrag(View view) {
154 Fragment fr;
155
156 if (view == findViewById(R.id.button2)) {
157 fr = new FragmentTwo();
158 } else {
159 fr = new FragmentOne();
160 }
161
162 FragmentManager fm = getFragmentManager();
163 FragmentTransaction fragmentTransaction = fm.beginTransaction();
164 fragmentTransaction.replace(R.id.fragment_place, fr);
165 fragmentTransaction.commit();
166 }
167}
168
169Fragment1.xml
170<?xml version="1.0" encoding="utf-8"?>
171<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
172 android:layout_width="match_parent"
173 android:layout_height="match_parent"
174 android:background="#00ffff"
175 android:orientation="vertical">
176
177 <TextView
178 android:id="@+id/textView1"
179 android:layout_width="match_parent"
180 android:layout_height="match_parent"
181 android:layout_weight="1"
182 android:text="This is fragment No.1"
183 android:textStyle="bold" />
184
185</LinearLayout>
186
187Fragment1.java
188package com.android.hackslash.lab;
189
190import android.app.Fragment;
191import android.os.Bundle;
192import android.view.LayoutInflater;
193import android.view.View;
194import android.view.ViewGroup;
195
196public class FragmentOne extends Fragment {
197 @Override
198 public View onCreateView(LayoutInflater inflater,
199 ViewGroup container, Bundle savedInstanceState) {
200 return inflater.inflate(
201 R.layout.fragment_one, container, false);
202 }
203}
204
205Fragment2.xml
206<?xml version="1.0" encoding="utf-8"?>
207<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
208 android:layout_width="match_parent"
209 android:layout_height="match_parent"
210 android:background="#ffff00"
211 android:orientation="vertical">
212
213 <TextView
214 android:id="@+id/textView2"
215 android:layout_width="match_parent"
216 android:layout_height="match_parent"
217 android:text="This is fragment No.2"
218 android:textStyle="bold" />
219
220</LinearLayout>
221
222Fragment2.java
223<?xml version="1.0" encoding="utf-8"?>
224<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
225 android:layout_width="match_parent"
226 android:layout_height="match_parent"
227 android:background="#ffff00"
228 android:orientation="vertical">
229
230 <TextView
231 android:id="@+id/textView2"
232 android:layout_width="match_parent"
233 android:layout_height="match_parent"
234 android:text="This is fragment No.2"
235 android:textStyle="bold" />
236
237</LinearLayout>
238 6 : SQL
239Activity_main.xml
240<?xml version="1.0" encoding="utf-8"?>
241<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
242 android:layout_width="fill_parent"
243 android:layout_height="fill_parent">
244
245 <ListView
246 android:id="@+id/list_view"
247 android:layout_width="match_parent"
248 android:layout_height="wrap_content"
249 android:dividerHeight="1dp"
250 android:padding="10dp"></ListView>
251
252 <TextView
253 android:id="@+id/empty"
254 android:layout_width="wrap_content"
255 android:layout_height="wrap_content"
256 android:layout_centerInParent="true"
257 android:text="@string/empty_list_text" />
258
259</RelativeLayout>
260
261MainActivity.java
262package com.android.hackslash.lab;
263
264import android.content.Intent;
265import android.database.Cursor;
266import android.os.Bundle;
267import android.support.v4.widget.SimpleCursorAdapter;
268import android.support.v7.app.ActionBarActivity;
269import android.view.Menu;
270import android.view.MenuItem;
271import android.view.View;
272import android.widget.AdapterView;
273import android.widget.ListView;
274import android.widget.TextView;
275
276public class MainActivity extends ActionBarActivity {
277
278 private DBManager dbManager;
279
280 private ListView listView;
281
282 private SimpleCursorAdapter adapter;
283
284 final String[] from = new String[]{DatabaseHelper._ID,
285 DatabaseHelper.SUBJECT, DatabaseHelper.DESC};
286
287 final int[] to = new int[]{R.id.id, R.id.title, R.id.desc};
288
289 @Override
290 protected void onCreate(Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
292
293 setContentView(R.layout.fragment_emp_list);
294
295 dbManager = new DBManager(this);
296 dbManager.open();
297 Cursor cursor = dbManager.fetch();
298
299 listView = (ListView) findViewById(R.id.list_view);
300 listView.setEmptyView(findViewById(R.id.empty));
301
302 adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
303 adapter.notifyDataSetChanged();
304
305 listView.setAdapter(adapter);
306
307 // OnCLickListiner For List Items
308 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
309 @Override
310 public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
311 TextView idTextView = (TextView) view.findViewById(R.id.id);
312 TextView titleTextView = (TextView) view.findViewById(R.id.title);
313 TextView descTextView = (TextView) view.findViewById(R.id.desc);
314
315 String id = idTextView.getText().toString();
316 String title = titleTextView.getText().toString();
317 String desc = descTextView.getText().toString();
318
319 Intent modify_intent = new Intent(getApplicationContext(), ModifyCountryActivity.class);
320 modify_intent.putExtra("title", title);
321 modify_intent.putExtra("desc", desc);
322 modify_intent.putExtra("id", id);
323
324 startActivity(modify_intent);
325 }
326 });
327 }
328
329 @Override
330 public boolean onCreateOptionsMenu(Menu menu) {
331 getMenuInflater().inflate(R.menu.main, menu);
332 return true;
333 }
334
335 @Override
336 public boolean onOptionsItemSelected(MenuItem item) {
337
338 int id = item.getItemId();
339 if (id == R.id.add_record) {
340
341 Intent add_mem = new Intent(this, AddCountryActivity.class);
342 startActivity(add_mem);
343
344 }
345 return super.onOptionsItemSelected(item);
346 }
347}
348
349Activity_add_country.xml
350<?xml version="1.0" encoding="utf-8"?>
351<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
352 android:layout_width="match_parent"
353 android:layout_height="match_parent"
354 android:orientation="vertical"
355 android:padding="20dp">
356
357 <EditText
358 android:id="@+id/subject_edittext"
359 android:layout_width="match_parent"
360 android:layout_height="wrap_content"
361 android:ems="10"
362 android:hint="@string/enter_title">
363
364 <requestFocus />
365 </EditText>
366
367 <EditText
368 android:id="@+id/description_edittext"
369 android:layout_width="match_parent"
370 android:layout_height="wrap_content"
371 android:ems="10"
372 android:hint="@string/enter_desc"
373 android:inputType="textMultiLine"
374 android:minLines="5"></EditText>
375
376 <Button
377 android:id="@+id/add_record"
378 android:layout_width="wrap_content"
379 android:layout_height="wrap_content"
380 android:layout_gravity="center"
381 android:text="@string/add_record" />
382</LinearLayout>
383
384
385AddCountry.java
386package com.android.hackslash.lab;
387
388import android.app.Activity;
389import android.content.Intent;
390import android.os.Bundle;
391import android.view.View;
392import android.view.View.OnClickListener;
393import android.widget.Button;
394import android.widget.EditText;
395
396public class AddCountryActivity extends Activity implements OnClickListener {
397
398 private Button addTodoBtn;
399 private EditText subjectEditText;
400 private EditText descEditText;
401
402 private DBManager dbManager;
403
404 @Override
405 protected void onCreate(Bundle savedInstanceState) {
406 super.onCreate(savedInstanceState);
407
408 setTitle("Add Record");
409
410 setContentView(R.layout.activity_add_record);
411
412 subjectEditText = (EditText) findViewById(R.id.subject_edittext);
413 descEditText = (EditText) findViewById(R.id.description_edittext);
414
415 addTodoBtn = (Button) findViewById(R.id.add_record);
416
417 dbManager = new DBManager(this);
418 dbManager.open();
419 addTodoBtn.setOnClickListener(this);
420 }
421
422 @Override
423 public void onClick(View v) {
424 switch (v.getId()) {
425 case R.id.add_record:
426
427 final String name = subjectEditText.getText().toString();
428 final String desc = descEditText.getText().toString();
429
430 dbManager.insert(name, desc);
431
432 Intent main = new Intent(AddCountryActivity.this, CountryListActivity.class)
433 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
434
435 startActivity(main);
436 break;
437 }
438 }
439}
440
441Activity_modify.xml
442<?xml version="1.0" encoding="utf-8"?>
443<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
444 android:layout_width="match_parent"
445 android:layout_height="match_parent"
446 android:orientation="vertical"
447 android:padding="10dp">
448
449 <EditText
450 android:id="@+id/subject_edittext"
451 android:layout_width="match_parent"
452 android:layout_height="wrap_content"
453 android:layout_marginBottom="10dp"
454 android:ems="10"
455 android:hint="@string/enter_title" />
456
457 <EditText
458 android:id="@+id/description_edittext"
459 android:layout_width="match_parent"
460 android:layout_height="wrap_content"
461 android:ems="10"
462 android:hint="@string/enter_desc"
463 android:inputType="textMultiLine"
464 android:minLines="5"></EditText>
465
466 <LinearLayout
467 android:layout_width="fill_parent"
468 android:layout_height="wrap_content"
469 android:gravity="center_horizontal"
470 android:orientation="horizontal"
471 android:weightSum="2">
472
473 <Button
474 android:id="@+id/btn_update"
475 android:layout_width="wrap_content"
476 android:layout_height="wrap_content"
477 android:layout_weight="1"
478 android:text="@string/btn_update" />
479
480 <Button
481 android:id="@+id/btn_delete"
482 android:layout_width="wrap_content"
483 android:layout_height="wrap_content"
484 android:layout_weight="1"
485 android:text="@string/btn_delete" />
486 </LinearLayout>
487</LinearLayout>
488
489ModifyActivity.java
490package com.android.hackslash.lab;
491
492import android.app.Activity;
493import android.content.Intent;
494import android.os.Bundle;
495import android.view.View;
496import android.view.View.OnClickListener;
497import android.widget.Button;
498import android.widget.EditText;
499
500public class ModifyActivity extends Activity implements OnClickListener {
501
502 private EditText titleText;
503 private Button updateBtn, deleteBtn;
504 private EditText descText;
505
506 private long _id;
507
508 private DBManager dbManager;
509
510 @Override
511 protected void onCreate(Bundle savedInstanceState) {
512 super.onCreate(savedInstanceState);
513
514 setTitle("Modify Record");
515
516 setContentView(R.layout.activity_modify_record);
517
518 dbManager = new DBManager(this);
519 dbManager.open();
520
521 titleText = (EditText) findViewById(R.id.subject_edittext);
522 descText = (EditText) findViewById(R.id.description_edittext);
523
524 updateBtn = (Button) findViewById(R.id.btn_update);
525 deleteBtn = (Button) findViewById(R.id.btn_delete);
526
527 Intent intent = getIntent();
528 String id = intent.getStringExtra("id");
529 String name = intent.getStringExtra("title");
530 String desc = intent.getStringExtra("desc");
531
532 _id = Long.parseLong(id);
533
534 titleText.setText(name);
535 descText.setText(desc);
536
537 updateBtn.setOnClickListener(this);
538 deleteBtn.setOnClickListener(this);
539 }
540
541 @Override
542 public void onClick(View v) {
543 switch (v.getId()) {
544 case R.id.btn_update:
545 String title = titleText.getText().toString();
546 String desc = descText.getText().toString();
547
548 dbManager.update(_id, title, desc);
549 this.returnHome();
550 break;
551
552 case R.id.btn_delete:
553 dbManager.delete(_id);
554 this.returnHome();
555 break;
556 }
557 }
558
559 public void returnHome() {
560 Intent home_intent = new Intent(getApplicationContext(), CountryListActivity.class)
561 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
562 startActivity(home_intent);
563 }
564}
565
566DBHelper.java
567package com.android.hackslash.lab;
568
569public class DatabaseHelper extends SQLiteOpenHelper {
570
571 // Table Name
572 public static final String TABLE_NAME = "COUNTRIES";
573
574 // Table columns
575 public static final String _ID = "_id";
576 public static final String SUBJECT = "subject";
577 public static final String DESC = "description";
578
579 // Database Information
580 static final String DB_NAME = "JOURNALDEV_COUNTRIES.DB";
581
582 // database version
583 static final int DB_VERSION = 1;
584
585 // Creating table query
586 private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
587 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUBJECT + " TEXT NOT NULL, " + DESC + " TEXT);";
588
589 public DatabaseHelper(Context context) {
590 super(context, DB_NAME, null, DB_VERSION);
591 }
592
593 @Override
594 public void onCreate(SQLiteDatabase db) {
595 db.execSQL(CREATE_TABLE);
596 }
597
598 @Override
599 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
600 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
601 onCreate(db);
602 }
603}
604
605DBManager.java
606package com.android.hackslash.lab;
607
608import android.content.ContentValues;
609import android.content.Context;
610import android.database.Cursor;
611import android.database.SQLException;
612import android.database.sqlite.SQLiteDatabase;
613
614public class DBManager {
615
616 private DatabaseHelper dbHelper;
617
618 private Context context;
619
620 private SQLiteDatabase database;
621
622 public DBManager(Context c) {
623 context = c;
624 }
625
626 public DBManager open() throws SQLException {
627 dbHelper = new DatabaseHelper(context);
628 database = dbHelper.getWritableDatabase();
629 return this;
630 }
631
632 public void close() {
633 dbHelper.close();
634 }
635
636 public void insert(String name, String desc) {
637 ContentValues contentValue = new ContentValues();
638 contentValue.put(DatabaseHelper.SUBJECT, name);
639 contentValue.put(DatabaseHelper.DESC, desc);
640 database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
641 }
642
643 public Cursor fetch() {
644 String[] columns = new String[]{DatabaseHelper._ID, DatabaseHelper.SUBJECT, DatabaseHelper.DESC};
645 Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
646 if (cursor != null) {
647 cursor.moveToFirst();
648 }
649 return cursor;
650 }
651
652 public int update(long _id, String name, String desc) {
653 ContentValues contentValues = new ContentValues();
654 contentValues.put(DatabaseHelper.SUBJECT, name);
655 contentValues.put(DatabaseHelper.DESC, desc);
656 int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
657 return i;
658 }
659
660 public void delete(long _id) {
661 database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
662 }
663
664}
6657 : Bluetooth
666Activity_main.xml
667<?xml version="1.0" encoding="utf-8"?>
668<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
669 xmlns:tools="http://schemas.android.com/tools"
670 android:layout_width="match_parent"
671 android:layout_height="match_parent"
672 android:transitionGroup="true"
673 tools:context=".Main6Activity">
674
675 <TextView
676 android:id="@+id/textview"
677 android:layout_width="wrap_content"
678 android:layout_height="wrap_content"
679 android:layout_alignParentTop="true"
680 android:layout_centerHorizontal="true"
681 android:text="Bluetooth"
682 android:textSize="35dp" />
683
684 <Button
685 android:id="@+id/button"
686 android:layout_width="wrap_content"
687 android:layout_height="wrap_content"
688 android:layout_below="@+id/textview"
689 android:layout_toStartOf="@+id/textview"
690 android:layout_toLeftOf="@+id/textview"
691 android:clickable="true"
692 android:onClick="on"
693 android:text="Turn On" />
694
695 <Button
696 android:id="@+id/button2"
697 android:layout_width="wrap_content"
698 android:layout_height="wrap_content"
699 android:layout_alignBottom="@+id/button"
700 android:layout_centerHorizontal="true"
701 android:onClick="visible"
702 android:text="Get visible" />
703
704 <Button
705 android:id="@+id/button3"
706 android:layout_width="wrap_content"
707 android:layout_height="wrap_content"
708 android:layout_below="@+id/button2"
709 android:onClick="list"
710 android:text="List devices" />
711
712 <Button
713 android:id="@+id/button4"
714 android:layout_width="wrap_content"
715 android:layout_height="wrap_content"
716 android:layout_below="@id/button2"
717 android:layout_toRightOf="@id/button3"
718 android:onClick="off"
719 android:text="turn off" />
720
721 <ListView
722 android:id="@+id/listView"
723 android:layout_width="wrap_content"
724 android:layout_height="wrap_content"
725 android:layout_below="@+id/textView2"
726 android:layout_alignStart="@+id/button"
727 android:layout_alignLeft="@+id/button"
728 android:layout_alignParentBottom="true" />
729
730 <TextView
731 android:id="@+id/textView2"
732 android:layout_width="wrap_content"
733 android:layout_height="wrap_content"
734 android:layout_below="@+id/button4"
735 android:layout_alignStart="@+id/listView"
736 android:layout_alignLeft="@+id/listView"
737 android:text="Paired devices:"
738 android:textColor="#ff34ff06"
739 android:textSize="25dp" />
740
741</RelativeLayout>
742MainActivity.java
743package com.android.hackslash.lab;
744
745import android.bluetooth.BluetoothAdapter;
746import android.bluetooth.BluetoothDevice;
747import android.content.Intent;
748import android.os.Bundle;
749import android.support.v7.app.AppCompatActivity;
750import android.view.View;
751import android.widget.ArrayAdapter;
752import android.widget.Button;
753import android.widget.ListView;
754import android.widget.Toast;
755
756import java.util.ArrayList;
757import java.util.Set;
758
759public class Main6Activity extends AppCompatActivity {
760 Button b1, b2, b3, b4;
761 private BluetoothAdapter BA;
762 private Set<BluetoothDevice> pairedDevices;
763 ListView lv;
764
765 @Override
766 protected void onCreate(Bundle savedInstanceState) {
767 super.onCreate(savedInstanceState);
768 setContentView(R.layout.activity_main6);
769
770 b1 = (Button) findViewById(R.id.button);
771 b2 = (Button) findViewById(R.id.button2);
772 b3 = (Button) findViewById(R.id.button3);
773 b4 = (Button) findViewById(R.id.button4);
774
775 BA = BluetoothAdapter.getDefaultAdapter();
776 lv = (ListView) findViewById(R.id.listView);
777 }
778
779 public void on(View v) {
780 if (!BA.isEnabled()) {
781 Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
782 startActivityForResult(turnOn, 0);
783 Toast.makeText(getApplicationContext(), "Turned on", Toast.LENGTH_LONG).show();
784 } else {
785 Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
786 }
787 }
788
789 public void off(View v) {
790 BA.disable();
791 Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_LONG).show();
792 }
793
794
795 public void visible(View v) {
796 Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
797 startActivityForResult(getVisible, 0);
798 }
799
800
801 public void list(View v) {
802 pairedDevices = BA.getBondedDevices();
803
804 ArrayList list = new ArrayList();
805
806 for (BluetoothDevice bt : pairedDevices) list.add(bt.getName());
807 Toast.makeText(getApplicationContext(), "Showing Paired Devices", Toast.LENGTH_SHORT).show();
808
809 final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
810
811 lv.setAdapter(adapter);
812 }
813}
8148 : Using Camera
815Activity_main.xml
816<?xml version="1.0" encoding="utf-8"?>
817<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
818 xmlns:tools="http://schemas.android.com/tools"
819 android:layout_width="match_parent"
820 android:layout_height="match_parent"
821 tools:context=".Main7Activity">
822
823 <Button
824 android:id="@+id/button1"
825 android:layout_width="wrap_content"
826 android:layout_height="wrap_content"
827 android:layout_alignParentBottom="true"
828 android:layout_centerHorizontal="true"
829 android:text="Take a Photo"></Button>
830
831 <ImageView
832 android:id="@+id/imageView1"
833 android:layout_width="fill_parent"
834 android:layout_height="fill_parent"
835 android:layout_above="@+id/button1"
836 android:layout_alignParentTop="true"/>
837</RelativeLayout>
838
839MainActivity.java
840package com.android.hackslash.lab;
841
842import android.support.v7.app.AppCompatActivity;
843import android.os.Bundle;
844import android.content.Intent;
845import android.graphics.Bitmap;
846import android.view.View;
847import android.widget.Button;
848import android.widget.ImageView;
849
850public class Main7Activity extends AppCompatActivity {
851 private static final int CAMERA_REQUEST = 1888;
852 ImageView imageView;
853
854 public void onCreate(Bundle savedInstanceState) {
855
856 super.onCreate(savedInstanceState);
857 setContentView(R.layout.activity_main7);
858
859 imageView = (ImageView) this.findViewById(R.id.imageView1);
860 Button photoButton = (Button) this.findViewById(R.id.button1);
861
862 photoButton.setOnClickListener(new View.OnClickListener() {
863
864 @Override
865 public void onClick(View v) {
866 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
867 startActivityForResult(cameraIntent, CAMERA_REQUEST);
868 }
869 });
870 }
871
872 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
873 if (requestCode == CAMERA_REQUEST) {
874 Bitmap photo = (Bitmap) data.getExtras().get("data");
875 imageView.setImageBitmap(photo);
876 }
877 }
878}
8799 : Location Services
880
881Activity_main.xml
882<?xml version = "1.0" encoding = "utf-8"?>
883<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
884 android:layout_width="fill_parent"
885 android:layout_height="fill_parent"
886 android:orientation="vertical">
887
888 <Button
889 android:id="@+id/button"
890 android:layout_width="fill_parent"
891 android:layout_height="wrap_content"
892 android:text="getlocation" />
893
894</LinearLayout>
895
896MainActivity.java
897package com.android.hackslash.lab;
898
899import android.os.Bundle;
900import android.Manifest;
901import android.app.Activity;
902import android.support.v4.app.ActivityCompat;
903import android.test.mock.MockPackageManager;
904import android.view.View;
905import android.widget.Button;
906import android.widget.Toast;
907
908public class Main8Activity extends Activity {
909
910 Button btnShowLocation;
911 private static final int REQUEST_CODE_PERMISSION = 2;
912 String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
913
914 GPSTracker gps;
915
916 @Override
917 public void onCreate(Bundle savedInstanceState) {
918 super.onCreate(savedInstanceState);
919 setContentView(R.layout.activity_main8);
920
921 try {
922 if (ActivityCompat.checkSelfPermission(this, mPermission)
923 != MockPackageManager.PERMISSION_GRANTED) {
924
925 ActivityCompat.requestPermissions(this, new String[]{mPermission},
926 REQUEST_CODE_PERMISSION);
927 }
928 } catch (Exception e) {
929 e.printStackTrace();
930 }
931
932 btnShowLocation = (Button) findViewById(R.id.button);
933 btnShowLocation.setOnClickListener(new View.OnClickListener() {
934
935 @Override
936 public void onClick(View arg0) {
937 gps = new GPSTracker(Main8Activity.this);
938
939 if (gps.canGetLocation()) {
940
941 double latitude = gps.getLatitude();
942 double longitude = gps.getLongitude();
943 Toast.makeText(getApplicationContext(), "Your Location is - \nLat: "
944 + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
945 } else {
946 }
947 }
948 });
949 }
950}
951
952GPSTracker.java
953package com.android.hackslash.lab;
954
955import android.annotation.SuppressLint;
956import android.app.Service;
957import android.content.Context;
958import android.content.Intent;
959import android.location.Location;
960import android.location.LocationListener;
961import android.location.LocationManager;
962import android.os.Bundle;
963import android.os.IBinder;
964import android.util.Log;
965
966public class GPSTracker extends Service implements LocationListener {
967
968 private final Context mContext;
969 boolean isGPSEnabled = false;
970 boolean isNetworkEnabled = false;
971 boolean canGetLocation = false;
972
973 Location location;
974 double latitude;
975 double longitude;
976
977 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
978 private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
979 protected LocationManager locationManager;
980
981 public GPSTracker(Context context) {
982 this.mContext = context;
983 getLocation();
984 }
985
986 @SuppressLint("MissingPermission")
987 public Location getLocation() {
988 try {
989 locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
990
991 isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
992 isNetworkEnabled = locationManager
993 .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
994
995 if (!isGPSEnabled && !isNetworkEnabled) {
996
997 } else {
998 this.canGetLocation = true;
999 if (isNetworkEnabled) {
1000 locationManager.requestLocationUpdates(
1001 LocationManager.NETWORK_PROVIDER,
1002 MIN_TIME_BW_UPDATES,
1003 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
1004
1005 Log.d("Network", "Network");
1006 if (locationManager != null) {
1007 location = locationManager
1008 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
1009
1010 if (location != null) {
1011 latitude = location.getLatitude();
1012 longitude = location.getLongitude();
1013 }
1014 }
1015 }
1016
1017 if (isGPSEnabled) {
1018 if (location == null) {
1019 locationManager.requestLocationUpdates(
1020 LocationManager.GPS_PROVIDER,
1021 MIN_TIME_BW_UPDATES,
1022 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
1023
1024 Log.d("GPS Enabled", "GPS Enabled");
1025 if (locationManager != null) {
1026 location = locationManager
1027 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
1028
1029 if (location != null) {
1030 latitude = location.getLatitude();
1031 longitude = location.getLongitude();
1032 }
1033 }
1034 }
1035 }
1036 }
1037
1038 } catch (Exception e) {
1039 e.printStackTrace();
1040 }
1041
1042 return location;
1043 }
1044
1045 public double getLatitude() {
1046 if (location != null) {
1047 latitude = location.getLatitude();
1048 }
1049 return latitude;
1050 }
1051
1052 public double getLongitude() {
1053 if (location != null) {
1054 longitude = location.getLongitude();
1055 }
1056
1057 return longitude;
1058 }
1059
1060 public boolean canGetLocation() {
1061 return this.canGetLocation;
1062 }
1063
1064 @Override
1065 public void onLocationChanged(Location location) {
1066 }
1067
1068 @Override
1069 public void onProviderDisabled(String provider) {
1070 }
1071
1072 @Override
1073 public void onProviderEnabled(String provider) {
1074 }
1075
1076 @Override
1077 public void onStatusChanged(String provider, int status, Bundle extras) {
1078 }
1079
1080 @Override
1081 public IBinder onBind(Intent arg0) {
1082 return null;
1083 }
1084}