Ok straight to the point I'm a beginner in this android programming, I have question about How to pass context from my activity due to "cant resolve method getapplicationcontext"
this is MyLocationListener.java :
public class MyLocationListener implements LocationListener {
// Dipanggil saat ada perubahan lokasi geografis pengguna
private Context mContext;
public MyLocationListener(Context context) {
mContext = context;
}
@Override
public void onLocationChanged(Location location) {
// Mendapatkan nilai latitude dari lokasi terbaru
double latitude = location.getLatitude();
// Mendapatkan nilai longitude dari lokasi terbaru
double longitude = location.getLongitude();
// Menampilkan lokasi terbaru menggunakan Toast
String message = "Lokasi saat ini :\n" +
"Latitude = " + latitude + "\n" +
"Longitude = " + longitude;
Toast.makeText(getApplicationContext(),
message, Toast.LENGTH_LONG).show();
}
// Dipanggil saat provider dinon-aktifkan oleh pengguna
@Override
public void onProviderDisabled(String provider) {
String message = "GPS disabled";
Toast.makeText(getApplicationContext(),
message, Toast.LENGTH_LONG).show();
}
// dipanggil saat provider diaktifkan oleh pengguna
@Override
public void onProviderEnabled(String provider) {
String message = "GPS enabled";
Toast.makeText(getApplicationContext(),
message, Toast.LENGTH_LONG).show();
}
// dipanggil saat ada perubahan status pada provider
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
This is my activity (GPSSample.java) :
public class GPSSample extends Activity {
/** Called when the activity is first created. */
@Override
LocationListener myLocationListener = new MyLocationListener(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inisiasi LocationManager dan LocationListener
LocationManager myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener myLocationListener = new MyLocationListener();
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
}
It said that I should use constructor to pass it, but how? I have Some constructor sample but I have no idea where to put it, and rename it based on my script
MyClass myClass = new MyClass(this);
Then create a constructor in that class that accepts Context as a param and use that
public class MyClass
{
Context c;
public MyClass(Context context)
{
c= context;
}
}}
Thanks for the help...