在Android中,只创建一个类

时间:2015-06-02 15:46:03

标签: java android

我正在尝试使用PhoneStateListener,并且我无法关闭监听器。在我的代码中,我创建了一个TeleListener类的新实例,它扩展了PhoneStateListener。为了关闭监听器,我需要调用我最初调用的TeleListener类的相同实例。但是我编写的代码是创建一个新实例,而不是使用现有实例。所以我的问题是,如何编写它以便最初调用一个新实例,然后当我想关闭监听器时,我调用原始实例?我使用的是切换到打开和关闭监听器,所以现在看起来是切换代码:

    // ---
    TeleMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    // --- END

    // --- switch
    switch_SW = (SwitchCompat) findViewById(R.id.mainSwitch_SW);
    switch_SW.setTextOn("ON");
    switch_SW.setTextOff("OFF");

    // --- get switch prefs
    switchPref = getSharedPreferences(SWITCH_PREFS, Context.MODE_PRIVATE);
    switch_SW.setChecked(switchPref.getBoolean(switchKeyStr, true));

    switch_SW.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {

                // ---
                TeleMan.listen(tListen, PhoneStateListener.LISTEN_CALL_STATE);
                SharedPreferences.Editor swEditor = getSharedPreferences(
                        SWITCH_PREFS, MODE_PRIVATE).edit();
                swEditor.putBoolean(switchKeyStr, true);
                swEditor.commit();
                Toast.makeText(getApplicationContext(), "ON",
                        Toast.LENGTH_SHORT).show();

                // --- END
            } else {

                // ---
                TeleMan.listen(tListen, PhoneStateListener.LISTEN_NONE);

                SharedPreferences.Editor swEditor = getSharedPreferences(
                        SWITCH_PREFS, MODE_PRIVATE).edit();
                swEditor.putBoolean(switchKeyStr, false);
                swEditor.commit();
                Toast.makeText(getApplicationContext(), "OFF",
                        Toast.LENGTH_SHORT).show();
                // ---
            }

        }
    });// --- END switch

我在这里创建了Listener的实例:

public class TestActivity extends AppCompatActivity {
private SwitchCompat switch_SW;

SharedPreferences switchPref;
TelephonyManager TeleMan;

public static final String SWITCH_PREFS = "switchPref";
public static final String switchKeyStr = "switchKey";
new instance --> private TeleListener tListen = new TeleListener();

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

...然后我进入上面的Switch代码...

3 个答案:

答案 0 :(得分:1)

我认为你需要Java Singleton Class Design

如果您已按照此模式完成,那么您将使用它:

TeleMan.getInstance().method();
//                      ^--- This could be any methods you've defined
//                           in this singleton.

请注意,TeleMan.getInstance()返回的所有引用都指向相同的内存位置。

TeleMan tm1 = TeleMan.getInstance();
TeleMan tm2 = TeleMan.getInstance();
TeleMan tm3 = TeleMan.getInstance();

tm1 == tm2 == tm3

答案 1 :(得分:0)

Srinath Ganesh是对的!当我声明TeleListener变量时,我将其从私有更改为公共静态,如下所示:

        public static TeleListener tListen;

    ...

    tListen = new TeleListener();
    TeleMan.listen(tListen = new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
    SharedPreferences.Editor swEditor = getSharedPreferences(
            SWITCH_PREFS, MODE_PRIVATE).edit();
    swEditor.putBoolean(switchKeyStr, true);
    swEditor.commit();
    Toast.makeText(getApplicationContext(), "ON",
            Toast.LENGTH_SHORT).show();

现在切换开关将启用和禁用监听器类。

答案 2 :(得分:0)

尽管static可以解决问题,但我发现这种方法为smelly code

相反,请考虑保持原样并添加相应的逻辑以在onResume()onStart()回调中附加侦听器;并在tListenonPause()回调方法中分离onStop()(取决于共享首选项,当然还有标记)