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;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
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(R.id.btnCheckGps);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getLocation();
}
});
}

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) {
return;
}
Location location = locationManager.getLastKnownLocation(mprovider);
locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

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

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

@Override
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 == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

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

}

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

}

@Override
public void onProviderEnabled(String s) {

}

@Override
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 *