-
Notifications
You must be signed in to change notification settings - Fork 0
8주차 과제 PR #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
8주차 과제 PR #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ android { | |
| kotlinOptions { | ||
| jvmTarget = "1.8" | ||
| } | ||
| viewBinding { | ||
| enable = true | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,20 +1,87 @@ | ||||||
| package com.example.assignment | ||||||
|
|
||||||
| import android.Manifest | ||||||
| import android.app.NotificationManager | ||||||
| import android.content.Context | ||||||
| import android.os.Bundle | ||||||
| import androidx.activity.enableEdgeToEdge | ||||||
| import android.widget.Toast | ||||||
| import androidx.activity.result.ActivityResultLauncher | ||||||
| import androidx.activity.result.contract.ActivityResultContracts | ||||||
| import androidx.appcompat.app.AlertDialog | ||||||
| import androidx.appcompat.app.AppCompatActivity | ||||||
| import androidx.core.view.ViewCompat | ||||||
| import androidx.core.view.WindowInsetsCompat | ||||||
| import com.example.assignment.databinding.ActivityMainBinding | ||||||
|
|
||||||
| class MainActivity : AppCompatActivity() { | ||||||
| private lateinit var binding: ActivityMainBinding | ||||||
| private lateinit var notificationHelper: NotificationHelper | ||||||
| private var count: Int = 0 | ||||||
|
|
||||||
| private val postResultLauncher: ActivityResultLauncher<String> = | ||||||
| registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> | ||||||
| if (isGranted) | ||||||
| Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show() | ||||||
| else | ||||||
| Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show() | ||||||
| } | ||||||
|
|
||||||
| override fun onCreate(savedInstanceState: Bundle?) { | ||||||
| super.onCreate(savedInstanceState) | ||||||
| enableEdgeToEdge() | ||||||
| setContentView(R.layout.activity_main) | ||||||
| ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||||||
| val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||||||
| v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||||||
| insets | ||||||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||||||
| notificationHelper = NotificationHelper(this) | ||||||
| setContentView(binding.root) | ||||||
|
|
||||||
| if (intent.getBooleanExtra("closeNotification", false)) { | ||||||
| val activityData = getSharedPreferences("activityData", Context.MODE_PRIVATE) | ||||||
| count = activityData.getInt("count", 0) | ||||||
|
|
||||||
| val notificationManager = | ||||||
| getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||||||
| notificationManager.cancel(NotificationHelper.NOTIFICATION_ID) | ||||||
| } else { | ||||||
| count = 0 | ||||||
| } | ||||||
| binding.countText.text = count.toString() | ||||||
|
|
||||||
| if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { | ||||||
| showRationaleDialog( | ||||||
| "Permission Demo requires Notification Permission", | ||||||
| "Notification Permission is denied" | ||||||
| ) | ||||||
| } else { | ||||||
| postResultLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) | ||||||
| } | ||||||
|
|
||||||
| val btnShowToast = binding.toastButton | ||||||
| btnShowToast.setOnClickListener() { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Toast.makeText(this, "toast message", Toast.LENGTH_SHORT).show() | ||||||
| } | ||||||
|
|
||||||
| val btnIncrementCount = binding.countButton | ||||||
| val countText = binding.countText | ||||||
| btnIncrementCount.setOnClickListener() { | ||||||
| count++ | ||||||
| countText.text = count.toString() | ||||||
| } | ||||||
|
|
||||||
| val randomData = intent.getIntExtra("randomData", 0) | ||||||
| if (randomData != 0) { | ||||||
| count = randomData | ||||||
| countText.text = count.toString() | ||||||
| } | ||||||
|
|
||||||
| val btnNavigateToRandom = binding.randomButton | ||||||
| btnNavigateToRandom.setOnClickListener() { | ||||||
| notificationHelper.showNotification(count) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private fun showRationaleDialog(title: String, message: String) { | ||||||
| val builder: AlertDialog.Builder = AlertDialog.Builder(this) | ||||||
| builder.setTitle(title) | ||||||
| .setMessage(message) | ||||||
| .setPositiveButton("Cancel") { dialog, _ -> | ||||||
| dialog.dismiss() | ||||||
| } | ||||||
| builder.create().show() | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.example.assignment | ||
|
|
||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
| import android.app.PendingIntent | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.Build | ||
| import androidx.core.app.NotificationCompat | ||
|
|
||
| class NotificationHelper(private val context: Context) { | ||
| companion object { | ||
| private const val CHANNEL_ID = "random_channel_id" | ||
| private const val CHANNEL_NAME = "Random Channel" | ||
| private const val CHANNEL_DESCRIPTION = "Generate and send random numbers" | ||
| const val NOTIFICATION_ID = 1 | ||
| } | ||
|
|
||
| init { | ||
| createNotificationChannel() | ||
| } | ||
|
|
||
| private fun createNotificationChannel() { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| val name = CHANNEL_NAME | ||
| val descriptionText = CHANNEL_DESCRIPTION | ||
| val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
| val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { | ||
| description = descriptionText | ||
| } | ||
| val notificationManager: NotificationManager = | ||
| context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
| notificationManager.createNotificationChannel(channel) | ||
| } | ||
| } | ||
|
|
||
| fun showNotification(count: Int) { | ||
| val activityData = context.getSharedPreferences("activityData", Context.MODE_PRIVATE) | ||
| activityData.edit().putInt("count", count).apply() | ||
|
|
||
| val randomIntent = Intent(context, RandomActivity::class.java).apply { | ||
| putExtra("countData", count) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. countData를 상수로 정의해서 사용하면 더 좋을 것 같습니당 |
||
| } | ||
| val randomPendingIntent: PendingIntent = | ||
| PendingIntent.getActivity( | ||
| context, | ||
| 0, | ||
| randomIntent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | ||
| ) | ||
|
|
||
| val closeIntent = Intent(context, MainActivity::class.java).apply { | ||
| flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP | ||
| putExtra("closeNotification", true) | ||
| } | ||
| val closePendingIntent: PendingIntent = | ||
| PendingIntent.getActivity( | ||
| context, | ||
| 1, | ||
| closeIntent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | ||
| ) | ||
|
|
||
| val builder = NotificationCompat.Builder(context, CHANNEL_ID) | ||
| .setSmallIcon(R.drawable.ic_launcher_foreground) | ||
| .setContentTitle(context.getString(R.string.notification_title)) | ||
| .setContentText(context.getString((R.string.notification_content))) | ||
| .setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
| .setContentIntent(randomPendingIntent) | ||
| .setAutoCancel(true) | ||
| .addAction( | ||
| R.drawable.ic_launcher_foreground, | ||
| context.getString(R.string.btn_more), | ||
| randomPendingIntent | ||
| ) | ||
| .addAction( | ||
| R.drawable.ic_launcher_foreground, | ||
| context.getString(R.string.btn_close), | ||
| closePendingIntent | ||
| ) | ||
|
|
||
| val notificationManager = | ||
| context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
| notificationManager.notify(NOTIFICATION_ID, builder.build()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.example.assignment | ||
|
|
||
| import android.app.NotificationManager | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import com.example.assignment.databinding.ActivityRandomBinding | ||
|
|
||
| class RandomActivity : AppCompatActivity() { | ||
| private lateinit var binding: ActivityRandomBinding | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityRandomBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
| notificationManager.cancel(NotificationHelper.NOTIFICATION_ID) | ||
|
|
||
| val receivedCount = intent.getIntExtra("countData", 0) | ||
| val randomNumberTextView = binding.countTextRandomView | ||
| val randomNum = (0..receivedCount).random() | ||
| randomNumberTextView.text = randomNum.toString() | ||
|
|
||
| val btnBack = binding.backButtonRandomView | ||
| btnBack.setOnClickListener() { | ||
| val intent = Intent(this, MainActivity::class.java).apply { | ||
| putExtra("randomData", randomNum) | ||
| flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
| } | ||
| startActivity(intent) | ||
| finish() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:shape="rectangle"> | ||
| <solid android:color="@color/main_button_color" /> | ||
| <corners android:radius="10dp" /> | ||
| </shape> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?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" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:background="@color/main_background_color"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/count_text_random_view" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="0" | ||
| android:textColor="@color/text_color" | ||
| android:textSize="100sp" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| app:layout_constraintVertical_bias="0.3" /> | ||
|
|
||
| <android.widget.Button | ||
| android:id="@+id/back_button_random_view" | ||
| style="@style/custom_button_style" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="BACK" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@id/count_text_random_view" /> | ||
|
|
||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
권한이 거부되었을 때 사용되는 메소드가 따로 있습니다! 나중에 사용해보세용