1

私は次のクラスを持っています:

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();この問題を修正する方法のアイデアを使用して、以前の方法からコンテキストを渡しますか?

4

1 に答える 1

1

代わりに試してください

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

ドキュメントから直接、getPreferences方法はありませんが、getSharedPreferences()方法はあります。

デフォルトのSharedPreferencesファイルを使用している場合は、次のことができることに注意してください。

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 

編集: クラスgetSharedPreferences()の一部にすぎません。Activityこの方法は実際には便利な方法です。

これは、このアクティビティのクラス名を設定名として渡すことにより、基になるgetSharedPreferences(String、int)メソッドを呼び出すだけです。

したがって、それがcontextActivityのインスタンスであることが絶対に確実な場合は、メソッド宣言を変更できます。

public void register(Activity activity, Intent intent) {

またはキャスト

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

ただし、がActivityのインスタンスでないcontext場合は、設定にアクセスするために他のアプローチに依存する必要があります。

于 2013-01-23T18:28:45.700 に答える