首页 > 分享 > 1+x移动应用开发(中级) 账号服务集成 推送服务 定位服务 机器学习服务(文本识别)

1+x移动应用开发(中级) 账号服务集成 推送服务 定位服务 机器学习服务(文本识别)

项目级环境搭建

buildscript {

repositories {

google()

jcenter()

maven {url 'https://developer.huawei.com/repo/'}

}

dependencies {

classpath "com.android.tools.build:gradle:4.1.1"

classpath 'com.huawei.agconnect:agcp:1.9.0.300'

}

}

allprojects {

repositories {

google()

jcenter()

maven {url 'https://developer.huawei.com/repo/'}

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

模块级环境搭建

plugins {

id 'com.android.application'

id 'com.huawei.agconnect'

}

android {

compileSdkVersion 33

buildToolsVersion "30.0.2"

defaultConfig {

applicationId "com.st.hms_051301_sh"

minSdkVersion 19

targetSdkVersion 33

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

signingConfigs{

release{

storeFile file("hms_051301_sh.jks")

keyAlias "key0"

keyPassword "123456"

storePassword "123456"

v1SigningEnabled true

v2SigningEnabled true

}

}

buildTypes {

release {

minifyEnabled false

signingConfig signingConfigs.release

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

debug{

signingConfig signingConfigs.release

debuggable true

}

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_8

targetCompatibility JavaVersion.VERSION_1_8

}

}

dependencies {

implementation 'com.huawei.hms:ml-computer-vision-ocr:3.11.0.301'

implementation 'com.huawei.hms:ml-computer-vision-ocr-cn-model:3.11.0.301'

implementation 'com.huawei.hms:location:6.4.0.300'

implementation 'com.huawei.hms:push:6.9.0.300'

implementation 'com.huawei.hms:hwid:6.9.0.301'

implementation 'com.huawei.hms:hmscoreinstaller:6.6.0.300'

implementation 'com.huawei.agconnect:agconnect-core:1.9.0.300'

implementation 'androidx.appcompat:appcompat:1.1.0'

implementation 'com.google.android.material:material:1.1.0'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

testImplementation 'junit:junit:4.+'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

在华为开发者联盟创建项目并将agconnect-services.json文件放入app目录下

 

然后在Build选项中配置指纹签名放入app目录下并在绑定项目

AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

package="com.st.hms_051301_sh">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

tools:replace="android:allowBackup"

android:theme="@style/Theme.Hms_051301_sh">

<service android:name=".MyHmsMessageService" android:exported="false"

tools:ignore="Instantiatable">

<intent-filter>

<action android:name="com.huawei.push.action.MESSAGING_EVENT"/>

</intent-filter>

</service>

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

activity_main.xml布局代码

<?xml version="1.0" encoding="utf-8"?>

<androidx.appcompat.widget.LinearLayoutCompat 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"

android:orientation="vertical"

tools:context=".MainActivity">

<Button

android:id="@+id/btn_authcode"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号验证登陆" />

<Button

android:id="@+id/btn_idtoken"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="idtoken登陆" />

<Button

android:id="@+id/btn_silent"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="静默登陆" />

<Button

android:id="@+id/btn_signout"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="退出" />

<Button

android:id="@+id/btn_cancelauth"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="取消授权" />

<Button

android:id="@+id/btn_requestLocation"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="开启定位" />

<Button

android:id="@+id/btn_removeLocation"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="移除定位" />

<Button

android:id="@+id/btn_localText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="文本识别" />

<ImageView

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:srcCompat="@drawable/ic_launcher_background"

tools:ignore="VectorDrawableCompat" />

</androidx.appcompat.widget.LinearLayoutCompat>

MainActivity代码

import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.os.Looper;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import com.huawei.hmf.tasks.OnCompleteListener;

import com.huawei.hmf.tasks.Task;

import com.huawei.hms.location.FusedLocationProviderClient;

import com.huawei.hms.location.LocationCallback;

import com.huawei.hms.location.LocationRequest;

import com.huawei.hms.location.LocationResult;

import com.huawei.hms.location.LocationServices;

import com.huawei.hms.mlsdk.MLAnalyzerFactory;

import com.huawei.hms.mlsdk.common.MLFrame;

import com.huawei.hms.mlsdk.text.MLLocalTextSetting;

import com.huawei.hms.mlsdk.text.MLText;

import com.huawei.hms.mlsdk.text.MLTextAnalyzer;

import com.huawei.hms.support.account.AccountAuthManager;

import com.huawei.hms.support.account.request.AccountAuthParams;

import com.huawei.hms.support.account.request.AccountAuthParamsHelper;

import com.huawei.hms.support.account.result.AuthAccount;

import com.huawei.hms.support.account.service.AccountAuthService;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button btnAuthcode;

private Button btnIdtoken;

private Button btnSilent;

private Button btnSignout;

private Button btnCancelauth;

private Button btnRequestLocation;

private Button btnRemoveLocation;

private Button btnLocalText;

private ImageView imageView;

private AccountAuthService service;

private LocationCallback locationCallback;

private FusedLocationProviderClient fusedLocationProviderClient;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Context context = MainActivity.this;

MyMessageService.getToken(context);

initView();

}

private void initView() {

btnAuthcode = findViewById(R.id.btn_authcode);

btnIdtoken = findViewById(R.id.btn_idtoken);

btnSilent = findViewById(R.id.btn_silent);

btnSignout = findViewById(R.id.btn_signout);

btnCancelauth = findViewById(R.id.btn_cancelauth);

btnRequestLocation = findViewById(R.id.btn_requestLocation);

btnRemoveLocation = findViewById(R.id.btn_removeLocation);

btnLocalText = findViewById(R.id.btn_localText);

imageView = findViewById(R.id.imageView);

btnAuthcode.setOnClickListener(this);

btnIdtoken.setOnClickListener(this);

btnSilent.setOnClickListener(this);

btnSignout.setOnClickListener(this);

btnCancelauth.setOnClickListener(this);

btnRequestLocation.setOnClickListener(this);

btnRemoveLocation.setOnClickListener(this);

btnLocalText.setOnClickListener(this);

}

@Override

public void onClick(View view) {

switch (view.getId()){

case R.id.btn_authcode:

authcodeSignIn();

break;

case R.id.btn_idtoken:

idTokenSignIn();

break;

case R.id.btn_silent:

SilentSignIn();

break;

case R.id.btn_signout:

authSignOut();

break;

case R.id.btn_cancelauth:

cancelAuth();

break;

case R.id.btn_requestLocation:

requestLocation();

break;

case R.id.btn_removeLocation:

removeLocation();

break;

case R.id.btn_localText:

analyseText();

break;

}

}

private void authcodeSignIn() {

AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();

service = AccountAuthManager.getService(this, params);

startActivityForResult(service.getSignInIntent(),7777);

}

private void idTokenSignIn(){

AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setIdToken().createParams();

service = AccountAuthManager.getService(this, params);

startActivityForResult(service.getSignInIntent(),8888);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if(requestCode==7777){

Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);

task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {

@Override

public void onComplete(Task<AuthAccount> task) {

if (task.isSuccessful()) {

System.out.println("===="+task.getResult().toString());

}else{

System.out.println("===="+task.getException().toString());

}

}

});

}

if (requestCode==8888){

Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);

task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {

@Override

public void onComplete(Task<AuthAccount> task) {

if (task.isSuccessful()) {

System.out.println("===="+task.getResult().toString());

}else{

System.out.println("===="+task.getException().toString());

}

}

});

}

}

private void SilentSignIn(){

AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();

service = AccountAuthManager.getService(this, params);

Task<AuthAccount> task = service.silentSignIn();

task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {

@Override

public void onComplete(Task<AuthAccount> task) {

if (task.isSuccessful()) {

System.out.println("===="+task.getResult().toString());

}else{

System.out.println("===="+task.getException().toString());

}

}

});

}

private void authSignOut(){

Task<Void> task = service.signOut();

task.addOnCompleteListener(new OnCompleteListener<Void>() {

@Override

public void onComplete(Task<Void> task) {

if (task.isSuccessful()) {

System.out.println("====Sign out successful");

}else{

System.out.println("====Sign out failure");

}

}

});

}

private void cancelAuth(){

Task<Void> task = service.cancelAuthorization();

task.addOnCompleteListener(new OnCompleteListener<Void>() {

@Override

public void onComplete(Task<Void> task) {

if (task.isSuccessful()) {

System.out.println("==== cancelAuth successful");

}else{

System.out.println("==== cancelAuth failure");

}

}

});

}

private void requestLocation(){

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);

LocationRequest locationRequest = new LocationRequest()

.setInterval(3000)

.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

locationCallback = new LocationCallback(){

@Override

public void onLocationResult(LocationResult locationResult) {

System.out.println("====location "+locationResult.getLocations().toString());

}

};

fusedLocationProviderClient.requestLocationUpdates(locationRequest ,locationCallback, Looper.myLooper())

.addOnCompleteListener(new OnCompleteListener<Void>() {

@Override

public void onComplete(Task<Void> task) {

if (task.isSuccessful()) {

System.out.println("====request location successful ");

}else{

System.out.println("====request location failure");

}

}

});

}

private void removeLocation(){

Task<Void> task = fusedLocationProviderClient.removeLocationUpdates(locationCallback);

task.addOnCompleteListener(new OnCompleteListener<Void>() {

@Override

public void onComplete(Task<Void> task) {

if (task.isSuccessful()) {

System.out.println("====remove location successful");

}else{

System.out.println("====remove location failure");

}

}

});

}

private void analyseText() {

MLLocalTextSetting mlLocalTextSetting = new MLLocalTextSetting.Factory()

.setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)

.setLanguage("zh")

.create();

MLTextAnalyzer localTextAnalyzer = MLAnalyzerFactory

.getInstance()

.getLocalTextAnalyzer(mlLocalTextSetting);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.text);

imageView.setImageBitmap(bitmap);

MLFrame mlFrame = MLFrame.fromBitmap(bitmap);

Task<MLText> mlTextTask = localTextAnalyzer.asyncAnalyseFrame(mlFrame);

mlTextTask.addOnCompleteListener(new OnCompleteListener<MLText>() {

@Override

public void onComplete(Task<MLText> task) {

if (task.isSuccessful()) {

System.out.println("==== successful" + task.getResult().getStringValue());

} else {

System.out.println("==== failure" + task.getException().toString());

}

}

});

try {

localTextAnalyzer.stop();

} catch (IOException e) {

e.printStackTrace();

}

}

}

MyHmsMessageService页面代码

import android.content.Context;

import com.huawei.agconnect.config.AGConnectServicesConfig;

import com.huawei.hms.aaid.HmsInstanceId;

import com.huawei.hms.common.ApiException;

import com.huawei.hms.push.HmsMessageService;

public class MyHmsMessageService {

public static void getToken(Context context){

new Thread(){

@Override

public void run() {

try {

String app_id= "108280109";

String tokenScope="HCM";

String token = HmsInstanceId.getInstance(context).getToken(app_id, tokenScope);

System.out.println("=====token"+token);

} catch (ApiException e) {

e.printStackTrace();

}

}

}.start();

}

}

最终在模拟器上的效果

相关知识

精准气象服务与农业应用.docx
电商平台移动端应用开发及维护服务合同.doc
微信小程序之植物识别demo(百度开发接口)
退役军人信息化服务平台订花开发服务流程
Springboot+Vue家政服务管理平台
深度学习农作物病虫害智能识别APP开发教程
餐饮服务与数字化运营
花店服务设计
20行Python代码开发植物识别 app
以开发之名

网址: 1+x移动应用开发(中级) 账号服务集成 推送服务 定位服务 机器学习服务(文本识别) https://m.huajiangbk.com/newsview546016.html

所属分类:花卉
上一篇: 安卓移动开发实验二:Androi
下一篇: 移动Web实训DAY