Android闹钟级信号提醒:AlarmReceiver(全屏意图通知)+AlarmActivity(锁屏亮屏/USAGE_ALARM循环铃/重复震动/知道了停止);设置页加强提醒引导卡
This commit is contained in:
@@ -39,6 +39,9 @@ android {
|
||||
|
||||
dependencies {
|
||||
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4")
|
||||
// AlarmReceiver 继承 JPushMessageReceiver,需在 app 模块编译期可见 SDK 类;
|
||||
// jpush_flutter 插件以 implementation 引入不透传到 app,这里显式补充(版本与插件一致)
|
||||
implementation("cn.jiguang.sdk:jpush:6.3.1")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||
<application
|
||||
android:label="玄龙期货"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
@@ -29,6 +32,18 @@
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<receiver android:name=".AlarmReceiver" android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="cn.jpush.android.intent.RECEIVER_MESSAGE"/>
|
||||
<category android:name="com.xuanlong.xuanlong_futures"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<activity android:name=".AlarmActivity"
|
||||
android:exported="false"
|
||||
android:launchMode="singleTask"
|
||||
android:showWhenLocked="true"
|
||||
android:turnScreenOn="true"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
|
||||
<!-- Don't delete the meta-data below.
|
||||
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
|
||||
<meta-data
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.xuanlong.xuanlong_futures
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.media.AudioAttributes
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import android.view.WindowManager
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
class AlarmActivity : Activity() {
|
||||
private var player: MediaPlayer? = null
|
||||
private var vibrator: Vibrator? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
window.addFlags(
|
||||
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
|
||||
or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|
||||
)
|
||||
setContentView(R.layout.activity_alarm)
|
||||
findViewById<Button>(R.id.alarm_dismiss).setOnClickListener { stopAndFinish() }
|
||||
startRing()
|
||||
render(intent)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
if (player == null) startRing() // 已停止又被拉起时重新响
|
||||
render(intent)
|
||||
}
|
||||
|
||||
private fun render(intent: Intent?) {
|
||||
val kind = intent?.getStringExtra("kind") ?: return
|
||||
val kindText = when (kind) {
|
||||
"long_ignition" -> "做多起爆点"; "short_ignition" -> "做空起爆点"
|
||||
"bd_resonance" -> "卖出(BD共振)"; "digest" -> "新信号"
|
||||
else -> "量价背离"
|
||||
}
|
||||
val detail = if (kind == "digest") {
|
||||
"${intent.getStringExtra("summary") ?: ""}"
|
||||
} else {
|
||||
"${intent.getStringExtra("symbol")} · ${intent.getStringExtra("interval")} · ${intent.getStringExtra("divergence_time")?.substring(5)}"
|
||||
}
|
||||
findViewById<TextView>(R.id.alarm_kind).text = kindText
|
||||
findViewById<TextView>(R.id.alarm_detail).text = detail
|
||||
}
|
||||
|
||||
private fun startRing() {
|
||||
if (player != null) return
|
||||
val attrs = AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.build()
|
||||
player = MediaPlayer.create(this, R.raw.alarm_ring, attrs, -1)?.apply {
|
||||
isLooping = true
|
||||
start()
|
||||
}
|
||||
vibrator = getSystemService(Vibrator::class.java).also { v ->
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
v.vibrate(VibrationEffect.createWaveform(longArrayOf(0, 600, 400), 0))
|
||||
} else {
|
||||
@Suppress("DEPRECATION") v.vibrate(longArrayOf(0, 600, 400), 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopAndFinish() {
|
||||
player?.run { stop(); release() }
|
||||
player = null
|
||||
vibrator?.cancel()
|
||||
vibrator = null
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
player?.run { stop(); release() }
|
||||
vibrator?.cancel()
|
||||
player = null; vibrator = null
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.xuanlong.xuanlong_futures
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.AudioAttributes
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.app.ActivityManager
|
||||
import cn.jpush.android.api.CustomMessage
|
||||
import cn.jpush.android.service.JPushMessageReceiver
|
||||
import org.json.JSONObject
|
||||
|
||||
class AlarmReceiver : JPushMessageReceiver() {
|
||||
override fun onMessage(context: Context, customMessage: CustomMessage?) {
|
||||
val msg = customMessage ?: return
|
||||
val extras = try { JSONObject(msg.extra ?: "{}") } catch (_: Exception) { return }
|
||||
if (extras.optString("type") != "signal_alarm") return
|
||||
|
||||
val intent = Intent(context, AlarmActivity::class.java).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
putExtra("kind", extras.optString("kind", "divergence"))
|
||||
if (extras.optString("kind") == "digest") {
|
||||
putExtra("summary", "本轮新增 ${extras.optString("signal_ids").split(",").size} 个信号,打开查看")
|
||||
} else {
|
||||
putExtra("symbol", extras.optString("symbol"))
|
||||
putExtra("interval", extras.optString("interval"))
|
||||
putExtra("divergence_time", extras.optString("divergence_time"))
|
||||
}
|
||||
}
|
||||
|
||||
// App 在前台:直接拉起,不经通知
|
||||
if (isAppForeground(context)) {
|
||||
context.startActivity(intent)
|
||||
return
|
||||
}
|
||||
// 后台/杀进程:全屏意图通知(系统有权限则直接弹 Activity,无权限降级 heads-up)
|
||||
notifyFullScreen(context, intent)
|
||||
}
|
||||
|
||||
private fun isAppForeground(context: Context): Boolean {
|
||||
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
val process = "${context.packageName}"
|
||||
return am.runningAppProcesses?.any {
|
||||
it.processName == process && it.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
|
||||
} ?: false
|
||||
}
|
||||
|
||||
private fun notifyFullScreen(context: Context, intent: Intent) {
|
||||
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val pi = PendingIntent.getActivity(
|
||||
context, 20261122, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
val builder: Notification.Builder
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
val channel = NotificationChannel(CHANNEL_ID, "信号强提醒", NotificationManager.IMPORTANCE_HIGH).apply {
|
||||
setSound(
|
||||
Uri.parse("android.resource://${context.packageName}/${R.raw.alarm_ring}"),
|
||||
AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build()
|
||||
)
|
||||
}
|
||||
nm.createNotificationChannel(channel)
|
||||
builder = Notification.Builder(context, CHANNEL_ID)
|
||||
} else {
|
||||
builder = Notification.Builder(context)
|
||||
}
|
||||
val notification = builder
|
||||
.setContentTitle("玄龙期货")
|
||||
.setContentText(intent.getStringExtra("kind")?.let { labelOf(it) } ?: "新信号")
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_alert)
|
||||
.setFullScreenIntent(pi, true)
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
nm.notify(20261122, notification)
|
||||
}
|
||||
|
||||
private fun labelOf(kind: String) = when (kind) {
|
||||
"long_ignition" -> "做多起爆点"; "short_ignition" -> "做空起爆点"
|
||||
"bd_resonance" -> "卖出(BD共振)"; "digest" -> "新信号"
|
||||
else -> "量价背离"
|
||||
}
|
||||
|
||||
companion object { private const val CHANNEL_ID = "signal_alarm_v1" }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent"
|
||||
android:orientation="vertical" android:gravity="center"
|
||||
android:background="@color/alarm_bg"
|
||||
android:padding="32dp">
|
||||
|
||||
<TextView android:id="@+id/alarm_kind"
|
||||
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||
android:textColor="@color/alarm_accent" android:textSize="40sp"
|
||||
android:textStyle="bold"/>
|
||||
|
||||
<TextView android:id="@+id/alarm_detail"
|
||||
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:textColor="@color/alarm_text" android:textSize="24sp"
|
||||
android:gravity="center"/>
|
||||
|
||||
<Button android:id="@+id/alarm_dismiss"
|
||||
android:layout_width="match_parent" android:layout_height="64dp"
|
||||
android:layout_marginTop="48dp"
|
||||
android:backgroundTint="@color/alarm_accent"
|
||||
android:text="知道了" android:textSize="22sp" android:textColor="@color/alarm_text"/>
|
||||
</LinearLayout>
|
||||
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="alarm_bg">#1A0F0D</color>
|
||||
<color name="alarm_accent">#C8452E</color>
|
||||
<color name="alarm_text">#F5E9D8</color>
|
||||
</resources>
|
||||
@@ -169,6 +169,8 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
|
||||
: null,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
_alarmGuideCard(),
|
||||
const SizedBox(height: 10),
|
||||
_dndCard(s, active),
|
||||
const SizedBox(height: 10),
|
||||
_tutorialCard(),
|
||||
@@ -181,6 +183,34 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _alarmGuideCard() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: XlColors.surface,
|
||||
borderRadius: BorderRadius.circular(XlRadius.card),
|
||||
border: Border.all(color: XlColors.border),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.alarm, size: 20, color: XlColors.goldLight),
|
||||
const SizedBox(width: 10),
|
||||
const Expanded(
|
||||
child: Text(
|
||||
'信号到达将以闹钟方式强提醒(锁屏弹屏+持续响铃)。若锁屏时未弹出,'
|
||||
'请在系统设置→应用→玄龙期货中开启「悬浮通知/允许全屏显示」。',
|
||||
style: TextStyle(fontSize: 13),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: openAppSettings,
|
||||
child: const Text('去设置'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _updateCard() {
|
||||
var checking = false;
|
||||
return StatefulBuilder(
|
||||
|
||||
Reference in New Issue
Block a user