共享首选项中的 MODE_PRIVATE 错误 [英] MODE_PRIVATE in sharedpreferences error

查看:46
本文介绍了共享首选项中的 MODE_PRIVATE 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此代码用作 BroadcastReceiver ,但它说

I use this code as a BroadcastReceiver , but it says that

MODE_PRIVATE 无法解析为变量广播接收器

MODE_PRIVATE cannot be resolved to a variable broadcastreceiver

public class anyNewService extends BroadcastReceiver {
String uid,text;
int c=1;


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    SharedPreferences settings =getSharedPreferences("CASPreferences", MODE_PRIVATE);
    uid = settings.getString("uid", "");
    anyNewCheker();
}


public void anyNewCheker()
{
      HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("http://10.0.2.2/cas/users/anynew.php");
        try
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);  
            nameValuePairs.add(new BasicNameValuePair("uid", uid));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
            // Execute HTTP Post Request  
            HttpResponse response = httpclient.execute(httppost);

            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(20);

             int current = 0;  
             while((current = bis.read()) != -1){  
                    baf.append((byte)current);  
             }  

            /* Convert the Bytes read to a String. */
            text = new String(baf.toByteArray());
            Log.e("text",text);
            if(!text.equals("0"))
            {



            }
        }
        catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block
        Log.e("error","err 1");

    } catch (IOException e) {  
            // TODO Auto-generated catch block
        Log.e("error","err 2");

    }

}

}

我该怎么办?谢谢

推荐答案

context.getSharedPreferences("CASPreferences", Context.MODE_PRIVATE);

MODE_PRIVATE 直接在 Activity 中工作,因为 Activity 继承自 Context.广播接收器没有.

MODE_PRIVATE works directly in an Activity as an Activity inherits from Context. A broadcast receiver does not.

这篇关于共享首选项中的 MODE_PRIVATE 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆