Minggu, 20 Maret 2022

GPS New

 


import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
String myLati = "-6.18534";
String myLongi = "106.77894";
String myPosisi = "Posisi GPS Anda";
LocationManager locationManager;

private static final int MY_PERMISSION_REQUEST = 1;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_utama);
ip=jsonParser.getIP();

 LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}

public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status, Bundle extras){ }
};

private void updateWithNewLocation(Location location) {
double latitude= Double.parseDouble(myLati);
double longitude= Double.parseDouble(myLongi);
String addressString = "No address found";

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if (((List) addresses).size() > 0) {
Address address = addresses.get(0);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
myLati = "-6.18534";
myLongi = "106.77894";
addressString="Jkt";

}

myPosisi=addressString;
myLati= String.valueOf(latitude);
myLongi= String.valueOf(longitude);

TextView txtMarquee=(TextView)findViewById(R.id.txtMarquee);
txtMarquee.setSelected(true);
String kata="Posisi Anda :"+myLati+"/"+myLongi+" "+myPosisi+"#";
String kalimat= String.format("%1$s", TextUtils.htmlEncode(kata));
txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat));

}
//------------ GPS ---------------------------------------------------------------------------- //

void callMarquee(){
String wkt=jsonParser.getWaktu();
TextView txtMarquee=(TextView)findViewById(R.id.txtMarquee);
txtMarquee.setSelected(true);
String kata="Aplikasi Berbasis Android "+wkt+" #";
String kalimat= String.format("%1$s", TextUtils.htmlEncode(kata));
txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat));
}

private final Runnable m_Runnable = new Runnable() {
public void run() {
new cek().execute();
mHandler.postDelayed(m_Runnable,5000);
}
};

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class cek extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}

protected String doInBackground(String... params) {
try {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
String url=ip+"notifikasi/notifikasi_cek.php";
Log.v("detail",url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params1);
Log.d("detail", json.toString());
sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
PESAN = json.getString("PESAN");
PK= json.getString("PK");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

}

public void keluarYN(){
AlertDialog.Builder ad=new AlertDialog.Builder(MenuUtama.this);
ad.setTitle("Konfirmasi");
ad.setMessage("Apakah benar ingin keluar?");

ad.setPositiveButton("OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}});

ad.setNegativeButton("No",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
}});

ad.show();
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
keluarYN();
return true;
}
return super.onKeyDown(keyCode, event);
}

pada XML marquee:

<TextView
android:id="@+id/txtMarquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000000"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="selamat datang di aplikasi android "
android:textColor="#0000ff"
android:textSize="16sp"
android:typeface="sans" />






Tidak ada komentar:

Posting Komentar