Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:34:12+00:00 2026-06-17T19:34:12+00:00

I have the following class: package com.somedomain.enigma; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import

  • 0

I have the following class:

package com.somedomain.enigma;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class APICall {
    private String DEBUG_TAG = "enigma";

    public void register(Context context, Intent intent) {
        final String URL = "http://somedomain.com:8888/register";

        String registrationId = intent.getStringExtra("registration_id");
        String error = intent.getStringExtra("error");
        String unregistered = intent.getStringExtra("unregistered"); 

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL);

        // Send a POST to the server to register the device
        if (registrationId != null) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("registrationId", registrationId));
            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                int responseCode = response.getStatusLine().getStatusCode();
                if(responseCode == 200) {
                    SharedPreferences settings = context.getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("registrationId", registrationId);
                    editor.commit();
                    Log.d(DEBUG_TAG, "Device registered successfully");
                } else {
                    Log.d(DEBUG_TAG, "Device registration failed");
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (unregistered != null) {
            // get old registration ID from shared preferences
            // notify 3rd-party server about the unregistered ID
            Log.d(DEBUG_TAG, "Unregister request");
        } 

        if (error != null) {
            if ("SERVICE_NOT_AVAILABLE".equals(error)) {
               // optionally retry using exponential back-off
                Log.d(DEBUG_TAG, "Service not available");
            } else {
                // Unrecoverable error, log it
                Log.d(DEBUG_TAG, "Received error: " + error);
            }
        }
    }
}

and I am receiving the error "The method getPreferences(int) is undefined for the type Context" on this line:

SharedPreferences settings = context.getPreferences(Context.MODE_PRIVATE);

I am passing in context from the previous method using Context context = getApplicationContext(); any ideas how to fix this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:34:13+00:00Added an answer on June 17, 2026 at 7:34 pm

    Try instead

    SharedPreferences settings = context.getSharedPreferences("MyPreferencesName",Context.MODE_PRIVATE);
    

    Straight from the documentation, there is no getPreferences method, but there is a getSharedPreferences() method.

    Note that if you are using a default SharedPreferences file, you can do

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
    

    Edit: getSharedPreferences() is only part of the Activity class. This method is actually a convenience method.

    This simply calls the underlying getSharedPreferences(String, int)
    method by passing in this activity’s class name as the preferences
    name.

    So if you are absolutely sure that context is an instance of Activity you can change the method declaration:

    public void register(Activity activity, Intent intent) {
    

    or cast

    SharedPreferences settings = ((Activity)context).getPreferences(Context.MODE_PRIVATE);
    

    However, if context is not an instance of Activity, you will have to rely on the other approaches for accessing Preferences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following action class: package com.pendulum.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest;
I have following java class package com.picvik.model; import java.util.Date; public class ViewAlbum { private
i have the following piece of code : package com.example.task; import java.util.List; import android.os.Bundle;
Here is my JUnit test class: package com.bynarystudio.tests.storefront; import java.util.List; import org.junit.Assert; import org.junit.Test;
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following classes: Main.java package com.example.webapp; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity;
I've got following java class. package com.org.data.dbresource; import org.springframework.orm.ibatis.SqlMapClientTemplate; public class DBConnectionManager { private
If I have the following class: package com.example; import org.springframework.beans.factory.annotation.Required; public class Customer {
I have done the following modifications in my code: ConnectionPool.java: package com.dao; import java.sql.*;
I have the following class in Java package com.artifex.mupdf.data; public class FzTextSpan { FzRect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.