Android APP間授權登錄(無需SDK類似微信登錄)(源碼+解析)
文章目錄
前言
記錄每天開發功能需求點以及踩坑點,方便以後查閱;如文章對讀者,道友,有幫助,少踩點坑,少一點頭疼,更是功德無量
一 需求
這兩天做了一個需求,(類似微信等的授權登錄)就是手頭做的這個項目暫且稱之爲APP B提供給友商去拉起,提供授權登錄的操作,授權成功後返回登錄憑證token等信息給APP A處理
- 1,實現APP間相互調起(查看了相關技術資料,三種實現方式)
- 2,拉起指定的授權頁面(SignActivity)
- 3,應用間數據相互傳遞
二 A應用(調用者)業務代碼與佈局
調用者A,MainActivity 頁面代碼
MainActivity簡單一個發起授權按鈕,下面是相關邏輯,博主面向不同讀者做了統一適配,詳細3步註釋如下
1,註冊按鈕與事件,發起事件核心代碼intent.setData(Uri.parse(mUri))
2,註冊廣播,核心代碼 intentFilter.addAction(“kx.com.kx.b.sign”) ,action讀者可自行定義
3,接收廣播回調回來的參數,進行處理
public class MainActivity extends AppCompatActivity {
private Button mBt;
// com.kx.b 是B授權包名,如果讀者Uri調起APP不熟悉,請查閱Uri拉起APP相關知識點,博主有時間的話,會在下篇補上相關知識點
private String mUri = "kx://com.kx.b/sign?type=1&user_package_name=com.kx.b";
private MyBroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//操作1 按鈕發起
mBt = findViewById(R.id.bt);
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setData(Uri.parse(mUri));//參數拼接在URI後面 type=1是授權頁面,user_package_name使用者包名,後續參數可自行添加
intent.putExtra("", "");//這裏Intent也可傳遞參數,但是一般情況下都會放到上面的URL中進行傳遞
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
//操作2
//註冊廣播接受者,接收授權成功返回廣播信息
mReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
//kx.com.kx.b.sign 自行定義action 即可
intentFilter.addAction("kx.com.kx.b.sign");
registerReceiver(mReceiver, intentFilter);
}
//操作3
//BroadcastReceiver 接收授權成功返回廣播信息,TODO
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 授權成功 返回token,app_pkg
Toast.makeText(MainActivity.this,"授權成功!",Toast.LENGTH_SHORT).show();
final String token = intent.getStringExtra("token");
final String app_pkg = intent.getStringExtra("app_pkg");
mBt.setText("token=" + token + "\n"+"app_pkg=" + app_pkg);
//todo 調起方登錄操作 做你需要的需求
}
}
//別忘了回收廣播 不然會報error,這裏是一個內存回收的知識點,第二個有時間博主會補上的知識點
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
MainActivity 對應的activity_main.xml佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt"
android:text="APP授權登陸"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
A應用註冊AndroidManifest.xml清單添加權限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
三 B應用(授權應用)業務代碼 (博主需求點也順路寫上,讀者可不讀下面123點)與佈局代碼
B(授權應用),SignActivity 頁面代碼
1,SignActivity打開前,先處理A應用數據,傳遞給服務器校驗是否授權
2,服務器給予授權,則是上面gif看到正常頁面,點擊按鈕授權即可
3,B應用未登錄先走登錄頁面,登錄後再進入上面授權頁面
4, 按鈕授權 核心代碼intent1.setAction(“kx.com.kx.b.sign”) ,sendBroadcast(intent1);
public class SignActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBt;
private String mUser_package_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign);
mBt = findViewById(R.id.bt_confirm_login);
mBt.setOnClickListener(this);
mBt.setEnabled(false);
//獲取傳遞的數據
Intent intent = getIntent();
Uri uri = intent.getData();
//獲取參數值
String type = uri.getQueryParameter("type");
mUser_package_name = uri.getQueryParameter("user_package_name");
//類型type 檢驗
if (TextUtils.equals(type, "1")) {
//Todo 未登錄 處理
request5037();
}
}
/**
* 外部 app 拉起授權 (code:5037)
* 服務器校驗 是否授權
* 授權 頁面A狀態
*/
private void request5037() {
mBt.setEnabled(true);
}
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setAction("kx.com.kx.bapp.sign");
intent1.putExtra("token", "xxxx-xxxx-xxxxx");
intent1.putExtra("app_pkg", "com.kx.aapp");
sendBroadcast(intent1);
finish();
}
}
SignActivity 對應的佈局代碼activity_sign.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_gravity="center_horizontal"
android:background="@color/color_eeeeee"/>
<ImageView
android:id="@+id/img_head"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="60dp"
android:background="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:text="B應用"
android:textColor="@color/color_333333"/>
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@color/color_eeeeee"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:text="登錄後應用將獲取一下權限"
android:textColor="@color/color_333333"
android:textSize="14sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_vertical"
android:background="@drawable/shape10_bg_bfbfbf"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="獲取的公開信息(暱稱、頭像等)"
android:textColor="@color/color_bfbfbf"
android:textSize="11sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_success_show"
android:visibility="gone"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_use_head"
android:layout_width="28dp"
android:layout_height="28dp"
/>
<TextView
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:textColor="@color/color_333333"
android:id="@+id/tv_use_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/bt_confirm_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:padding="6dp"
android:text="@string/comfirm_login"
android:textColor="@color/white"
android:textSize="20sp"/>
</LinearLayout>
B應用在清單文件AndroidManifest.xml 需要配置
<!--授權頁面-->
<activity android:name=".SignActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.DELETE"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data
android:host="com.kx.bapp"
android:path="/sign"
android:scheme="kx"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
四 結尾
demo(包含2個APP,A申請授權,B授權應用)下載地址:
https://download.csdn.net/download/qxing123456789/10778914
如有寫的不好,歡迎提出bug或者不足,衷心感謝,聯繫個人郵箱[email protected]
###腳註
多說一嘴:1.
多說一嘴:各位看文的兄弟姐妹多多鍛鍊身體,願大家身體健康. ↩︎