0

Now the program works fine,

this program can toggle wifi on/off

Now I want make this function to library, and simplify the code in the activity class

public class MyActivity extends Activity {

    private WifiManager wifiManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        Button bWifi = (Button) findViewById(R.id.button);
        bWifi.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view) {

                if (!wifiManager.isWifiEnabled())
                {
                    wifiManager.setWifiEnabled(true);
                }
                else
                {
                    wifiManager.setWifiEnabled(false);
                }
                Toast.makeText(getApplicationContext(), "Toggle wifi", Toast.LENGTH_LONG).show();
            }
        });

    }
}

And then I change to this. After the changes, I cannot run the app, How to call android function in other class?

public class MyActivity extends Activity {

    Setting setting=new Setting();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bWifi = (Button) findViewById(R.id.button);
        bWifi.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view) {

                setting.toggleWifi();
            }
        });

}

Setting class:

public class Setting extends Activity {
    private WifiManager wifiManager;

    public Setting()
    {

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    }


    public void toggleWifi()
    {
        if (!wifiManager.isWifiEnabled())
        {
            wifiManager.setWifiEnabled(true);
        }
        else
        {
            wifiManager.setWifiEnabled(false);
        }
        Toast.makeText(getApplicationContext(), "Toggle Wifi", Toast.LENGTH_LONG).show();
    }
}

This is second version, in this version, there is only one activity. And it can shows you the problem exactly. This version is still not work, if I delete this line

    wifiManager = (WifiManager) (this.activity.getSystemService(Context.WIFI_SERVICE));

The app will works.

public class MyActivity extends Activity {

    Setting setting=new Setting(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bWifi = (Button) findViewById(R.id.button);
        bWifi.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view) {

            }
        });

    }
}



public class Setting {
    private WifiManager wifiManager;
    private Activity activity;

    public Setting(Activity activity)
    {
        this.activity=activity;
        wifiManager = (WifiManager) (this.activity.getSystemService(Context.WIFI_SERVICE));
    }

}
CL So
  • 3,647
  • 10
  • 51
  • 95
  • 1
    Why is Settings in your app an Activity? There is only 1 activity running in the foreground at any time. I think you need to go back to the Android documents and understand exactly what an Activity is, rather than trying to pick base classes so your code will compile. – Gabe Sechan Jun 14 '14 at 16:44
  • It's not legal to create an Activity with `new Setting()` . Activity lifecycle MUST be managed by the Android framework: you can never create them with the `new` keyword. – ben75 Jun 14 '14 at 16:47

1 Answers1

1

Why does your helper class Setting extend Activity? I believe you have done this just to get an instance of the Context so that you can make a call to getSystemService(). An Activity is a UI component with it's own lifecyle. You shouldn't be using it as a utility/helper class.

Instead of creating an Activity, you can pass the context to your Settings Object from your Activity, and call getSystemService() using that context.

 public void toggleWifi(Context mContext)
{   WifiManager wifiManager = = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(true);
    }
    else
    {
        wifiManager.setWifiEnabled(false);
    }
    Toast.makeText(getApplicationContext(), "Toggle Wifi", Toast.LENGTH_LONG).show();
}

Now you can call this method from your Activity

   setting.toggleWifi(MyActivity.this);

Note: When you pass the Context to helper classes, make sure that you don't hold it longer than required.Or to make it simple, don't hold on to a Context longer than the Context exists.

coderplus
  • 5,793
  • 5
  • 34
  • 54
  • I found the problem of my ver2, because I create the Setting when I define it, `Setting setting=new Setting(this);` now I place it in onCreate `setting=new Setting(this);`, then everything is work. – CL So Jun 14 '14 at 17:34
  • Yes it will also work for you if you set Context as a class level variable, but it's a best practice to not keep a reference to Context longer than required. Beware of memory leaks. Refer http://stackoverflow.com/questions/7880657/best-practice-to-pass-context-to-non-activity-classes – coderplus Jun 14 '14 at 17:46
  • From your reference. Now I know the main problem is **Helper-class might live longer then your MainActivity**. How about if I add this in onStop()? `setting=null;`. Can this solve the problem? – CL So Jun 14 '14 at 18:18
  • I think that should work, but you will have to re-instantiate the variable on onResume if it's null. – coderplus Jun 14 '14 at 18:42