Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- package pl.kalisz.pwsz.pup.endogps;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- GPScore gps;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void sendIntent(View view)
- {
- gps = new GPScore(MainActivity.this);
- Intent intent = new Intent (this, MapsActivity.class);
- intent.putExtra("dlugosc", gps.getDlugosc());
- intent.putExtra("szerokosc",gps.getSzerokosc());
- startActivity(intent);
- }
- }
- activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Pokaz pozycje"
- android:id="@+id/pokaz"
- android:layout_gravity="center_horizontal"
- android:onClick="sendIntent" />
- </LinearLayout>
- MapsActivity.java
- package pl.kalisz.pwsz.pup.endogps;
- import android.content.Intent;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
- private GoogleMap mMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- Intent intent = getIntent();
- double dlugosc = intent.getExtras().getDouble("dlugosc");
- double szerokosc = intent.getExtras().getDouble("szerokosc");
- // Add a marker in Sydney and move the camera
- LatLng sydney = new LatLng(dlugosc,szerokosc);
- mMap.addMarker(new MarkerOptions().position(sydney).title("My Marker"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
- }
- }
- activity_maps.xml
- <code><pre>`<fragment xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/map"
- tools:context=".MapsActivity"
- android:name="com.google.android.gms.maps.SupportMapFragment" />`
- GPScore.java
- package pl.kalisz.pwsz.pup.endogps;
- import android.app.AlertDialog;
- import android.app.Service;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.provider.Settings;
- import android.support.annotation.Nullable;
- import android.widget.Toast;
- import java.security.Provider;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by noVak on 2015-11-27.
- */
- public class GPScore extends Service implements LocationListener {
- private final Context context;
- Location location=null;
- boolean isGPSEnabled = false;
- boolean isNetworkEnabled = false;
- private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=5;
- private static final long MIN_TIME_BW_UPDATES=1000*60*1;
- protected LocationManager locationManager;
- double szerokosc;
- double dlugosc;
- public GPScore(Context context)
- {
- this.context=context;
- this.PobierzLokacje();
- }
- public void PobierzLokacje()
- {
- try {
- locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
- isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- this.isNEnable(this.context);
- if(isGPSEnabled==false || isNetworkEnabled==false)
- {
- this.showSettinsAlert();
- }
- else
- {
- if(location==null)
- {
- location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(location==null)
- {
- location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- szerokosc= location.getLatitude();
- dlugosc = location.getLongitude();
- Toast.makeText(this.context, "Szerokosc: "+Double.toString(szerokosc)+" Długość: "+Double.toString(dlugosc), Toast.LENGTH_SHORT).show();
- }
- }
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public double getDlugosc()
- {
- return dlugosc;
- }
- public double getSzerokosc()
- {
- return szerokosc;
- }
- public void isNEnable(Context ctx) {
- ConnectivityManager connecManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo myNetworkInfo = connecManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- if (myNetworkInfo.isConnected())
- {
- isNetworkEnabled=true;
- }
- else
- {
- isNetworkEnabled=false;
- }
- }
- public void showSettinsAlert() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
- alertDialog.setTitle("Internet lub GPS");
- alertDialog.setMessage("GPS lub Internet nie wlaczony. Chcesz przejsc do ustawien?");
- alertDialog.setPositiveButton("Ustawienia", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- context.startActivity(intent);
- }
- });
- alertDialog.setNegativeButton("Anuluj", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- alertDialog.show();
- }
- @Override
- public void onLocationChanged(Location location) {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- Toast.makeText(this.context, "GPS Włączony", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onProviderDisabled(String provider) {
- Toast.makeText(this.context, "GPS Wyłączony", Toast.LENGTH_SHORT).show();
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement