Android實現Line登入分享
阿新 • • 發佈:2018-12-29
一、獲取引數
1、註冊登入Line開發者賬號
在Line官網並找不到註冊地方,可以通過Line APP進行註冊,註冊之後進入Line開發者官網:https://developers.line.me/en/進行開發者賬號授權。
2、建立應用
3、建立完成在Channel settings下獲取Channel ID和Channel secrt
4、在AppSettings下配置包名和簽名
5、下載Line 的aar:https://developers.line.biz/en/docs/line-login/downloads/,並引入工程
allprojects { repositories { ... flatDir { dirs 'libs' } ... } }
compile(name: 'line_sdk_4.0.8', ext: 'aar')
二、登入實現
1.APP-APP
int REQUEST_CODE=1; LineApiClient lineApiClient; LineApiClientBuilder apiBuilder= new LineApiClientBuilder(activity,lineChannelID); lineApiClient=apiBuilder.build(); if(checkApkExist(activity,"jp.naver.line.android")){//App-to-App Log.d(tag,"Login-App-to-App"); Intent loginIntent= LineLoginApi.getLoginIntent(activity,lineChannelID); } else{ //瀏覽器中的LINE登入介面 Log.d(tag,"Login-web"); loginIntent=LineLoginApi.getLoginIntentWithoutLineAppAuth(activity,lineChannelID); } activity.startActivityForResult(loginIntent,REQUEST_CODE);
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode != REQUEST_CODE) { Log.e("ERROR", "Unsupported Request"); return; } try { LineLoginResult result = LineLoginApi.getLoginResultFromIntent(data); Log.e(tag,"msg="+result.getErrorData().getMessage()); switch (result.getResponseCode()) { case SUCCESS: // Login successful Log.d(tag,"loginSuccess"); String user_id=result.getLineProfile().getUserId(); String user_name=result.getLineProfile().getDisplayName(); String accessToken = result.getLineCredential().getAccessToken().getAccessToken(); break; case CANCEL: // Login canceled by user Log.e(tag, "LINE Login Canceled by user!!"); Log.e(tag,"msg="+result.getErrorData().getMessage()); break; default: // Login canceled due to other error Log.e(tag, "Login FAILED!"+result.getErrorData().toString()); } }catch (Exception e){ e.printStackTrace(); } }
//檢查是否安裝了app
public boolean checkApkExist(Context context, String packageName){
if(packageName==null){
return false;
}
try{
ApplicationInfo applicationInfo=context.getPackageManager()
.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
三、分享實現
public void shareToLine(Activity activity,String uriString,String title,String text){
Log.d(tag,"share to Line");
String linePackageName="jp.naver.line.android";
String lineClassName="jp.naver.line.android.activity.selectchat.SelectChatActivityLaunchActivity";
ComponentName componentName=new ComponentName(linePackageName,lineClassName);
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.parse(uriString);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// shareIntent.setType("image/jpeg"); //圖片分享
shareIntent.setType("text/plain"); // 純文字
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);//分享的標題
shareIntent.putExtra(Intent.EXTRA_TEXT, text);//分享內容
shareIntent.setComponent(componentName);//跳到指定APP的Activity
activity.startActivity(Intent.createChooser(shareIntent,""));
}