在引导时从SharedPreferences访问数据

时间:2014-10-28 18:27:45

标签: android service broadcastreceiver sharedpreferences togglebutton

我想在启动时启动服务。它的工作正常。我现在只想在切换按钮打开时才启动服务。我在SharedPreferences中保存了切换按钮状态。

问题 我从SharedPreferences获取默认值,在我的情况下是假的

BroadcastReceiver的CODE如下

public class MyReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {




         SharedPreferences saved_values=context.getApplicationContext().getSharedPreferences( "ServiceLockTest", Context.MODE_PRIVATE);
         boolean checkstate = saved_values.getBoolean("tgpref", false); // return false
         int pr=saved_values.getInt("progess", 100); //return 100

         TheService.SHAKE_THRESHOLD=pr;

         if(checkstate)
         {
         Intent myIntent = new Intent(context, TheService.class);
         context.startService(myIntent);
         }
         else
         {
             Toast.makeText(context,"yoooooooooooooooooooooo" , Toast.LENGTH_LONG).show();
              System.out.println(pr);
              System.out.println(checkstate);
         }


    } 
}

保存切换按钮状态的代码

if (isChecked) {


                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("tgpref", true); // value to store
                    editor.commit();
                    // The toggle is enabled
                     if (mgr.isAdminActive(cn))
                     {
                         Toast.makeText(getBaseContext(),"Service is afor",Toast.LENGTH_LONG).show();


                     }
                     else
                     {
                         Log.i("Admin", "Not an admin");//mean admin is not active
                         showAct();//will open new activity
                     }
                } 

请帮忙。谢谢

0 个答案:

没有答案