13

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

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");

    }

}

}

what should I do? thank you

Cyb3r
  • 187
  • 1
  • 1
  • 10

1 Answers1

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

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

Kevin TeslaCoil
  • 10,037
  • 1
  • 39
  • 33
  • 3
    Importing Context won't give you all it's constants, it just gives you "Context" so you can do Context.MODE_PRIVATE – Kevin TeslaCoil Sep 13 '11 at 17:56
  • HI, i am facing same problem, here is code any help plz public TwitterSession(Context context) { sharedPref = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE); editor = sharedPref.edit(); } – ALi Nov 12 '13 at 12:57