2013-01-23 101 views
1

我有下面的類:Android的情況下錯誤

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

和我收到這條線上的錯誤"The method getPreferences(int) is undefined for the type Context"

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

我使用Context context = getApplicationContext();任何想法傳遞方面從以前的方法如何解決這個問題?

回答

1

嘗試,而不是

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

直從documentation,沒有getPreferences方法,但有一個getSharedPreferences()方法。

請注意,如果您使用的是默認的SharedPreferences文件,you can do

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 

編輯:getSharedPreferences()只是Activity類的一部分。這種方法實際上是一種方便的方法。

這只是調用底層getSharedPreferences(字符串,整數) 方法,通過本次活動的類名傳遞作爲首 名。

所以如果你是絕對相信context是活動的一個實例,您可以更改方法聲明:

public void register(Activity activity, Intent intent) { 

或投

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

但是,如果context一個活動的實例,你將不得不依賴其他方法來訪問首選項。

+0

啊好吧,我沒看夠,我看到了這個代碼示例,但我想這只是一個活動。 'SharedPreferences sharedPref = getActivity()。getPreferences(Context.MODE_PRIVATE);' – user2004988

+1

@ρяσѕρєяK只有一種活動方法。 –

+0

@ user2004988這只是Activity [class]的一部分(http://developer.android.com/reference/android/app/Activity.html#getPreferences(int))所以是的,你是對的:) –