Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MODE_PRIVATE in sharedpreferences error

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

like image 267
Cyb3r Avatar asked Sep 11 '11 23:09

Cyb3r


People also ask

What is Mode_private when creating shared preference file?

MODE_PRIVATE. By setting this mode, the file can only be accessed using calling application. 5. MODE_WORLD_READABLE. This mode allow other application to read the preferences.

What is Mode_private?

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. In MODE_WORLD_READABLE other application can read the created file but can not modify it.

Can we store HashMap in SharedPreferences?

Android App Development for Beginners This example demonstrates about How can I save a HashMap to Shared Preferences in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

Can we save image in SharedPreferences?

Once the user allows access to the camera, we can capture an image. We get the captured image inside onActivityResult() method. Now we call the PreferenceManager method and store the captured image into SharedPreferences.


1 Answers

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

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

like image 85
Kevin TeslaCoil Avatar answered Sep 28 '22 22:09

Kevin TeslaCoil