第三方共享(长按登录,点击确认)注册
例:
在Module的build.gradle文件中添加依赖和属性配置(记得还有第三方共享的jar包要导):
将文件中的jar包导入工程中的libs文件夹 并引用。。。
点击打开链接:https://download.csdn.net/download/jun_tong/10434430
dependencies {
compile 'com.umeng.sdk:common:latest.integration' compile 'com.umeng.sdk:analytics:latest.integration'
}
在AndroidManifest.xml清单文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- 推荐的权限 --> <!-- 添加如下权限,以便使用更多的第三方SDK和更精准的统计数据 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity主页面:
public class MainActivity extends AppCompatActivity implements LoginView { private EditText et_userName; private EditText et_password; private ProgressBar progressBar; private LoginPresenter loginPresenter; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); loginPresenter = new LoginPresenter(this); } private void initview() { et_userName = (EditText) findViewById(R.id.main_et_username); et_password = (EditText) findViewById(R.id.main_et_password); progressBar = (ProgressBar) findViewById(R.id.main_progressBar); button =(Button) findViewById(R.id.denglu); button.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { UMImage image = new UMImage(MainActivity.this, R.drawable.umeng_socialize_qq); new ShareAction(MainActivity.this).withMedia(image).setDisplayList(SHARE_MEDIA.QQ) .setCallback(umShareListener).open(); return true; } }); } private UMShareListener umShareListener = new UMShareListener() { /** * @descrption 分享开始的回调 * @param platform 平台类型 */ @Override public void onStart(SHARE_MEDIA platform) { } /** * @descrption 分享成功的回调 * @param platform 平台类型 */ @Override public void onResult(SHARE_MEDIA platform) { Toast.makeText(MainActivity.this,"成功 了",Toast.LENGTH_LONG).show(); } /** * @descrption 分享失败的回调 * @param platform 平台类型 * @param t 错误原因 */ @Override public void onError(SHARE_MEDIA platform, Throwable t) { Toast.makeText(MainActivity.this,"失 败"+t.getMessage(),Toast.LENGTH_LONG).show(); } /** * @descrption 分享取消的回调 * @param platform 平台类型 */ @Override public void onCancel(SHARE_MEDIA platform) { Toast.makeText(MainActivity.this,"取消了",Toast.LENGTH_LONG).show(); } }; public void onResume() { super.onResume(); MobclickAgent.onResume(this); } public void onPause() { super.onPause(); MobclickAgent.onPause(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } //点击登录 public void LoginClick(View view) { loginPresenter.login(); } @Override public String getUsername() { return et_userName.getText().toString(); } @Override public String getPassword() { return et_password.getText().toString(); } @Override public Context context() { return this; } @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideLoading() { progressBar.setVisibility(View.GONE); } @Override public void showSuccessMsg(User user) { Toast.makeText(MainActivity.this, "User " + user.getUsername() + " Login Sccess!", Toast.LENGTH_SHORT).show(); } @Override public void showFailedMsg(String s) { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void clearEditText() { et_userName.setText(""); et_password.setText(""); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } public void Register(View view) { startActivity(new Intent(this,RegisterActivity.class)); } }
主方法布局MainActivity.XML
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:id="@+id/denglu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:layout_toLeftOf="@id/align" android:onClick="LoginClick" android:text="登录" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_et_password" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/align" android:onClick="Register" android:text="注册" /> </RelativeLayout>
LoginView 接口:
public interface LoginView { //得到用户填写的信息 String getUsername(); String getPassword(); Context context(); //显示和隐藏登录ProgressBar void showLoading(); void hideLoading(); //登录成功或失败后,返回信息的方法 void showSuccessMsg(User user); void showFailedMsg(String s); //清楚数据 void clearEditText(); }
User类:
public class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
LoginPresenter 类:
public class LoginPresenter { private LoginView loginView; private LoginModel loginModel; private Handler mHandler; public LoginPresenter(MainActivity loginView) { this.loginView = loginView; loginModel = new LoginModelImpl(); mHandler = new Handler(); } public void login() { loginView.showLoading(); loginModel.login(loginView.getUsername(), loginView.getPassword(), new LoginModel.OnLoginListener() { @Override public void loginSuccess(final User user) { //模拟登录成功后,返回信息到Activity,吐出成功信息 mHandler.post(new Runnable() { @Override public void run() { loginView.showSuccessMsg(user); loginView.hideLoading(); } }); } @Override public void loginFailed(final String s) { //模拟登录失败后,返回信息,吐出错误信息 mHandler.post(new Runnable() { @Override public void run() { loginView.showFailedMsg(s); loginView.hideLoading(); } }); } },loginView.context()); } }
LoginModel接口:
import android.content.Context; import com.example.day_0520_umeng.Log.User; public interface LoginModel { void login(String username, String password, OnLoginListener onLoginListener, Context context); interface OnLoginListener { void loginSuccess(User user); void loginFailed(String s); } }
LoginModelImpl 类:
import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import com.example.day_0520_umeng.Log.User; public class LoginModelImpl implements LoginModel { @Override public void login(final String username, final String password, final OnLoginListener onLoginListener, Context context) { /** * 模拟子线程,执行耗时操作 */ @SuppressLint("WrongConstant") final SharedPreferences user = context.getSharedPreferences("user", Context.MODE_APPEND); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); if (username.isEmpty()||password.isEmpty()){ onLoginListener.loginFailed("Incorrect username or password."); return; } if (username.equals(user.getString("name","")) && password.equals(user.getString("pwd",""))) { onLoginListener.loginSuccess(new User(username,password)); } else { onLoginListener.loginFailed("Incorrect username or password."); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }
====================================================
====================================================
====================================================
====================================================
RegisterActivity 第二主类:
import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import com.example.day_0520_umeng.Log.User; public class RegisterActivity extends AppCompatActivity implements RegisterView { private ImageView align; private ProgressBar progressBar; private EditText et_password; private EditText et_userName; private RegisterPresenter presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); align = (ImageView) findViewById(R.id.align); progressBar = (ProgressBar) findViewById(R.id.main_progressBar); et_password = (EditText) findViewById(R.id.main_et_password); et_userName = (EditText) findViewById(R.id.main_et_username); presenter = new RegisterPresenter(this); } @Override public String getUsername() { return et_userName.getText().toString(); } @Override public String getPassword() { return et_password.getText().toString(); } @Override public Context context() { return this; } @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideLoading() { progressBar.setVisibility(View.GONE); } @Override public void showSuccessMsg(User user) { Toast.makeText(RegisterActivity.this, "User " + user.getUsername() + " Register Sccess!", Toast.LENGTH_SHORT).show(); finish(); } @Override public void showFailedMsg(String s) { Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void clearEditText() { et_userName.setText(""); et_password.setText(""); } public void RegisterClick(View view) { presenter.register(); } }
activity_register布局xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> <EditText android:id="@+id/main_et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:id="@+id/main_et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/main_et_username" android:layout_marginTop="10dp" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" /> <ProgressBar android:id="@+id/main_progressBar" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:visibility="gone" /> <ImageView android:id="@+id/align" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:layout_width="250dp" android:layout_height="wrap_content" android:layout_below="@+id/main_et_password" android:layout_centerHorizontal="true" android:layout_marginTop="12dp" android:onClick="RegisterClick" android:textSize="25sp" android:text="注册" /> </RelativeLayout>
RegisterView 接口:
public interface RegisterView { //得到用户填写的信息 String getUsername(); String getPassword(); Context context(); //显示和隐藏登录ProgressBar void showLoading(); void hideLoading(); //注册成功或失败后,返回信息的方法 void showSuccessMsg(User user); void showFailedMsg(String s); //清除数据 void clearEditText(); }
RegisterPresenter 类:
public class RegisterPresenter { private RegisterView registerView; private RegisterModelImpl registerModelImpl; private Handler mHandler; public RegisterPresenter(RegisterActivity registerView) { this.registerView = registerView; registerModelImpl = new RegisterModelImpl(); mHandler = new Handler(); } public void register() { registerView.showLoading(); registerModelImpl.register(registerView.getUsername(), registerView.getPassword(), new RegisterModel.OnRegisterListener() { @Override public void registerSuccess(final User user) { //模拟注册成功后,返回信息到Activity,吐出成功信息 mHandler.post(new Runnable() { @Override public void run() { registerView.showSuccessMsg(user); registerView.hideLoading(); } }); } @Override public void registerFailed(final String s) { //模拟注册失败后,返回信息,吐出错误信息 mHandler.post(new Runnable() { @Override public void run() { registerView.showFailedMsg(s); registerView.hideLoading(); } }); } }, registerView.context()); } }
RegisterModel 接口:
public interface RegisterModel { void register(String username, String password, OnRegisterListener onRegisterListener, Context context); interface OnRegisterListener { void registerSuccess(User user); void registerFailed(String s); } }
RegisterModelImpl 类:
public class RegisterModelImpl implements RegisterModel { @Override public void register(final String username, final String password, final OnRegisterListener onRegisterListener, Context context) { @SuppressLint("WrongConstant") final SharedPreferences user = context.getSharedPreferences("user", Context.MODE_APPEND); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); if (username.isEmpty() || password.isEmpty()) { onRegisterListener.registerFailed("用户或密码不能为空"); return; } if (username.equals(user.getString("name", ""))) { onRegisterListener.registerFailed("用户已存在"); } else { onRegisterListener.registerSuccess(new User(username, password)); SharedPreferences.Editor edit = user.edit(); edit.putString("name", username); edit.putString("pwd", password); edit.commit(); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }
==========================================
MyApp 项目Application类:
(记得在AndroidManifest.xml清单文件中的<application>里添加name)
public class MyApp extends Application{ { PlatformConfig.setWeixin("wx967daebe835fbeac","5bb696d9ccd75a38c8a0bfe0675559b3"); PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba"); PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com"); } @Override public void onCreate() { super.onCreate(); UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "1fe6a20054bcef865eeb0991ee84525b"); MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL); //初使 UMShareAPI.get(this); } }
socialize_share_menu_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <com.umeng.socialize.shareboard.SocializeImageView android:id="@+id/socialize_image_view" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@drawable/umeng_socialize_menu_default" /> <TextView android:id="@+id/socialize_text_view" android:layout_width="63dp" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:ellipsize="end" android:singleLine="true" android:text="未知" android:textSize="12sp" /> </LinearLayout>
umeng_socialize_oauth_dialog
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <com.umeng.socialize.shareboard.SocializeImageView android:id="@+id/socialize_image_view" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@drawable/umeng_socialize_menu_default" /> <TextView android:id="@+id/socialize_text_view" android:layout_width="63dp" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:ellipsize="end" android:singleLine="true" android:text="未知" android:textSize="12sp" /> </LinearLayout>
umeng_socialize_share
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <com.umeng.socialize.shareboard.SocializeImageView android:id="@+id/socialize_image_view" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@drawable/umeng_socialize_menu_default" /> <TextView android:id="@+id/socialize_text_view" android:layout_width="63dp" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:ellipsize="end" android:singleLine="true" android:text="未知" android:textSize="12sp" /> </LinearLayout>
===============================================
===============================================
===============================================
===============================================
AndroidManifest.xml清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day_0520_umeng"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- 推荐的权限 --> <!-- 添加如下权限,以便使用更多的第三方SDK和更精准的统计数据 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.umeng.qq.tencent.AuthActivity" android:launchMode="singleTask" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.umeng.qq.tencent.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/> <!-- 分享编辑页--> <activity android:name="com.umeng.socialize.editorpage.ShareActivity" android:theme="@android:style/Theme.NoTitleBar" android:excludeFromRecents="true" /> <meta-data android:value="5aaf6a54f29d984ca4000148" android:name="UMENG_APPKEY"/> <meta-data android:value="Channel ID" android:name="UMENG_CHANNEL"/> <activity android:name=".RegisterActivity"></activity> </application> </manifest>