2016-11-28 9 views
-1

Facebookのログインがあるアプリを開発しています。私はユーザー情報を取得しようとしているし、私のserver.iに送信sdkを使用しています4.i共有設定を使用して情報を保存しようとしているが、私は共有の設定については考えていない。これは私のFacebookのログイン活動です。これはランチャー活動です。facebookでログインsdkの共有設定とユーザーに関する情報を取得

public class LoginActivity extends AbsRuntimePermission { 
private static final int REQUEST_Permission =10; 


    private TextView info; 
    private LoginButton loginButton; 
    private CallbackManager callbackManager; 
    private SparseIntArray mErrorString; 
    private Button facebook_button; 
    ProgressDialog progress; 
    private String facebook_id,f_name,m_name,l_name,gender,profile_image,full_name,email_id; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    //for permission in manifest file cause it is launch activity 

    requestAppPermissions(new String[]{ 
      Manifest.permission.ACCESS_FINE_LOCATION, 
      Manifest.permission.ACCESS_COARSE_LOCATION, 
      Manifest.permission.CALL_PHONE, 
      Manifest.permission.INTERNET},R.string.msg,REQUEST_Permission); 

    //facebook 
    info = (TextView) findViewById(R.id.info); 
    loginButton = (LoginButton) findViewById(R.id.login_button); 
    progress = new ProgressDialog(LoginActivity.this); 
    progress.setMessage(getResources().getString(R.string.please_wait_facebooklogin)); 
    progress.setIndeterminate(false); 
    progress.setCancelable(false); 

    facebook_id=f_name=m_name=l_name=gender=profile_image=full_name=email_id=""; 

    //facebook sdk 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 

    //register callback object for facebook result 

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      progress.show(); 
      Profile profile = Profile.getCurrentProfile(); 
      if(profile != null){ 
       facebook_id = profile.getId(); 
       f_name=profile.getFirstName(); 
       m_name=profile.getMiddleName(); 
       l_name=profile.getLastName(); 
       profile_image=profile.getProfilePictureUri(400,400).toString(); 
      } 
      //show Toast 
      GraphRequest request =GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback(){ 
         @Override 
         public void onCompleted(JSONObject object, GraphResponse response) { 
          try { 
           email_id = object.getString("email"); 
           gender = object.getString("gender"); 
           String Profile_name = object.getString("name"); 
           long fb_id = object.getLong("id"); //use this for logout 

           //Starting a new activity using this information 
           Intent i = new Intent(LoginActivity.this, MainActivity.class); 
           i.putExtra("type", "facebook"); 
           i.putExtra("Facebook_id", facebook_id); 
           i.putExtra("f_name", f_name); 
           i.putExtra("m_name", m_name); 
           i.putExtra("l_name", l_name); 
           i.putExtra("full_name", full_name); 
           i.putExtra("Profile_image", profile_image); 
           i.putExtra("email_id", email_id); 
           i.putExtra("gender", gender); 

           progress.dismiss(); 
           startActivity(i); 
           finish(); 

          }catch (JSONException e){ 
           e.printStackTrace(); 
          } 
         } 
        }); 
      request.executeAsync(); 
     } 

     @Override 
     public void onCancel() { 
     Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show(); 
     progress.dismiss(); 
     } 

     @Override 

     public void onError(FacebookException error) { 
      Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show(); 
      progress.dismiss(); 
     } 
    }); 

    //facebook button click 
    facebook_button.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View view) { 
      LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","user_friends","email")); 

     } 
    }); 
} 

private void logoutFromFacebook(){ 
    try{ 
     if(AccessToken.getCurrentAccessToken()==null){ 
      return; 
     } 

    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode,resultCode,data); 
} 

@Override 
public void onPermissionGranted(int requestCode) { 

    //anything after permission Granted 
    Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_LONG).show(); 

} 



public void goTo(View view) { 
    Intent intent = new Intent(this, MainActivity.class); 
    this.startActivity(intent); 
} 

}

+0

あなたのエラーログを共有してください – Shailesh

+0

@ShaileshLimbadiya sir今、私はそのエラーを修正しました...今問題は、ログインが完了し、共有設定http://pastebin.comの中に何も保存しないときに私の次のアクティビティにジャンプできません。/py23Gtqfここで私のフルコードをチェックしてください。 –

+0

私の答えを確認してください – Shailesh

答えて

1

AndroidのSharedPreferences私たちは、キーと値の形で民間プリミティブ アプリケーションデータを格納することができますpair.For詳細はthisに従ってください。

共有設定から読み込むには、次のようにします。

String dateFromSharedPreferences=getActivity().getPreferences(Context.MODE_PRIVATE); 
sharedPref.getString("Username"); 

あなたはTextViewの中で渡された値ANSショーを取得するには、次のようにonCreate機能でMainActivityでそうコードMainActivityLoginActivityから値を渡している共有好み

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putString("Username", YourUsername); 
    editor.putString("Password", YourPassword); 
    editor.apply(); 

に保存します。

((TextView)findViewById(R.id.tv1)).setText(getIntent().getExtras().getString("full_name")); 
((TextView)findViewById(R.id.tv2)).setText(getIntent().getExtras().getString("email_id")); 
((TextView)findViewById(R.id.tv3)).setText(getIntent().getExtras().getString("gender")); 
+0

アプリがクラッシュしています –

+0

@kuldeeprajのでログを今すぐ送信してください – jafarbtech

+0

http://pastebin.com/VTE4LPQJ @jafarbtechここにあります –

0
SharedPreferences sharedPreferences; 
SharedPreferences.Editor editor; 

意思に行く前にこのコードを記述します。

sharedPreferences = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE); 
      editor = sharedPreferences.edit(); 
      editor.putString("User_Name", f_name); 
      editor.putString("User_Email", email_id); 
      editor.apply(); 
+1

私はこのチュートリアルに従っているhttp://www.coders-hub.com/2014/07/how-to-add-facebook-sdk-in-android.html#.WDvxyaJ96DU –

+0

なぜ私はgetFacebookIdを得ることができないlogoutFromFacebookの私か? –

+0

http://pastebin.com/VTE4LPQJこれを確認して –

0

私はこのタイプの質問をたくさん回答していますが、まだ私はこの質問の答えを与えています。すべてのデータを処理するメソッドを作成するのは簡単です。 FacebookのJSONオブジェクトを受け取った後に、それ以降のようにします。あなたは、バンドル内のすべてのFacebookのデータとストアを取得することができます//ここで

try { 
     Bundle bFacebookData = getFacebookData(object); 
     progressDialog.dismiss(); 
     progressDialog.cancel(); 
     setSessionData(bFacebookData); 

    }catch (JSONException e){ 
     e.printStackTrace(); 
    } 

あなたは

String userId = "", userEmail = "", userFirstName = "", userLastName = "", 
      userPhone = "", dateOfBirth = "", gender = "", profilePic = ""; 

    private void setSessionData(Bundle bundle) 
    { 
     try 
     { 
      userId = bundle.getString("userId"); 
      userEmail = bundle.getString("email"); 
      userFirstName = bundle.getString("first_name"); 
      userLastName = bundle.getString("last_name"); 
      userPhone = bundle.getString("phone"); 
      dateOfBirth = bundle.getString("birthday"); 
      gender = bundle.getString("gender"); 
      profilePic = bundle.getString("profile_pic"); 

      if (userId !=null) 
      { 

       SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 

       editor.putString("userId", userId); 
       editor.putString("email", userEmail); 
       editor.putString("first_name", userFirstName); 
       editor.putString("last_name", userLastName); 
       editor.putString("phone", userPhone); 
       editor.putString("birthday", dateOfBirth); 
       editor.putString("gender", gender); 
       editor.putString("profile_pic", profilePic); 

       editor.apply(); 


       Intent i = new Intent(activity, MainActivity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       activity.startActivity(i); 
      } 
      else 
      { 
       //"Some thing went wrong Please try again" 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrach(); 
     } 
    } 

あなたの別のクラスの新しい活動をSharedPreferencesにデータを格納して起動することができます//ここ

private Bundle getFacebookData(JSONObject object) 
    { 
     try { 
      Bundle bundle = new Bundle(); 
      //String id = object.getString("id"); 

      try 
      { 
       URL profile_pic = new URL("https://graph.facebook.com/" + object.getString("id") + "/picture?width=200&height=150"); 
       Log.i("profile_pic", profile_pic + ""); 
       if (profile_pic.toString() != null) 
       { 
        bundle.putString("profile_pic", profile_pic.toString()); 
       } 
       else 
       { 
        bundle.putString("profile_pic", "noProfilePic"); 
       } 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
       return null; 
      } 

      if (object.has("id")) 
      { 
       if (object.getString("id") != null) 
       { 
        bundle.putString("userId", object.getString("id")); 
        Log.i(TAG, "Info " + object.getString("id")); 
       } 
       else 
       { 
        bundle.putString("userId", "noUserId"); 
        Log.i(TAG, "Info " + object.getString("id")); 
       } 
      } 
      if (object.has("first_name")) 
      { 
       if (object.getString("first_name") != null) 
       { 
        bundle.putString("first_name", object.getString("first_name")); 
        Log.i(TAG + "Info", object.getString("first_name")); 
       } 
       else 
       { 
        bundle.putString("first_name", "noFirstName"); 
        Log.i(TAG + "Info", object.getString("first_name")); 
       } 
      } 
      if (object.has("last_name")) 
      { 
       if (object.getString("last_name") != null) 
       { 
        bundle.putString("last_name", object.getString("last_name")); 
        Log.i(TAG + "Info", object.getString("last_name")); 
       } 
       else 
       { 
        bundle.putString("last_name", "noLastName"); 
        Log.i(TAG + "Info", object.getString("last_name")); 
       } 
      } 

      if (object.has("email")) 
      { 
       if (object.getString("email") != null) 
       { 
        bundle.putString("email", object.getString("email")); 
        Log.i(TAG + "Info", object.getString("email")); 
       } 
       else 
       { 
        bundle.putString("email", "noEmail"); 
        Log.i(TAG + "Info", object.getString("email")); 
       } 
      } 


      //this is added dynamically 
      bundle.putString("phone", "noPhone"); 

      if (object.has("gender")) 
      { 
       if (object.getString("gender") != null) 
       { 
        bundle.putString("gender", object.getString("gender")); 
        Log.i(TAG + "Info", object.getString("gender")); 
       } 
       else 
       { 
        bundle.putString("gender", "noGender"); 
        Log.i(TAG + "Info", object.getString("gender")); 
       } 
      } 

      if (object.has("birthday")) 
      { 
       if (object.getString("birthday") != null) 
       { 
        bundle.putString("birthday", object.getString("birthday")); 
        Log.i(TAG + "Info", object.getString("birthday")); 
       } 
       else 
       { 
        bundle.putString("birthday", "noDateOfBirth"); 
        Log.i(TAG + "Info", object.getString("birthday")); 
       } 
      } 

      if (object.has("location")) 
      { 
       if (object.getJSONObject("location").getString("name") != null) 
       { 
        bundle.putString("location", object.getJSONObject("location").getString("name")); 
        Log.i(TAG + "Info", object.getJSONObject("location").getString("name")); 
       } 
       else 
       { 
        bundle.putString("location", "noLocation"); 
        Log.i(TAG + "Info", object.getJSONObject("location").getString("name")); 
       } 
      } 

      return bundle; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 

     } 
     return null; 
    } 

SharedPreferencesからデータを受信するには

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE); 

((TextView)findViewById(R.id.tv1)).setText(sharedPreferences.getString("first_name", "")); 
((TextView)findViewById(R.id.tv2)).setText(sharedPreferences.getString("last_name", "")); 
関連する問題