项目集成
项目使用自动集成,进行一下四步:
-
确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。
buildscript { repositories { jcenter() } ...... } allprojects { repositories { jcenter() } }
-
在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。
android { ...... defaultConfig { applicationId "com.xxx.xxx" //JPush上注册的包名. ...... ndk { //选择要添加的对应cpu类型的.so库。 abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a' // 还可以添加 'x86', 'x86_64', 'mips', 'mips64' } manifestPlaceholders = [ JPUSH_PKGNAME : applicationId, JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可. ] ...... } ...... } dependencies { ...... compile 'cn.jiguang.sdk:jmessage:2.9.0' // 此处以JMessage 2.9.0 版本为例。 compile 'cn.jiguang.sdk:jcore:2.0.0' // 此处以JCore 2.0.0 版本为例。 ...... }
-
在项目中创建一个类并继承JCommonService类,如下:
package com.talk.nonsense.service; import cn.jpush.android.service.JCommonService; public class MyService extends JCommonService { }
-
然后在AndroidManifest.xml文件中application标签下,继续集成:
<!--这部分中的.MyService是第二步创建的类名--> <service android:name=".service.MyService" android:enabled="true" android:exported="false" android:process=":pushcore"> <intent-filter> <action android:name="cn.jiguang.user.service.action" /> </intent-filter> </service>
使用登录和注册功能进行验证
- 新建一个LoginActivity的Activity。
- 修改AndroidManifest.xml文件,将软件启动时显示的页面变为Login,修改如下:
<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" />
- 编写登录注册布局,在activity_login.xml文件中编写
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".LoginActivity"> <EditText android:id="@+id/login_username_edit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="150dp" android:layout_marginEnd="8dp" android:ems="10" android:hint="@string/login_username_text" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/login_password_edit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="80dp" android:layout_marginEnd="8dp" android:hint="@string/login_password_text" android:inputType="textPassword" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/login_username_edit" /> <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="80dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/login_password_edit"> <Button android:id="@+id/login_login_btn" android:text="@string/login_login_text" android:layout_width="197dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginEnd="4dp" android:onClick="loginLogin"/> <Button android:id="@+id/login_register_btn" android:text="@string/login_register_text" android:layout_width="197dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_marginEnd="8dp" android:layout_marginStart="4dp" android:onClick="loginRegister"/> </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
- 编写LoginActivity类如下:
package com.talk.nonsense; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import cn.jpush.im.android.api.JMessageClient; import cn.jpush.im.api.BasicCallback; public class LoginActivity extends AppCompatActivity { private EditText username; private EditText password; private LoginActivity loginActivity = this; // public static List<Activity> activities = new ArrayList<>(); // private boolean isLogin = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // activities.add(this); // IM的SDK初始化 JMessageClient.init(this); username = findViewById(R.id.login_username_edit); password = findViewById(R.id.login_password_edit); } // 登录点击事件 public void loginLogin(View view) { if(username == null || password == null) { Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show(); return; } Log.i("Login:", "---------------not null"); String nameStr = username.getText().toString(); String pwdStr = password.getText().toString(); if (nameStr == null || "".equals(nameStr.trim()) || pwdStr == null || "".equals(pwdStr.trim())) { Toast.makeText(this, "输入错误!", Toast.LENGTH_SHORT).show(); return; } Log.i("Login:", "---------------not space"); //用户名以字母或者数字开头。支持字母、数字、下划线、英文点、减号、 @。长度为Byte(4~128) //密码长度Byte(4~128) if (!nameStr.matches("([0-9a-z]+)([0-9a-z_\\.]|[@\\-])*") || nameStr.getBytes().length <= 4 || nameStr.getBytes().length >= 128 || pwdStr.getBytes().length <= 4 || pwdStr.getBytes().length >= 128) { Toast.makeText(this, "用户名或密码不符合规范!", Toast.LENGTH_SHORT).show(); return; } Log.i("Login:", "---------------OK"); JMessageClient.login(nameStr, pwdStr, new BasicCallback() { @Override public void gotResult(int i, String s) { Log.i("Login:", "------------" + s); if( i == 0) { //isLogin = true; startActivity(new Intent(loginActivity, MyMainActivity.class)); Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "登录失败!", Toast.LENGTH_SHORT).show(); return; } } }); } // 注册 点击事件 public void loginRegister(View view) { if(username == null || password == null) { Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show(); return; } String nameStr = username.getText().toString(); String pwdStr = password.getText().toString(); if (nameStr == null || "".equals(nameStr.trim()) || pwdStr == null || "".equals(pwdStr.trim())) { Toast.makeText(this, "输入错误1", Toast.LENGTH_SHORT).show(); return; } //用户名以字母或者数字开头。支持字母、数字、下划线、英文点、减号、 @。长度为Byte(4~128) //密码长度Byte(4~128) if (!nameStr.matches("([0-9a-z]+)([0-9a-z_\\.]|[@\\-])*") || nameStr.getBytes().length <= 4 || nameStr.getBytes().length >= 128 || pwdStr.getBytes().length <= 4 || pwdStr.getBytes().length >= 128) { Toast.makeText(this, "用户名或密码不符合规范!", Toast.LENGTH_SHORT).show(); return; } // 注册账号 JMessageClient.register(nameStr, pwdStr, new BasicCallback() { @Override public void gotResult(int i, String s) { Log.i("Register:", "------------" + i); if (i == 0) { //isLogin = true; //startActivity(new Intent(loginActivity, MainActivity.class)); Toast.makeText(getApplicationContext(), "注册成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "注册失败!", Toast.LENGTH_SHORT).show(); return; } } }); } }
- 最后运行程序,先注册,在登录,如果成功,则JMessage集成成功。
JMessage中的登录和注册
要使用JMessage要先调用JMessageClient.init();
方法,进行初始化。
注册
调用方法:
JMessageClient.register(nameStr, pwdStr, new BasicCallback() {
@Override
public void gotResult(int i, String s) {
Log.i("Register:", "------------" + i);
if (i == 0) {
//注册成功,执行该方法体
} else {
//注册失败,执行该方法体
}
}
});
在JMessageClient.register(String var0, String var1, BasicCallback var2)
中,var0为用户名,username
var1为密码,password
var2用于回调
登录
JMessageClient.login(nameStr, pwdStr, new BasicCallback() {
@Override
public void gotResult(int i, String s) {
if( i == 0) {
//登录成功,执行该方法体
} else {
//登录失败,执行该方法体
}
}
});
同样,上述代码中nameStr为用户名(username),pwdStr为密码(password)。