1. Trang chủ
  2. » Thể loại khác

tich hop facebook trong android

10 159 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Tích hợp Facebook Android Android cho phép ứng dụng bạn kết nối tới Facebook chia liệu loại cập nhật Facebook Chương trình bày cách tích hợp Facebook vào ứng dụng Có hai cách để bạn tích hợp Facebook chia sẻ liệu từ ứng dụng, là: − • Facebook SDK • Intent Share Tích hợp Facebook SDK Đây cách để kết nối với Facebook Bạn phải đăng ký ứng dụng tiếp nhận số Facebook ID, sau bạn phải tải Facebook SDK thêm vào project Các bước liệt kê sau: Tạo app signature Bạn phải tạo Key signature, trước tạo nó, đảm bảo bạn cài đặt SSL, không bạn cần tải SSL từ .here Bây mở dòng nhắc lệnh tới jre folder (một java folder) Tới bạn cần gõ lệnh thật xác Bạn phải thay path bên “your path” với keystore mà bạn tìm thấy Eclipse việc chọn Window tab > Preferences tab sau chọn tùy chọn build android từ bên trái keytool -exportcert -alias androiddebugkey -keystore "your path" | openssl sha1 -binary | openssl base64 Sau Enter, bạn nhắc password Cung cấp password chép key mà cung cấp cho bạn Như sau: − Đăng ký ứng dụng Bây tạo ứng dụng Facebook developers.facebook.com/apps điền tất thông tin Như sau: − http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Bây tới khu vực Native Android App điền tên project, tên lớp, dán Key Hashes mà chép bước Như sau: − Nếu thứ ổn, bạn nhận App ID bí mật Chỉ cần chép ID lưu Như sau:− Tải SDK tích hợp Tải Facebook SDK Nhập vào Eclipse Sau nhập, nhấn chuột phải vào Facebook project chọn properties Chọn android > add button > facebook sdk > ok http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Tạo ứng dụng đăng nhập Facebook Sau hoàn thành thứ, bạn chạy ví dụ mẫu mà kèm với SDK tạo ứng dụng riêng cho Để đăng nhập, bạn cần gọi phương thức openActiveSession Cú pháp là: − // start Facebook Login Session.openActiveSession(this, true, new Session.StatusCallback() { // callback when session changes state public void call(Session session, SessionState state, Exception exception) { if (session.isOpened()) { // make request to;2 the /me API Request.executeMeRequestAsync(session, new Request GraphUserCallback() { // callback after Graph API response with user object @Override public void onCompleted(GraphUser user, Response response) { if (user != null) { TextView welcome = (TextView) findViewById(R.id.welcome); welcome.setText("Hello " + user.getName() + "!"); } } }); } } } Intent share Intent Share sử dụng để chia liệu ứng dụng Trong tình này, khơng xử lý SDK Stuff, ứng dụng Facebook xử lý Chúng ta đơn giản gọi ứng dụng Facebook truyền liệu để chia sẻ Theo cách này, chia sẻ thứ Facebook Android cung cấp Intent Library để chia sẻ liệu Activity App Để sử dụng Intent Share, phải xác định kiểu Share Intent Cú pháp sau:ACTION_SEND Cú pháp sau: − Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); Kế tiếp, bạn cần định nghĩa kiểu liệu để truyền, sau truyền liệu Cú pháp sau:− shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from tutorialspoint"); startActivity(Intent.createChooser(shareIntent, "Share your thoughts")); Ngoài phương thức trên, bảng liệt kê số phương thức cho phép xử lý Intent − Stt Phương thức & Miêu tả http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     addCategory(String category) Phương thức thêm Category tới Intent createChooser(Intent target, CharSequence title) Tạo ACTION_CHOOSER Intent getAction() Phương thức thu nhận Action chung để thực hiện, chẳng hạn ACTION_VIEW getCategories() Phương thức trả tập hợp tất Category Intent scale event putExtra(String name, int value) Phương thức thêm liệu kế thừa tới Intent toString() Phương thức trả chuỗi chứa miêu tả ngắn gon, người đọc đối tượng Ví dụ Ví dụ sau minh họa sử dụng IntentShare để chia sẻ liệu Facebook Nó tạo ứng dụng cho phép bạn chia số text Facebook Sau nội dung Main Activity file sửa đổi: MainActivity.java package com.example.sairamkrishna.myapplication; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Typeface; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.io.FileNotFoundException; import java.io.InputStream; import static java.lang.System.currentTimeMillis; public class MainActivity extends ActionBarActivity { private ImageView img; http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img=(ImageView)findViewById(R.id.imageView); Button b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); Uri screenshotUri = Uri.parse("android.resource://comexample.sairamkrishna.myapplication/*"); try { InputStream stream = getContentResolver().openInputStream(screenshotUri); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } sharingIntent.setType("image/jpeg"); sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); startActivity(Intent.createChooser(sharingIntent, "Share image using")); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Bạn sửa đổi nội dung res/layout/activity_main.xml Tiếp theo nội dung AndroidManifest.xml file Cuối cùng, bạn chạy ứng dụng Android vừa tạo http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Chọn thiết bị mobile hiển thị hình mặc định sau:− http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Nhấn nút bạn thấy danh sách Share Provider sau: http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Bây chọn Facebook từ danh sách sau viết thơng điệp Như sau: − http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com       http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   ... import android. net.Uri; import android. support.v7.app.ActionBarActivity; import android. os.Bundle; import android. view.Menu; import android. view.MenuItem; import android. view.View; import android. widget.Button;...

Ngày đăng: 02/12/2017, 07:09

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w