增加通知
This commit is contained in:
parent
1f5dfeaf23
commit
4f4cac52dc
7 changed files with 86 additions and 21 deletions
|
@ -2,24 +2,8 @@
|
|||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<value>
|
||||
<entry key="MainActivity">
|
||||
<State />
|
||||
</entry>
|
||||
<entry key="app">
|
||||
<State>
|
||||
<targetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="QUICK_BOOT_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="VIRTUAL_DEVICE_PATH" />
|
||||
<value value="$USER_HOME$/.android/avd/Resizable_Experimental_API_34.avd" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</targetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2024-03-14T04:14:17.825894651Z" />
|
||||
</State>
|
||||
<State />
|
||||
</entry>
|
||||
</value>
|
||||
</component>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
package uk.kagurach.android101;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.Manifest;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
@ -24,6 +29,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,7 +28,6 @@ public class MainActivity2 extends AppCompatActivity {
|
|||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
Log.d("K","2");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -73,7 +72,10 @@ public class MainActivity2 extends AppCompatActivity {
|
|||
if (_r>=0&&_g>=0&&_b>=0&&_r<=255&&_g<=255&&_b<=255){
|
||||
t.setTextColor(Color.rgb(_r,_g,_b));
|
||||
}else{
|
||||
Log.e("page2.setcolor","wrong color");
|
||||
NotificationHelper helper = new NotificationHelper(this);
|
||||
helper.createNotificationChannel();
|
||||
helper.pushNotification("Page 2: Error Setting Color",
|
||||
"The color: R="+_r+" G="+_g+" B="+_b+" is invalid!");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package uk.kagurach.android101
|
||||
|
||||
import android.Manifest
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat.getSystemService
|
||||
import kotlin.random.Random
|
||||
|
||||
class NotificationHelper(_ctx: Context) {
|
||||
val CHANNEL_ID = "DBG_PUSHER"
|
||||
val name = "Debug Pusher"
|
||||
var ctx: Context = _ctx
|
||||
var notifyID = 1;
|
||||
var rng = Random(114514);
|
||||
private val notificationManager =
|
||||
getSystemService(ctx,NotificationManager::class.java) as NotificationManager
|
||||
|
||||
public fun pushNotification(title:String,content:String) {
|
||||
val builder = NotificationCompat.Builder(ctx, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
||||
.setContentTitle(title)
|
||||
.setContentText(content)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setAutoCancel(true)
|
||||
.setStyle(NotificationCompat.BigTextStyle()
|
||||
.bigText(content))
|
||||
|
||||
with(NotificationManagerCompat.from(ctx)) {
|
||||
if (ActivityCompat.checkSelfPermission(
|
||||
ctx,
|
||||
Manifest.permission.POST_NOTIFICATIONS
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
return
|
||||
}
|
||||
notify(notifyID, builder.build())
|
||||
val lastNotifyID = notifyID;
|
||||
while (notifyID==lastNotifyID){
|
||||
notifyID = rng.nextInt()
|
||||
if (notifyID<=0){
|
||||
notifyID = -notifyID
|
||||
notifyID += 1
|
||||
}
|
||||
rng = Random(notifyID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected fun createNotificationChannel() {
|
||||
// Create the NotificationChannel, but only on API 26+ because
|
||||
// the NotificationChannel class is not in the Support Library.
|
||||
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
||||
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {}
|
||||
// Register the channel with the system.
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
protected fun deleteNotificationChannel(){
|
||||
notificationManager.deleteNotificationChannel(CHANNEL_ID)
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@ import androidx.core.view.ViewCompat;
|
|||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class Page3 extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -21,4 +20,5 @@ public class Page3 extends AppCompatActivity {
|
|||
return insets;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue