onsharedpreferencechangelistener无法在Android应用中运行

时间:2016-05-19 06:21:08

标签: android sharedpreferences

我写了这个与sharedPreference一起使用的代码, 在sosbutton我改变了sharedPreference,我想在sharedPreference改变时,  onsharedpreferencechangelistener fire。 我写了这段代码,但是onsharedpreferencechangelistener没有开火。

Location location = null;
if (mlocManager!= null) {
                    location = mlocManager
                         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }

请帮帮我:( 提前谢谢

3 个答案:

答案 0 :(得分:0)

不推荐使用

MODE_WORLD_READABLE,请尝试MODE_PRIVATE

答案 1 :(得分:0)

试试这可能对你有所帮助...... 你没有提到你的

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

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


     final    SharedPreferences prefs = getSharedPreferences("demopref", Context.MODE_PRIVATE);
        final Button sosBtn=(Button) findViewById(R.id.button);
        sosBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                String data = prefs.getString("demostring", "No Value");

                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("demostring","HI...");
                editor.commit();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);

        ////////////////////////////////
            }
            });        

         listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

                Toast.makeText(getApplicationContext(),"changed : " + key,Toast.LENGTH_LONG).show();


            }
        };

        prefs.registerOnSharedPreferenceChangeListener(listener);
    }

}

这是我的代码

select Referencia,Designacao,cor,Tamanho,sum(qtd),entreguesede,stock
from
(
Select  
isnull(ST.REF,'') as Referencia
, fi.design as Designacao
, fi.cor Cor
, fi.tam Tamanho
, sum(case when ft.tipodoc=3 then -fi.qtt else fi.qtt end) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) Stock

from    fi (nolock) 
inner join ft (nolock) on ft.ftstamp=fi.ftstamp  
inner join td (nolock) on ft.ndoc=td.ndoc and td.regrd=0 
left    join st (nolock) on fi.ref = st.ref or fi.oref = st.ref
left    join sz (nolock) on sz.no = fi.armazem

where fi.composto=0 
and ft.fdata between '20160301' and '20160331'
and ft.anulado=0 
and ft.fno>=0  
and fi.qtt<>0
and ft.tipodoc in(1, 2, 3)
and ((FI.ARMAZEM  between 1000 and 1999) or fi.armazem in(10,20))
and isnull(sz.nome,'') like '%' +  + '%'

group   by FI.REF, fi.design,fi.cor,fi.tam,st.ref

union all

Select  
isnull(ST.REF,'') Referencia
, fi.design Designacao
, fi.cor Cor
, fi.tam Tamanho
,sum(case when ft.tipodoc=3 then -fi.qtt else fi.qtt end) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) Stock

from    fi (nolock) 
inner join ft (nolock) on ft.ftstamp=fi.ftstamp  
inner join td (nolock) on ft.ndoc=td.ndoc and td.regrd=0 
left    join st (nolock) on fi.ref = st.ref or fi.oref = st.ref
left    join sz (nolock) on sz.no = fi.armazem
where fi.composto=0 
and ft.fdata >= '20150505'
and ft.fdata between '20160301' and '20160331'
and ft.anulado=0 
and ft.fno>=0  
and fi.qtt<>0
and ft.ndoc in (401,501)
and isnull(sz.nome,'') like '%' +  + '%'

group   by FI.REF, fi.design, fi.cor, fi.tam, st.ref

union all

select 
isnull(ST.REF,'') Referencia
, bi.design as Designacao
, bi.cor Cor
, bi.tam Tamanho
,sum(bi.qtt) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=bi.ref and sl.cor=bi.cor and sl.tam=bi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=bi.ref and sl.cor=bi.cor and sl.tam=bi.tam and sl.datalc<='20160331'),0) Stock

from bi(nolock)
inner join bo(nolock) on bo.bostamp = bi.bostamp  
inner join bo2(nolock) on bo2stamp = bo.bostamp
inner join ts(nolock) on ts.ndos = bo.ndos and ts.ndos = 60
left join st(nolock) on bi.ref = st.ref
left join sz(nolock) on sz.no = bi.armazem
where bi.composto=0 
and bo.dataobra between '20160301' and '20160331'
and bo2.anulado= 0 
and bo.obrano >= 0
and bi.qtt<>0
and isnull(sz.nome,'') like '%' +  + '%'

group   by bi.REF, bi.design, bi.cor, bi.tam, st.ref
) a
group by Referencia,Designacao,cor,Tamanho,entreguesede,stock
order   by  1,2,3,4,6,7

答案 2 :(得分:0)

我解决了我的问题:

  public class MainActivity  extends Activity {

        SharedPreferences pref;
        SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {

            @Override
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                if(key.equals("demopref"))
                Toast.makeText(getApplicationContext(),"Hello2---"+key,Toast.LENGTH_LONG).show();

            }
        };

        @SuppressWarnings("deprecation")

        int i=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

            ////////////////////////////////////////

            final Button sosBtn = (Button) findViewById(R.id.button);
            sosBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putString("demopref","HI"+String.valueOf(i));
                    i++;
                    editor.commit();
                    /////////////////////////////////

                }
            });



        }

        @Override
        protected void onResume() {
            pref.registerOnSharedPreferenceChangeListener(listener);

            super.onResume();
        }

        @Override
        protected void onPause() {
            pref.unregisterOnSharedPreferenceChangeListener(listener);

            super.onPause();
        }


    }