Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
S
SuperEasyClean
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
songjianyu
SuperEasyClean
Commits
0d93d035
Commit
0d93d035
authored
Dec 27, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
1ad5fa9f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
443 additions
and
0 deletions
+443
-0
README.md
README.md
+2
-0
StayJobService.kt
app/src/main/java/com/base/appzxhy/service/StayJobService.kt
+122
-0
StayNotification.kt
...rc/main/java/com/base/appzxhy/service/StayNotification.kt
+149
-0
stay_notification_big_location.xml
app/src/main/res/layout/stay_notification_big_location.xml
+142
-0
stay_notification_small_location.xml
app/src/main/res/layout/stay_notification_small_location.xml
+28
-0
No files found.
README.md
View file @
0d93d035
...
@@ -19,4 +19,6 @@
...
@@ -19,4 +19,6 @@
11.
设置闹钟(24h,48h,72h)
11.
设置闹钟(24h,48h,72h)
12.
注册生命周期回调,app从后台回来走启动页
12.
注册生命周期回调,app从后台回来走启动页
## 常驻通知栏
app/src/main/java/com/base/appzxhy/service/StayJobService.kt
0 → 100644
View file @
0d93d035
package
com.base.appzxhy.service
import
android.app.NotificationManager
import
android.app.job.JobInfo
import
android.app.job.JobParameters
import
android.app.job.JobScheduler
import
android.app.job.JobService
import
android.content.ComponentName
import
android.content.Context
import
android.content.Intent
import
android.content.pm.ServiceInfo
import
android.os.Build
import
android.os.CountDownTimer
import
androidx.work.Configuration
import
com.base.appzxhy.BuildConfig
import
com.base.appzxhy.service.StayNotification.createPermanentNotification
import
com.base.appzxhy.utils.LogEx
/**
* 常驻通知栏
*/
class
StayJobService
:
JobService
()
{
init
{
val
builder
=
Configuration
.
Builder
()
builder
.
setJobSchedulerJobIdRange
(
0
,
1000
)
}
companion
object
{
private
val
TAG
=
"StayJobService"
val
NOTIFICATION_PERMANENT_ID
=
186
private
var
isRunning
=
false
private
const
val
JOB_INFO_ID
:
Int
=
101
private
const
val
JOB_PERIODIC
:
Long
=
5
*
1000L
fun
Context
.
startJob
()
{
if
(
isRunning
)
return
LogEx
.
logDebug
(
TAG
,
"startJob"
)
val
jobScheduler
=
getSystemService
(
JOB_SCHEDULER_SERVICE
)
as
JobScheduler
val
componentName
=
ComponentName
(
this
,
StayJobService
::
class
.
java
)
val
jobInfo
=
JobInfo
.
Builder
(
JOB_INFO_ID
,
componentName
)
.
setMinimumLatency
(
30000
)
.
build
()
jobScheduler
.
schedule
(
jobInfo
)
}
}
override
fun
onStartCommand
(
intent
:
Intent
?,
flags
:
Int
,
startId
:
Int
):
Int
{
LogEx
.
logDebug
(
TAG
,
"onStartCommand"
)
return
super
.
onStartCommand
(
intent
,
flags
,
startId
)
}
private
fun
startForeground
()
{
val
notification
=
createPermanentNotification
(
applicationContext
)
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
Q
)
{
startForeground
(
NOTIFICATION_PERMANENT_ID
,
notification
,
ServiceInfo
.
FOREGROUND_SERVICE_TYPE_LOCATION
)
}
else
{
startForeground
(
NOTIFICATION_PERMANENT_ID
,
notification
)
}
isRunning
=
true
}
private
fun
notifyForeground
()
{
val
notificationManager
=
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
notificationManager
.
notify
(
NOTIFICATION_PERMANENT_ID
,
createPermanentNotification
(
applicationContext
)
)
}
override
fun
onDestroy
()
{
isRunning
=
false
super
.
onDestroy
()
}
override
fun
onCreate
()
{
LogEx
.
logDebug
(
TAG
,
"onCreate isRunning=$isRunning"
)
if
(!
isRunning
)
{
isRunning
=
true
startForeground
()
Timer
().
start
()
}
super
.
onCreate
()
}
override
fun
onStartJob
(
params
:
JobParameters
?):
Boolean
{
return
true
}
override
fun
onStopJob
(
params
:
JobParameters
?):
Boolean
{
return
false
}
val
millisInFuture
=
if
(
BuildConfig
.
DEBUG
)
5000L
else
30000L
private
inner
class
Timer
()
:
CountDownTimer
(
millisInFuture
,
1000
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
}
override
fun
onFinish
()
{
LogEx
.
logDebug
(
TAG
,
"Timer onFinish"
)
notifyForeground
()
Timer
().
start
()
}
}
}
app/src/main/java/com/base/appzxhy/service/StayNotification.kt
0 → 100644
View file @
0d93d035
package
com.base.appzxhy.service
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.graphics.BitmapFactory
import
android.graphics.drawable.Icon
import
android.os.Build
import
android.widget.RemoteViews
import
androidx.core.app.NotificationCompat
import
androidx.core.graphics.drawable.IconCompat
import
com.base.appzxhy.R
import
com.base.appzxhy.service.StayJobService.Companion.NOTIFICATION_PERMANENT_ID
import
com.base.appzxhy.ui.main.MainActivity
object
StayNotification
{
private
fun
customRemoteViews
(
contentView
:
RemoteViews
,
expendView
:
RemoteViews
)
{
// val requestCode1 = Random.nextInt(1800)
// val intent1 = Intent(context, MyStartActivity::class.java).apply {
// putExtra("actionId", ConstObject.NOTIFICATION_ACTION_LOG)
// }
// val pendingIntent1 =
// PendingIntent.getActivity(
// context,
// requestCode1,
// intent1,
// PendingIntent.FLAG_IMMUTABLE
// )
// contentView.setOnClickPendingIntent(R.id.fl_1, pendingIntent1)
// expendView.setOnClickPendingIntent(R.id.fl_1, pendingIntent1)
//
// val requestCode2 = Random.nextInt(1800)
// val intent2 = Intent(context, MyStartActivity::class.java).apply {
// putExtra("actionId", ConstObject.NOTIFICATION_ACTION_DOCUMENT)
// }
// val pendingIntent2 =
// PendingIntent.getActivity(
// context,
// requestCode2,
// intent2,
// PendingIntent.FLAG_IMMUTABLE
// )
// contentView.setOnClickPendingIntent(R.id.ll_2, pendingIntent2)
// expendView.setOnClickPendingIntent(R.id.ll_2, pendingIntent2)
//
// val requestCode3 = Random.nextInt(1800)
// val intent3 = Intent(context, MyStartActivity::class.java).apply {
// putExtra("actionId", ConstObject.NOTIFICATION_ACTION_BOOKMARK)
// }
// val pendingIntent3 =
// PendingIntent.getActivity(
// context,
// requestCode3,
// intent3,
// PendingIntent.FLAG_IMMUTABLE
// )
// contentView.setOnClickPendingIntent(R.id.ll_3, pendingIntent3)
// expendView.setOnClickPendingIntent(R.id.ll_3, pendingIntent3)
//
//
// val requestCode4 = Random.nextInt(1800)
// val intent4 = Intent(context, MyStartActivity::class.java).apply {
// putExtra("actionId", ConstObject.NOTIFICATION_ACTION_NEW_IMAGE_PDF)
// }
// val pendingIntent4 =
// PendingIntent.getActivity(
// context,
// requestCode4,
// intent4,
// PendingIntent.FLAG_IMMUTABLE
// )
// contentView.setOnClickPendingIntent(R.id.ll_4, pendingIntent4)
// expendView.setOnClickPendingIntent(R.id.ll_4, pendingIntent4)
}
fun
notifyStayNotification
(
context
:
Context
,
desc
:
String
)
{
val
notificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
notificationManager
.
notify
(
NOTIFICATION_PERMANENT_ID
,
createPermanentNotification
(
context
)
)
}
fun
createPermanentNotification
(
context
:
Context
):
Notification
{
val
channelName
=
"Permanent Foreground Service Channel"
val
channelId
=
"permanent_channel"
val
contentView
=
RemoteViews
(
context
.
packageName
,
R
.
layout
.
stay_notification_small_location
)
val
expendView
=
RemoteViews
(
context
.
packageName
,
R
.
layout
.
stay_notification_big_location
)
customRemoteViews
(
contentView
,
expendView
)
val
builder
=
NotificationCompat
.
Builder
(
context
,
channelId
)
val
smallIcon
=
IconCompat
.
createFromIcon
(
context
,
Icon
.
createWithResource
(
context
,
R
.
mipmap
.
ic_launcher
)
)
smallIcon
?.
let
{
builder
.
setSmallIcon
(
smallIcon
)
//设置状态栏内的小图标
}
val
nfIntent
=
Intent
(
context
,
MainActivity
::
class
.
java
)
val
pendingIntent
=
PendingIntent
.
getActivity
(
context
,
0
,
nfIntent
,
PendingIntent
.
FLAG_IMMUTABLE
)
builder
.
setLargeIcon
(
BitmapFactory
.
decodeResource
(
context
.
resources
,
R
.
mipmap
.
ic_launcher
)
)
builder
.
setContentTitle
(
context
.
resources
.
getString
(
R
.
string
.
app_name
))
builder
.
setContentIntent
(
pendingIntent
)
//设置PendingIntent
builder
.
setVisibility
(
NotificationCompat
.
VISIBILITY_PRIVATE
)
//设置通知公开可见
builder
.
setAutoCancel
(
false
)
builder
.
setOngoing
(
true
)
builder
.
setPriority
(
NotificationCompat
.
PRIORITY_MAX
)
//优先级为:重要通知
builder
.
setWhen
(
System
.
currentTimeMillis
())
builder
.
setCustomContentView
(
contentView
)
builder
.
setCustomBigContentView
(
expendView
)
builder
.
setCustomHeadsUpContentView
(
expendView
)
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
val
channel
=
NotificationChannel
(
channelId
,
channelName
,
NotificationManager
.
IMPORTANCE_LOW
)
channel
.
lockscreenVisibility
=
1
val
notificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
notificationManager
.
createNotificationChannel
(
channel
)
builder
.
setChannelId
(
channelId
)
}
return
builder
.
build
()
}
}
\ No newline at end of file
app/src/main/res/layout/stay_notification_big_location.xml
0 → 100644
View file @
0d93d035
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:orientation=
"vertical"
android:paddingVertical=
"8dp"
tools:ignore=
"DisableBaselineAlignment"
>
<TextView
android:id=
"@+id/tv_desc"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginStart=
"30dp"
android:layout_marginTop=
"10dp"
android:text=
"@string/sharing_location_people"
android:textColor=
"@color/black"
android:textStyle=
"bold"
/>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"8dp"
android:orientation=
"horizontal"
>
<LinearLayout
android:id=
"@+id/ll_1"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_vertical"
android:layout_weight=
"1"
android:orientation=
"vertical"
tools:ignore=
"UseCompoundDrawables"
>
<ImageView
android:layout_width=
"35dp"
android:layout_height=
"35dp"
android:layout_gravity=
"center_horizontal"
android:src=
"@mipmap/home_notification"
tools:ignore=
"ContentDescription"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_horizontal"
android:layout_marginTop=
"2dp"
android:includeFontPadding=
"false"
android:text=
"@string/home"
android:textColor=
"@color/black"
android:textSize=
"14sp"
tools:ignore=
"HardcodedText"
/>
</LinearLayout>
<LinearLayout
android:id=
"@+id/ll_2"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_vertical"
android:layout_weight=
"1"
android:orientation=
"vertical"
tools:ignore=
"UseCompoundDrawables"
>
<ImageView
android:layout_width=
"35dp"
android:layout_height=
"35dp"
android:layout_gravity=
"center_horizontal"
android:src=
"@mipmap/mycode_notification"
tools:ignore=
"ContentDescription"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_horizontal"
android:layout_marginTop=
"2dp"
android:includeFontPadding=
"false"
android:text=
"@string/my_code"
android:textColor=
"@color/black"
android:textSize=
"14sp"
tools:ignore=
"HardcodedText"
/>
</LinearLayout>
<LinearLayout
android:id=
"@+id/ll_3"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:orientation=
"vertical"
tools:ignore=
"UseCompoundDrawables"
>
<ImageView
android:layout_width=
"35dp"
android:layout_height=
"35dp"
android:layout_gravity=
"center_horizontal"
android:src=
"@mipmap/share_notification"
tools:ignore=
"ContentDescription"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_horizontal"
android:layout_marginTop=
"2dp"
android:includeFontPadding=
"false"
android:text=
"@string/share"
android:textColor=
"@color/black"
android:textSize=
"14sp"
tools:ignore=
"HardcodedText"
/>
</LinearLayout>
<LinearLayout
android:id=
"@+id/ll_4"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:orientation=
"vertical"
tools:ignore=
"UseCompoundDrawables"
>
<ImageView
android:layout_width=
"35dp"
android:layout_height=
"35dp"
android:layout_gravity=
"center_horizontal"
android:src=
"@mipmap/settings_notification"
tools:ignore=
"ContentDescription"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_horizontal"
android:layout_marginTop=
"2dp"
android:includeFontPadding=
"false"
android:text=
"@string/settings"
android:textColor=
"@color/black"
android:textSize=
"14sp"
tools:ignore=
"HardcodedText"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
\ No newline at end of file
app/src/main/res/layout/stay_notification_small_location.xml
0 → 100644
View file @
0d93d035
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:orientation=
"vertical"
>
<TextView
android:id=
"@+id/tv_title"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginHorizontal=
"8dp"
android:text=
"@string/phone_tracker"
android:textColor=
"#010101"
android:textSize=
"12sp"
android:textStyle=
"bold"
/>
<TextView
android:id=
"@+id/tv_desc"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_vertical"
android:layout_marginHorizontal=
"8dp"
android:includeFontPadding=
"false"
android:text=
"@string/background_for_location"
android:textSize=
"11sp"
/>
</LinearLayout>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment