共享偏好错误中的MODE_PRIVATE

时间:2011-09-11 23:30:09

标签: android sharedpreferences

我将此代码用作BroadcastReceiver,但它表示

  

MODE_PRIVATE无法解析为变量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");

    }

}

}

我该怎么办? 谢谢

1 个答案:

答案 0 :(得分:38)

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

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