Advertisement
Guest User

noVak

a guest
Nov 27th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.31 KB | None | 0 0
  1. MainActivity.java
  2.  package pl.kalisz.pwsz.pup.endogps;
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.  
  12.     GPScore gps;
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.  
  17.  
  18.  
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.     }
  23.     public void sendIntent(View view)
  24.     {
  25.         gps = new GPScore(MainActivity.this);
  26.         Intent intent = new Intent (this, MapsActivity.class);
  27.         intent.putExtra("dlugosc", gps.getDlugosc());
  28.         intent.putExtra("szerokosc",gps.getSzerokosc());
  29.         startActivity(intent);
  30.  
  31.     }
  32. }
  33.  
  34. activity_main.xml
  35.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  37.     android:layout_height="match_parent"
  38.     tools:context=".MainActivity"
  39.     android:orientation="vertical">
  40.     <Button
  41.         android:layout_width="wrap_content"
  42.         android:layout_height="wrap_content"
  43.         android:text="Pokaz pozycje"
  44.         android:id="@+id/pokaz"
  45.         android:layout_gravity="center_horizontal"
  46.         android:onClick="sendIntent" />
  47. </LinearLayout>
  48.  
  49. MapsActivity.java
  50.  package pl.kalisz.pwsz.pup.endogps;
  51.  
  52. import android.content.Intent;
  53. import android.support.v4.app.FragmentActivity;
  54. import android.os.Bundle;
  55.  
  56. import com.google.android.gms.maps.CameraUpdateFactory;
  57. import com.google.android.gms.maps.GoogleMap;
  58. import com.google.android.gms.maps.OnMapReadyCallback;
  59. import com.google.android.gms.maps.SupportMapFragment;
  60. import com.google.android.gms.maps.model.LatLng;
  61. import com.google.android.gms.maps.model.MarkerOptions;
  62.  
  63. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  64.  
  65.     private GoogleMap mMap;
  66.  
  67.     @Override
  68.     protected void onCreate(Bundle savedInstanceState) {
  69.         super.onCreate(savedInstanceState);
  70.         setContentView(R.layout.activity_maps);
  71.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  72.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  73.                 .findFragmentById(R.id.map);
  74.         mapFragment.getMapAsync(this);
  75.     }
  76.  
  77.  
  78.     /**
  79.      * Manipulates the map once available.
  80.      * This callback is triggered when the map is ready to be used.
  81.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  82.      * we just add a marker near Sydney, Australia.
  83.      * If Google Play services is not installed on the device, the user will be prompted to install
  84.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  85.      * installed Google Play services and returned to the app.
  86.      */
  87.     @Override
  88.     public void onMapReady(GoogleMap googleMap) {
  89.         mMap = googleMap;
  90.         Intent intent = getIntent();
  91.         double dlugosc = intent.getExtras().getDouble("dlugosc");
  92.         double szerokosc = intent.getExtras().getDouble("szerokosc");
  93.         // Add a marker in Sydney and move the camera
  94.         LatLng sydney = new LatLng(dlugosc,szerokosc);
  95.         mMap.addMarker(new MarkerOptions().position(sydney).title("My Marker"));
  96.         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  97.     }
  98. }
  99.  
  100.  
  101. activity_maps.xml
  102. <code><pre>`<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  103. xmlns:tools="http://schemas.android.com/tools"
  104. android:layout_width="match_parent"
  105. android:layout_height="match_parent"
  106. android:id="@+id/map"
  107. tools:context=".MapsActivity"
  108. android:name="com.google.android.gms.maps.SupportMapFragment" />`
  109.  
  110. GPScore.java
  111.  package pl.kalisz.pwsz.pup.endogps;
  112.  
  113. import android.app.AlertDialog;
  114. import android.app.Service;
  115. import android.content.Context;
  116. import android.content.DialogInterface;
  117. import android.content.Intent;
  118. import android.location.Location;
  119. import android.location.LocationListener;
  120. import android.location.LocationManager;
  121. import android.net.ConnectivityManager;
  122. import android.net.NetworkInfo;
  123. import android.os.Bundle;
  124. import android.os.IBinder;
  125. import android.provider.Settings;
  126. import android.support.annotation.Nullable;
  127. import android.widget.Toast;
  128.  
  129. import java.security.Provider;
  130. import java.util.List;
  131. import java.util.Map;
  132.  
  133. /**
  134.  * Created by noVak on 2015-11-27.
  135.  */
  136. public class GPScore extends Service implements LocationListener {
  137.  
  138.     private final Context context;
  139.  
  140.     Location location=null;
  141.     boolean isGPSEnabled = false;
  142.     boolean isNetworkEnabled = false;
  143.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=5;
  144.     private static final long MIN_TIME_BW_UPDATES=1000*60*1;
  145.     protected LocationManager locationManager;
  146.     double szerokosc;
  147.     double dlugosc;
  148.  
  149.     public GPScore(Context context)
  150.     {
  151.         this.context=context;
  152.         this.PobierzLokacje();
  153.     }
  154.  
  155.     public void PobierzLokacje()
  156.     {
  157.         try {
  158.             locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
  159.             isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  160.             this.isNEnable(this.context);
  161.             if(isGPSEnabled==false || isNetworkEnabled==false)
  162.             {
  163.                 this.showSettinsAlert();
  164.             }
  165.             else
  166.             {
  167.                 if(location==null)
  168.                 {
  169.                     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  170.                     if(location==null)
  171.                     {
  172.                         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  173.                     }
  174.                     szerokosc= location.getLatitude();
  175.                     dlugosc = location.getLongitude();
  176.                     Toast.makeText(this.context, "Szerokosc: "+Double.toString(szerokosc)+" Długość: "+Double.toString(dlugosc), Toast.LENGTH_SHORT).show();
  177.                 }
  178.             }
  179.  
  180.  
  181.  
  182.         }catch(Exception e)
  183.         {
  184.             e.printStackTrace();
  185.         }
  186.     }
  187.  
  188.     public double getDlugosc()
  189.     {
  190.         return dlugosc;
  191.     }
  192.     public double getSzerokosc()
  193.     {
  194.         return szerokosc;
  195.     }
  196.  
  197.     public void isNEnable(Context ctx) {
  198.  
  199.         ConnectivityManager connecManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
  200.  
  201.         NetworkInfo myNetworkInfo = connecManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  202.  
  203.         if (myNetworkInfo.isConnected())
  204.         {
  205.             isNetworkEnabled=true;
  206.         }
  207.         else
  208.         {
  209.             isNetworkEnabled=false;
  210.         }
  211.  
  212.     }
  213.  
  214.     public void showSettinsAlert() {
  215.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
  216.  
  217.         alertDialog.setTitle("Internet lub GPS");
  218.         alertDialog.setMessage("GPS lub Internet nie wlaczony. Chcesz przejsc  do ustawien?");
  219.         alertDialog.setPositiveButton("Ustawienia", new DialogInterface.OnClickListener() {
  220.             @Override
  221.             public void onClick(DialogInterface dialog, int which) {
  222.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  223.                 context.startActivity(intent);
  224.             }
  225.         });
  226.  
  227.         alertDialog.setNegativeButton("Anuluj", new DialogInterface.OnClickListener() {
  228.                 @Override
  229.                 public void onClick(DialogInterface dialog, int which) {
  230.                     dialog.cancel();
  231.                 }
  232.             });
  233.  
  234.         alertDialog.show();
  235.     }
  236.  
  237.     @Override
  238.     public void onLocationChanged(Location location) {
  239.  
  240.     }
  241.  
  242.     @Override
  243.     public void onStatusChanged(String provider, int status, Bundle extras) {
  244.  
  245.     }
  246.  
  247.     @Override
  248.     public void onProviderEnabled(String provider) {
  249.         Toast.makeText(this.context, "GPS Włączony", Toast.LENGTH_SHORT).show();
  250.  
  251.     }
  252.  
  253.     @Override
  254.     public void onProviderDisabled(String provider) {
  255.         Toast.makeText(this.context, "GPS Wyłączony", Toast.LENGTH_SHORT).show();
  256.     }
  257.  
  258.     @Nullable
  259.     @Override
  260.     public IBinder onBind(Intent intent) {
  261.         return null;
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement