I want to access custom view from public void onLocationChanged in public class MyCurrentLocationListener implements LocationListener.
MyActivity:
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "net.motameni.alisapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyCurrentLocationListener locationListener = new MyCurrentLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
and MyCurrentLocationListener is this:
public class MyCurrentLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
TextView textView = (TextView) findViewById(R.id.location_message);
textView.setTextSize(40);
textView.setText("hello");
setContentView(textView);
}
What is wrong???
You can not do UI changes from non UI function.
for doing this either you have to pass your view object to listener method, Or you have to create a method in your activity class that receive value from listener and updated value of your view.
update code in your oncreate:
TextView tv = (TextView)findViewById(R.id.tv1);
MyCurrentLocationListener locationListener = new MyCurrentLocationListener(tv);
and in your listener class create a constructor -
TextView textView;
public MyCurrentLocationListener (TextView tv){
textView = tv;
}
and form location change -
public void onLocationChanged(Location location) {
textView.setTextSize(40);
textView.setText("hello");
}
this is the best way.
Related
please help me with this, I´m working with android studio – java. I have an Activity(MainActivity) where I want to show the information about location; but I want this information come from other class(Location) toward this MainActivity, how can I make this location class send the location data automatically.
Well, I had try to call the method in MainActivity where I get the location data from class-location, but this methods has parameters “Location location”, so I can’t call it directly.
And I had try put a method inside the “location method” to send the information, but It doesn’t work
I declare the location variables global and put It in other method to send them but doesn’t’ work
I´m trying using the location information (latitude and longitude) from a class using extra, but how can get the data.
Anyway… please help me, the Idea is pass the location information: “getLatitude()”and “getLongitude()” for a class to my MainActivity automatically and to a DDBB later.
So much thank for any help
this is "the MainActivity" class
public class MainActivity extends AppCompatActivity {
TextView tvlatitud, tvlongitud;
Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvlongitud = findViewById(R.id.tv_longitud);
tvlatitud = findViewById(R.id.tv_latitud);
location = new Location(this);
askLocation();
}
public void askLocation(){
location.updateGPS();
}
public void showData(String lat, String lon ){
tvlatitud.setText(lat);
tvlongitud.setText(lon);
}
and this is the Location class
public class Location extends AppCompatActivity {
private static final int PERMISSIONS_FINE_LOCATION = 99;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
String latitude1, longitude1;
private MainActivity mainActivity;
public Location(MainActivity mainActivity){
this.mainActivity = mainActivity;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationRequest = new LocationRequest();
locationRequest.setInterval(1000 * 30);
locationRequest.setFastestInterval(1000 * 50);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
updateGPS();
}
public void updateGPS() {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(Location.this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
updateUIValues(location);
mainActivity.showData(latitude1, longitude1);
}
});
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, PERMISSIONS_FINE_LOCATION);
}
}
}
private void updateUIValues(android.location.Location location) {
latitude1 = String.valueOf(location.getLatitude());
longitude1 = String.valueOf(location.getLongitude());
}
}
Need help here, about onPostExecute. If I want to put the update on textView what should be the code and what should I do?.
#Override
protected Value[] doInBackground(Integer... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);
Value[] variableValues = batteryLevel.getValues();
return variableValues;
}
#Override
protected void onPostExecute(Value[] variableValues) {
// Update your views here
}
Do as follows, save reference to TextView as member and use it in nested AsyncTask:
public class YourActivity extends Activity{
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
textView = (TextView)findViewById(R.id.yourTextViewID);
...
}
private YourAsyncTask extends AsyncTask{
...
#Override
protected void onPostExecute(Value[] variableValues) {
if(textView != null)
textView.setText("your text");
}
}
}
If you want get last value from Ubidots to your textview. You can try this, but u must set the textview first OnCreate
#Override
protected void onPostExecute(Value[] variableValues) {
// Update your views here
String status = Double.toString(variableValues[0].getValue());
//this is textview for show last value from ubidots
mBatteryStatus.setText(status);
}
}
I have an ActionBar and it has Tabs.
In the MainActivity there is a LocationListener implemented.
In onLocationChanged(Location location) method I save the longitude value to double longitude variable.
I would like to pass this value to the RunFragment class.
Could anybody help me?
This is the MainActivity code:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener, LocationListener{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
double longitude;
private LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Run").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Map").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Statistics").setTabListener(this));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
});
protected void startLocationUpdate() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public void onLocationChanged(Location location) {
longitude=location.getLongitude();
}
This is the RunFragment code:
public class RunFragment extends Fragment {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.run_layout, container, false);
tv =(TextView)view.findViewById(R.id.textView1);
return view;
}
public void setTv(String string){
tv.setText(string);
}
}
Can't you get a reference to the RunFragment on the Activity through a findViewById()? Then add a method to RunFragment and call it from onLocationChanged().
I develop an Android app.
I want to stock the gps location and draw a line on the map with these datas, like "draw my course".
What's the best way ?
I designed my map with TileMill.
Where can I stock my data and use it with mapbox?
This is my code :
public class GPSActivity extends Activity{
private LocationManager manager = null;
private MyLocationListener listener;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.location);
listener = new MyLocationListener(textView);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener(textView);
manager.requestLocationUpdates(LocationManager
.GPS_PROVIDER, 5000, 10,listener);
}
public class MyLocationListener implements LocationListener
{
TextView textView;
public MyLocationListener(TextView textView)
{
this.textView = textView;
}
#Override
public void onLocationChanged(Location location) {
this.textView.setText(location.toString());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
--> With this code, I have the location. I would like to stock longitude and latitude and use these with mapbox. What's the best way to stock these datas?
I start to design my map on map box : https://a.tiles.mapbox.com/v3/malicia.hh3dgalc/page.html?secure=1#13/48.1169/-1.6803
It will be an interactive map, I want to draw an itinerary with the datas stocked.
Can someone help me?
I have declared a button in A.Class and have set disabled.
public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
Now i want to enable this button in another class(B.class)
I tried enabling this button by giving
Button s2=A.s1;
But its throwing an error saying "Cannot make a static reference to the non-static method findViewById(int) from the type Activity"
Pls help me in correcting the error
`First Class:
public class Mapper extends Activity implements OnClickListener
{
public static int sng=0,c=0;
public static double lat;
public static double lon;
public String placenametemp;
public Bundle savedInstanceState;
public static int chk= MapMe.check;
LocationManager locman1= MapMe.locman;
MyOwnLocationOverlay mylocover=MapMe.myLocationOverlay;
public MediaPlayer msong=MapMe.mp;
***public Button s1=(Button)findViewById(R.id.sn1);***
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.geocode_button);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.latlong_button);
secondButton.setOnClickListener(this);
View thirdButton = findViewById(R.id.presentLocation_button);
thirdButton.setOnClickListener(this);
View selectsong=findViewById(R.id.song_select);
selectsong.setOnClickListener(this);
s1.setOnClickListener(this);
s1.setEnabled(false);
}
}
Second Class :
public class MapMe extends MapActivity implements LocationListener
{
Button s1=(Button)findViewById(R.id.sn1);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // Suppress title bar to give more space
setContentView(R.layout.mapme);
updateGPSprefs();
// Set up location manager for determining present location of phone
locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Listener for GPS Status...
final GpsStatus.Listener onGpsStatusChange = new GpsStatus.Listener(){
public void onGpsStatusChanged(int event){
switch(event){
case GpsStatus.GPS_EVENT_STARTED:
// Started...
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
// First Fix...
Toast.makeText(MapMe.this, "GPS has First fix",
Toast.LENGTH_LONG).show();
break;
case GpsStatus.GPS_EVENT_STOPPED:
// Stopped...
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// Satellite update
break;
}
GpsStatus status = locman.getGpsStatus(null);
Iterable<GpsSatellite> satlist = status.getSatellites();
}
};
public void fu(double a,double b,double c,double d)
{
if((val==0)&&(val1==0))
{
mp.reset();
try {
check=1;
System.out.println("matched");
mp.setDataSource(link1);
mp.prepare();
mp.start();
Toast.makeText(MapMe.this, "playing", Toast.LENGTH_LONG).show();
System.out.println("Song button1");
***s1.setEnabled(true);***
System.out.println("Song button");
}
catch(Exception e)
{
}
}
`
You are calling findViewById() in wrong location. You should call it from within some non-static method like onCreate(). This method i.e findViewById() is non-static, so you can not call it the way you have mentioned. So you should declare your variable as static and then set its value in onCreate() of the activity.
Why dont you just call findViewById() in B class and then disable the view. As long as the view is available in the currently loaded layout the following code in B class should do your job.
Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
inside Class A :
public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
Inside Class B:
you should Write : A.s1.setEnabled(true);
EDITED
inside Mapper.java :
Class Mapper extends Activity{
public static Button s1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
}
}
inside Mapme.java :
Class Mapme extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_new);
Mapper.s1.setEnabled(true);
}
}
try this out. Might Help you.