简体   繁体   中英

Cannot call method of MainActivity in Android

I have a classic example of Preferences where I want to call MainActivity method to hide ImageView .

But it does not go to debug even.

So how to call method of MainActivity properly?

public class SettingsActivity extends AppCompatPreferenceActivity 
...

@Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            try {
                if (key.equals("isGPS_Switch")) {
                    Boolean isGPSSwitch = sharedPreferences.getBoolean(key, false);
                    if (isGPSSwitch) {
                        // START SERVICE
                        getActivity().startService(new Intent(getActivity(), gpsService.class));

                        ((MainActivity)getActivity()).MyLocationMapDisplay(true);

                    } else {
                        // STOP SERVICE
                        getActivity().stopService(new Intent(getActivity(), gpsService.class));

                        ((MainActivity)getActivity()).MyLocationMapDisplay(false);

                    }
                }
            } catch (Exception ex) {
                Log.e("Preferences", ex.getMessage());
            }
        }

And MAinActivity class

public class MainActivity extends AppCompatActivity 
...
 private ImageView imageDisplayGPS;

 public void MyLocationMapDisplay(boolean isDisplay)
    {

        if(isDisplay)
        {
            imageDisplayGPS.setVisibility(View.VISIBLE);
        }
        else
        {
            imageDisplayGPS.setVisibility(View.GONE);
        }
    }

Nothing could simple than using BroadcastReciver

private void configureBroadcastReciver() {
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent i)
            {
                try
                {
                    String imageDisplayGPSState = (String) i.getExtras().get("btn_imageDisplayGPS");
                    if(imageDisplayGPSState.equals("true"))
                    {
                        MyLocationMapDisplay(true);
                    }
                    else
                    {
                        MyLocationMapDisplay(false);
                    }
                }
                catch (Exception ex)
                {
                    Log.d("MAIN ACTIVITY", ex.getMessage());
                }
            }
        };

        registerReceiver(broadcastReceiver, new IntentFilter("buttons_visible"));
    }

And in the Preferences is

@Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            try {
                if (key.equals("isGPS_Switch")) {
                    Boolean isGPSSwitch = sharedPreferences.getBoolean(key, false);
                    if (isGPSSwitch) {
                        // START SERVICE
                        getActivity().startService(new Intent(getActivity(), gpsService.class));

                        Intent buttons_visible = new Intent("buttons_visible");
                        buttons_visible.putExtra("btn_imageDisplayGPS", "true");
                        getContext().sendBroadcast(buttons_visible);


                    } else {
                        // STOP SERVICE
                        getActivity().stopService(new Intent(getActivity(), gpsService.class));

                        Intent buttons_visible = new Intent("buttons_visible");
                        buttons_visible.putExtra("btn_imageDisplayGPS", "false");
                        getContext().sendBroadcast(buttons_visible);


                    }
                }
            } catch (Exception ex) {
                Log.e("Preferences", ex.getMessage());
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM