[Huawei Developers] HMS Community:
Introduction

In this article, we can learn about the basic questions on different country flags of Quiz application. Also, we can learn to integrate Huawei Crash service of AppGallery Connect in Quiz application.

If you are new to this application, follow my previous articles.

https://forums.developer.huawei.com/forumPortal/en/topic/0202877278014350004

https://forums.developer.huawei.com/forumPortal/en/topic/0201884103719030016?fid=0101187876626530001

https://forums.developer.huawei.com/forumPortal/en/topic/0202890333711770040

https://forums.developer.huawei.com/forumPortal/en/topic/0201892776213940037

What is Crash service?

The AppGallery Connect Crash service provides a powerful lightweight solution to app crash problems. With the service, you can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real-time, without the need to write any code.

Huawei Crash is a real-time crash reporting tool that helps in tracking, prioritizing, and fixing stability issues that compromise the quality of your app.

To ensure stable running of your app and prevent user experience deterioration caused by crashes, you are advised to monitor the running status of your app on each device, which is the key. The Crash service provides real-time reports, revealing any crash of your app on any device. In addition, the Crash service can intelligently aggregate crashes, providing context data when a crash occurs, such as environment information and stack, for you to prioritize the crash easily for rapid resolution.

Requirements

1. Any operating system (MacOS, Linux, and Windows).

2. Must have a Huawei phone with HMS 4.0.0.300 or later.

3. Must have a laptop or desktop with Android Studio, JDK 1.8, SDK platform 26, and Gradle 4.6 and above installed.

4. Minimum API Level 24 is required.

5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification on the Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. On the right-upper corner of the android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Note: Project Name depends on the user created name.

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

7. Enter SHA-256 certificate fingerprint and click Save button, as follows.

Note: Above steps from Step 1 to 7 is common for all Huawei Kits.

8. Click Manage APIs tab and enable HUAWEI Analytics.

9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'

10. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Analytics Kit
implementation 'com.huawei.hms:hianalytics:6.4.0.300'
// Huawei Crash Service
implementation 'com.huawei.agconnect:agconnect-crash:1.6.0.300'

11. Now Sync the gradle.

12. Add the required permission to the AndroidManifest.xml file.

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

Configuration in AppGallery Connect

Find the Crash using AppGallery connect dashboard.

Choose My Projects > Quality > Crash, and click Enable now.

Let us move to development

I have created a project on Android studio with empty activity let us start coding.

1. Initialize an AGConnectCrash before other methods and Enable crash.

AGConnectCrash.getInstance().enableCrashCollection(true)

2. Test the below method in your code.

AGConnectCrash.getInstance().testIt(this@Home)

3. Customizing User IDs: Analyzing crashes by a user to resolve crashes using User ID. The user can be uniquely identified after a crash occurs.

AGConnectCrash.getInstance().setUserId("123456789")

4. Recording Custom Logs: we can record custom logs, Recorded logs will be reported together with the crash data.

AGConnectCrash.getInstance().log("catch exception")

5. Adding a Custom key-value pair: we can add a custom key-value pair to record the status. The recorded status will be reported together with the crash data.

AGConnectCrash.getInstance().setCustomKey("keyInt", 1)

In the Home.kt we can find the business logic for crash service.

class Home : AppCompatActivity(), HomeAdapter.ItemListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

// Crash Service
AGConnectCrash.getInstance().enableCrashCollection(true)
// Record Custom Status
AGConnectCrash.getInstance().setCustomKey("keyInt", 1)
// Set the user ID
AGConnectCrash.getInstance().setUserId("123456789")
try {
throw NullPointerException()
} catch (ex: NullPointerException) {
AGConnectCrash.getInstance().log("catch exception")
// Record non-fatal exception
AGConnectCrash.getInstance().recordException(ex)
}

}

override fun onItemClick(item: Int) {
when(item ) {
0 -> {val intent = Intent(this@Home, AndroidActivity::class.java)
startActivity(intent)
}
1 -> {val intent = Intent(this@Home, HMSActivity::class.java)
startActivity(intent)
}
2 -> {val intent = Intent(this@Home, SportsActivity::class.java)
startActivity(intent)
}
3 -> {val intent = Intent(this@Home, QuizActivity::class.java)
startActivity(intent)
// Test
AGConnectCrash.getInstance().testIt(this@Home)
}
}
}

}

In the QuizActivity.kt we can find the business logic for country flags questions.

class QuizActivity : AppCompatActivity(), View.OnClickListener {

private var nCurrentPosition: Int = 1
private var nQuestionsList: ArrayList<Question>? = null
private var nSelectedOptionPosition: Int = 0
private var nCorrectAnswers: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)

nQuestionsList = Constants.getQuestions()
setQuestion()

txt_option_one.setOnClickListener(this)
txt_option_two.setOnClickListener(this)
txt_option_three.setOnClickListener(this)
txt_option_four.setOnClickListener(this)
btn_submit.setOnClickListener(this)

}

private fun setQuestion() {
val question = nQuestionsList!![nCurrentPosition -1]
defaultOptionsView()
if(nCurrentPosition == nQuestionsList!!.size){
btn_submit.text = "FINISH"
} else {
btn_submit.text = "SUBMIT"
}
progressBar.progress = nCurrentPosition
txt_progress.text = "$nCurrentPosition" + "/" + progressBar.max
txt_question.text = question!!.question
iv_image.setImageResource(question.image)
txt_option_one.text = question.optionOne
txt_option_two.text = question.optionTwo
txt_option_three.text = question.optionThree
txt_option_four.text = question.optionFour
}

private fun defaultOptionsView() {
val options = ArrayList<TextView>()
options.add(0, txt_option_one)
options.add(1, txt_option_two)
options.add(2, txt_option_three)
options.add(3, txt_option_four)
for(option in options){
option.setTextColor(Color.parseColor("#7A8089"))
option.typeface = Typeface.DEFAULT
option.background = ContextCompat.getDrawable(this, R.drawable.border_bg)
}
}

override fun onClick(v: View?) {
when(v?.id){
R.id.txt_option_one -> {
selectedOptionView(txt_option_one, 1)
}
R.id.txt_option_two -> {
selectedOptionView(txt_option_two, 2)
}
R.id.txt_option_three -> {
selectedOptionView(txt_option_three, 3)
}
R.id.txt_option_four -> {
selectedOptionView(txt_option_four, 4)
}
R.id.btn_submit -> {
if(nSelectedOptionPosition == 0){
nCurrentPosition++
when{
nCurrentPosition <= nQuestionsList!!.size -> {
setQuestion()
} else -> {
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra(Constants.CORRECT_ANSWERS, nCorrectAnswers)
intent.putExtra(Constants.TOTAL_QUESTIONS, nQuestionsList!!.size)
startActivity(intent)
}
}
} else {
val question = nQuestionsList?.get(nCurrentPosition -1)
if(question!!.correctAnswer != nSelectedOptionPosition){
answerView(nSelectedOptionPosition, R.drawable.wrong_bg)
} else {
nCorrectAnswers ++
}
answerView(question!!.correctAnswer, R.drawable.correct_bg)
if(nCurrentPosition == nQuestionsList!!.size){
btn_submit.text = "FINISH"
} else {
btn_submit.text = "Next Question"
}
nSelectedOptionPosition = 0
}
}
}
}

private fun answerView(answer:Int, drawableView:Int){
when(answer) {
1 -> {
txt_option_one.background = ContextCompat.getDrawable(this, drawableView)
}
2 -> {
txt_option_two.background = ContextCompat.getDrawable(this, drawableView)
}
3 -> {
txt_option_three.background = ContextCompat.getDrawable(this, drawableView)
}
4 -> {
txt_option_four.background = ContextCompat.getDrawable(this, drawableView)
}
}
}

private fun selectedOptionView(tv: TextView, selectedOptionNum: Int){
defaultOptionsView()
nSelectedOptionPosition = selectedOptionNum
tv.setTextColor(Color.parseColor("#363A43"))
tv.setTypeface(tv.typeface, Typeface.BOLD)
tv.background = ContextCompat.getDrawable(this, R.drawable.select_bg)
}

}

In the ResultActivity.kt we can find the business logic for country flags result view.

class ResultActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)

val totalQuestions = intent.getIntExtra(Constants.TOTAL_QUESTIONS, 0)
val correctAnswers = intent.getIntExtra(Constants.CORRECT_ANSWERS, 0)
txt_score.text = "Your Score is $correctAnswers out of $totalQuestions"

btn_finish.setOnClickListener {
startActivity(Intent(this, Home::class.java))
}

}

}

Create a data class Question.kt to access variables.

data class Question(
val id:Int,
val question:String,
val image:Int,
val optionOne:String,
val optionTwo:String,
val optionThree:String,
val optionFour:String,
val correctAnswer:Int
)

Create object class Constants.kt to show questions.

object Constants {

const val TOTAL_QUESTIONS: String = "total_question"
const val CORRECT_ANSWERS: String = "correct_answers"

fun getQuestions():ArrayList<Question>{
val questionsList = ArrayList<Question>()

// 1
val que1 = Question(1, "What Country does this flag belong to?",
R.drawable.angola_flag, "Zambia",
"Botswana", "Angola", "Namibia", 3)
questionsList.add(que1)

// 2
val que2 = Question(2, "What Country does this flag belong to?",
R.drawable.barmuda_flag, "Bermuda",
"Havana", "Merida", "Miami", 1)
questionsList.add(que2)

// 3
val que3 = Question(3, "What Country does this flag belong to?",
R.drawable.bolivia_flag, "Brazil",
"Bolivia", "Paraguay", "Uruguay", 2)
questionsList.add(que3)

// 4
val que4 = Question(4, "What Country does this flag belong to?",
R.drawable.cambodia_flag, "Vietnam",
"Laos", "Cambodia", "Jakarta", 3)
questionsList.add(que4)

// 5
val que5 = Question(5, "What Country does this flag belong to?",
R.drawable.chile_flag, "Peru",
"Panama", "Honduras", "Chile", 4)
questionsList.add(que5)

// 6
val que6 = Question(6, "What Country does this flag belong to?",
R.drawable.cuba_flag, "Guyana",
"Cuba", "Ecuador", "Guatemala", 2)
questionsList.add(que6)

// 7
val que7 = Question(7, "What Country does this flag belong to?",
R.drawable.honduras_flag, "Honduras",
"Costa Rica", "Puerto Rico", "Dominican Republic", 1)
questionsList.add(que7)

// 8
val que8 = Question(8, "What Country does this flag belong to?",
R.drawable.mali_flag, "Algeria",
"Libya", "Mali", "Tunisia", 3)
questionsList.add(que8)

// 9
val que9 = Question(9, "What Country does this flag belong to?",
R.drawable.norway_flag, "Sweden",
"Poland", "Austria", "Norway", 4)
questionsList.add(que9)

// 10
val que10 = Question(10, "What Country does this flag belong to?",
R.drawable.suriname_flag, "Barbados",
"Haiti", "Suriname", "Belize", 3)
questionsList.add(que10)
return questionsList

}

}

In the activity_quiz.xml we can create the UI screen for questions.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
tools:context=".flags.QuizActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center" >

<TextView
android:id="@+id/txt_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="What Country does this flag belong to?"
android:textColor="#363A43"
android:textSize="18sp">
</TextView>

<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginTop="16dp"
android:src="@drawable/android_image"/>

<LinearLayout
android:id="@+id/progress_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="16dp" >

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:minHeight="50dp"
android:progress="0"
android:indeterminate="false"
android:max="10">
</ProgressBar>

<TextView
android:id="@+id/txt_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:gravity="center"
android:textColorHint="#7A8089"
android:textSize="14sp"
tools:text="0/10">
</TextView>

</LinearLayout>

<TextView
android:id="@+id/txt_option_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border_bg"
android:padding="15dp"
android:gravity="center"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="India">
</TextView>

<TextView
android:id="@+id/txt_option_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border_bg"
android:padding="15dp"
android:gravity="center"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="Sri Lanka">
</TextView>

<TextView
android:id="@+id/txt_option_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border_bg"
android:padding="15dp"
android:gravity="center"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="Burma">
</TextView>

<TextView
android:id="@+id/txt_option_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border_bg"
android:padding="15dp"
android:gravity="center"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="USA">
</TextView>

<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/design_default_color_primary"
android:text="Submit"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>

</ScrollView>

In the activity_result.xml we can create the UI screen for result.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:background="@drawable/result_bg"
android:padding="20dp"
tools:context=".flags.ResultActivity">

<TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:text="Result"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="22sp">
</TextView>

<ImageView
android:id="@+id/img_trophy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@drawable/trophy"/>

<TextView
android:id="@+id/txt_congratulations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Hi, Congratulations!!"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="25sp">
</TextView>

<TextView
android:id="@+id/txt_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Your Score is 9 out of 10"
android:textColor="@android:color/secondary_text_dark"
android:textSize="20sp">
</TextView>

<Button
android:id="@+id/btn_finish"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@android:color/white"
android:text="Finish"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />

</LinearLayout>

Demo

Crash Service

Tips and Tricks

1. Make sure you are already registered as Huawei developer.

2. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.

3. Make sure you have added the agconnect-services.json file to app folder.

4. Make sure you have added SHA-256 fingerprint without fail.

5. Make sure all the dependencies are added properly.

Conclusion

In this article, we have learned about the basic questions on different country flags of Quiz application. Also, we have learned to integrate Huawei Crash service of AppGallery Connect in Quiz application. Crash service provides a powerful lightweight solution to app crash problems. With the service, you can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time, without the need to write any code.

I hope you have read this article. If you found it helpful, please provide likes and comments.

Reference

Crash Service – Documentation

Crash Service – Training Video