Techrens Alert – part 2

Techrens Alert – part 2

So for the next step I started a new project in Android Studio. For part 2 I create the foundation for the app.

Part 2

First thing I did was selected API 15 which will run on 97% of android devices. If you care about older devices/os using your app you want to pay attention to the minimum SDK.

The next thing I did was selected what type of activity I wanted, in this case I selected Basic Activity.

I then went ahead and changed the defaults from MainActivity to TestActivity (not needed just something I do).

So after the project is built  the first thing I did was open the manifest file which is called AndroidManifest.xml.

I then added the following permissions:

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.INTERNET” />


Next I went to the content_test1.xml and added a button to the screen. For an ID I called the button btnCheckGPS. I also gave the Hello World text an ID name of txtMessage.

So now the magic begins I go to the MainActivity which is TestActivity1 for me and add the following code:

public class TestActivity1 extends AppCompatActivity implements LocationListener {

LocationManager locationManager;
String mprovider;

protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(;

FloatingActionButton fab = (FloatingActionButton) findViewById(;
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Snackbar.make(view, “Replace with your own action”, Snackbar.LENGTH_LONG)
.setAction(“Action”, null).show();
final Button button = (Button) findViewById(;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

private void getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

mprovider = locationManager.getBestProvider(criteria, false);

if (mprovider != null && !mprovider.equals(“”)) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Location location = locationManager.getLastKnownLocation(mprovider);
locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

if (location != null)
Toast.makeText(getBaseContext(), “No Location Provider Found Check Your Code”, Toast.LENGTH_SHORT).show();

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == {
return true;

return super.onOptionsItemSelected(item);

public void onLocationChanged(Location location) {
TextView message = (TextView) findViewById(;
message.setText(“Current Longitude:” + location.getLongitude() + “\n\nCurrent Latitude:” + location.getLatitude());


public void onStatusChanged(String s, int i, Bundle bundle) {


public void onProviderEnabled(String s) {


public void onProviderDisabled(String s) {


In essence what I did with the above code is when the button on the page is clicked it will get the GPS coordinates and post it to the page replacing the Hello World.

So you should have enough information to duplicate what I have done. But if you are lazy and don’t want to try it you can download the apk.

If you install the apk above you may get a security warning about trusted sources. Now you can trust me 😉 but you should always be mindful of apps you install.

So now that we have that out of the way here are the next steps 🙂

Try to break what I have created 🙂

Leave comments below and follow me on twitter @techrens

Leave a Reply

Your email address will not be published. Required fields are marked *