接着上一篇的内容继续讲,今天主要使用leancloud进行登录和注册
第一步:加载用到的jar包
新建一个LoginDemo工程,在gradle文件利配置这样的代码
给第一个gradle修改代码,maven主要来自动下载安装jar包的
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven {
url "http://mvn.leancloud.cn/nexus/content/repositories/releases"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'org.robolectric:robolectric-gradle-plugin:0.14.+'
}
}
allprojects {
repositories {
jcenter()
maven {
url "http://mvn.leancloud.cn/nexus/content/repositories/releases"
}
maven {
url "https://raw.githubusercontent.com/YunzhanghuOpen/redpacket-maven-repo/master/release"
}
}
}
给第二个gradle修改代码
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
// LeanCloud 基础包
compile ('cn.leancloud.android:avoscloud-sdk:v3.+')
compile ('cn.leancloud.android:avoscloud-statistics:v3.+')
testCompile 'junit:junit:4.12'
}
这样就完成了所需的资源配置
第二步:写我们的登录界面
login_layout.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:background="@drawable/login_bg"
tools:context="com.xiaowu.www.logindemo.MainActivity">
<EditText
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:id="@+id/user_password"
android:layout_width="match_parent"
android:layout_height="55dp"
android:hint="密码"
android:textSize="15sp"
android:inputType="textPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
/>
<EditText
android:layout_marginRight="80dp"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="55dp"
android:id="@+id/user_name"
android:layout_above="@+id/user_password"
android:inputType="text"
android:textSize="15sp"
android:hint="用户名"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_marginLeft="110dp"
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_below="@+id/user_password"
android:layout_marginTop="5dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/regester_button"
android:text="注册"
android:layout_alignBaseline="@+id/login_button"
android:layout_alignBottom="@+id/login_button"
android:layout_toRightOf="@+id/login_button"
android:layout_toEndOf="@+id/login_button"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" />
<Button
android:layout_width="match_parent"
android:layout_height="55dp"
android:id="@+id/register_login_button"
android:text="注册并登录"
android:layout_below="@+id/user_password"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:clickable="false"
android:visibility="gone"/>
</RelativeLayout>
布局的图示为:
写代码前,必须要实现的一个类,我取的名字是class App extends Application,这里存放全局用到的量,你在leancloud申请到的App_id,App_key就写在这里面。代码如下:
package com.xiaowu.www.logindemo;
import android.app.Application;
import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVOSCloud;
/**
* Created by min on 2017/2/28.
*/
public class App extends Application
{
private static final String APP_ID = "LNlzpJ2dUVzDWSrrOiJxY028-gzGzoHs";//不要用我的,我已经改了,用了会报错
private static final String APP_KEY = "GO8JwvYeQIgdlb9F4joMm83H1";//用自己申请的
@Override
public void onCreate(){
super.onCreate();
AVOSCloud.initialize(this,APP_ID,APP_KEY);
AVOSCloud.setDebugLogEnabled(true);
AVAnalytics.enableCrashReport(this, true);//开启CrashReport收集, 默认是开启状态.
}
}
登录界面的LoginActivity.class的代码
package com.xiaowu.www.logindemo;/**
* Created by min on 2017/2/28.
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.AnalyticsUtils;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.SignUpCallback;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText edit_userName;
private EditText edit_userPassword;
private Button button_login;
private Button button_register;
private Button button_register_login;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.login_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//点击actionbar上的返回键,可以退出
getSupportActionBar().setTitle("登录");
initView();//初始化view
}
private void initView() {
edit_userName = (EditText)findViewById(R.id.user_name);
edit_userPassword = (EditText)findViewById(R.id.user_password);
button_login = (Button)findViewById(R.id.login_button);
button_register = (Button)findViewById(R.id.regester_button);
button_register_login = (Button)findViewById(R.id.register_login_button);
button_login.setOnClickListener(this);
button_register.setOnClickListener(this);
button_register_login.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
AVAnalytics.onResume(this);
}
@Override
protected void onPause(){
super.onPause();
AVAnalytics.onPause(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onClick(View view) {
int id = view.getId();
String name = edit_userName.getText().toString();
String password = edit_userName.getText().toString();
switch (id){
case R.id.login_button:
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
Toast.makeText(this,"用户名或者密码不能为空",Toast.LENGTH_LONG).show();
edit_userName.requestFocus();//焦点放在这里
}else{//后台线程操作,检验登录的合法性
AVUser.logInInBackground(name, password, new LogInCallback<AVUser>() {
@Override
public void done(AVUser avUser, AVException e) {
if(null==e){//没有异常
LoginActivity.this.finish();//结束本activity,进入mainactivity
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this,"用户名已存在或者密码不对",Toast.LENGTH_LONG).show();
}
}
});
}
break;
case R.id.regester_button:
button_register.setVisibility(View.GONE);
button_register.setClickable(false);
button_login.setVisibility(View.GONE);
button_login.setClickable(false);
button_register_login.setVisibility(View.VISIBLE);
button_register_login.setClickable(true);
break;
case R.id.register_login_button:
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
Toast.makeText(this,"用户名或者密码不能为空",Toast.LENGTH_LONG).show();
edit_userName.requestFocus();//焦点放在这里
}else{
AVUser user = new AVUser();//创建用户
user.setUsername(name);
user.setPassword(password);
user.signUpInBackground(new SignUpCallback() {//注册
@Override
public void done(AVException e) {
if(null==e){
Toast.makeText(LoginActivity.this,"该用户名已被注册,请修改用户名",Toast.LENGTH_LONG).show();
edit_userName.requestFocus();
}else{
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);//跳转到mainactivity界面
LoginActivity.this.finish();
}
}
});
}
}
}
}
注意重点标识的地方,看懂了才能继续,下面给出MainActivity的布局,很简单的,只给布局,代码就不给了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到主界面!"
android:layout_gravity="center"
android:paddingLeft="100dp"/>
</LinearLayout>
第三步:权限配置部分,比如能上网,允许读写操作,activity的注册等等
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaowu.www.logindemo">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- 基础模块(必须加入以下声明)START -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>
</manifest>
看看结果怎么样:
效果还可以,看看控制台的效果:
有数据了,成功。后面就是文件的传输了。休息一下。