Commit 3080f853 authored by wanglei's avatar wanglei

常驻通知栏

parent 82ca5b7d
...@@ -131,9 +131,7 @@ ...@@ -131,9 +131,7 @@
android:name=".activity.BatteryInfoActivity" android:name=".activity.BatteryInfoActivity"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<meta-data <meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION" android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
android:value="true" /> android:value="true" />
...@@ -165,9 +163,6 @@ ...@@ -165,9 +163,6 @@
android:screenOrientation="portrait" android:screenOrientation="portrait"
tools:ignore="DiscouragedApi,LockedOrientationActivity" /> tools:ignore="DiscouragedApi,LockedOrientationActivity" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<service <service
android:name=".fcm.FcmService" android:name=".fcm.FcmService"
...@@ -195,6 +190,24 @@ ...@@ -195,6 +190,24 @@
<action android:name="CANCEL_NOTIFICATION" /> <action android:name="CANCEL_NOTIFICATION" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<!--常驻通知栏-->
<service
android:name=".service.PermanentNotificationService"
android:permission="android.permission.FOREGROUND_SERVICE" />
<!--闪光灯-->
<service
android:name=".service.FlashlightService"
android:exported="true"
tools:ignore="ExportedService" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -14,6 +14,7 @@ import com.test.easy.easycleanerjunk.fcm.NotificationUtil ...@@ -14,6 +14,7 @@ import com.test.easy.easycleanerjunk.fcm.NotificationUtil
import com.test.easy.easycleanerjunk.helps.BaseActivity import com.test.easy.easycleanerjunk.helps.BaseActivity
import com.test.easy.easycleanerjunk.helps.ConfigHelper import com.test.easy.easycleanerjunk.helps.ConfigHelper
import com.test.easy.easycleanerjunk.helps.ads.AdmobUtils import com.test.easy.easycleanerjunk.helps.ads.AdmobUtils
import com.test.easy.easycleanerjunk.service.PermanentNotificationService
import com.test.easy.easycleanerjunk.utils.BarUtils import com.test.easy.easycleanerjunk.utils.BarUtils
@SuppressLint("CustomSplashScreen") @SuppressLint("CustomSplashScreen")
...@@ -48,7 +49,7 @@ class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(), ...@@ -48,7 +49,7 @@ class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(),
cancelIntent.action = CloseNotificationReceiver.action cancelIntent.action = CloseNotificationReceiver.action
cancelIntent.putExtra(CloseNotificationReceiver.id, jumpType) cancelIntent.putExtra(CloseNotificationReceiver.id, jumpType)
sendBroadcast(cancelIntent) sendBroadcast(cancelIntent)
}else{ } else {
NotificationUtil.stopNotificationHandler() NotificationUtil.stopNotificationHandler()
} }
...@@ -79,12 +80,12 @@ class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(), ...@@ -79,12 +80,12 @@ class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(),
} }
private fun startNotification() { private fun startNotification() {
// val intent = Intent(this, NotificationService::class.java) val intent = Intent(this, PermanentNotificationService::class.java)
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// startForegroundService(intent) startForegroundService(intent)
// } else { } else {
// startService(intent) startService(intent)
// } }
} }
override fun onProgressMax() { override fun onProgressMax() {
......
package com.test.easy.easycleanerjunk.service
import android.app.Service
import android.content.Context
import android.content.Intent
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Build
import android.os.IBinder
import android.util.Log
class FlashlightService : Service() {
private var cameraManager: CameraManager? = null
private var flashlightOn = false
override fun onCreate() {
super.onCreate()
Log.e("MXL", "SerciveonCreate: ")
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager?
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
val cameraId = cameraManager!!.cameraIdList[0]
flashlightOn = if (flashlightOn) {
cameraManager?.setTorchMode(cameraId, false)
false
} else {
cameraManager?.setTorchMode(cameraId, true)
true
}
} catch (e: CameraAccessException) {
e.printStackTrace()
}
return START_NOT_STICKY
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
}
\ No newline at end of file
package com.test.easy.easycleanerjunk.service
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.test.easy.easycleanerjunk.R
import com.test.easy.easycleanerjunk.activity.AppManagerActivity
import com.test.easy.easycleanerjunk.activity.BatteryInfoActivity
import com.test.easy.easycleanerjunk.activity.CleanJunkActivity
import com.test.easy.easycleanerjunk.activity.home.NewMainActivity
/**
* 常驻通知栏
*/
class PermanentNotificationService : Service() {
companion object {
var isRunning = false
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (isRunning) {
stopSelf()
return START_NOT_STICKY
}
val notification = createPermanentNotification(applicationContext)
startForeground(1, notification)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onDestroy() {
isRunning = false
super.onDestroy()
// 取消订阅
}
private fun createPermanentNotification(context: Context): Notification {
val isOngoing = true //是否持续(为不消失的常驻通知)
val channelName = "Foreground Service Channel"
val channelId = "Service_Id"
val category = Notification.CATEGORY_SERVICE
val contentView = RemoteViews(context.packageName, R.layout.reminder_layout_notification_notify)
val expendView = RemoteViews(context.packageName, R.layout.reminder_layout_notification_big_notify)
val intent0 = Intent(context, CleanJunkActivity::class.java)
val pendingIntent0 =
PendingIntent.getActivity(context, 0, intent0, PendingIntent.FLAG_IMMUTABLE)
contentView.setOnClickPendingIntent(R.id.id_ll_clean, pendingIntent0)
expendView.setOnClickPendingIntent(R.id.id_ll_clean, pendingIntent0)
val intent2 = Intent(context, AppManagerActivity::class.java)
val pendingIntent2 =
PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)
contentView.setOnClickPendingIntent(R.id.id_app_manager, pendingIntent2)
expendView.setOnClickPendingIntent(R.id.id_app_manager, pendingIntent2)
val intent3 = Intent(context, BatteryInfoActivity::class.java)
val pendingIntent3 =
PendingIntent.getActivity(context, 0, intent3, PendingIntent.FLAG_IMMUTABLE)
contentView.setOnClickPendingIntent(R.id.id_battery_info, pendingIntent3)
expendView.setOnClickPendingIntent(R.id.id_battery_info, pendingIntent3)
val intent4 = Intent()
val serviceComponent = ComponentName(context, FlashlightService::class.java)
intent4.component = serviceComponent
val pendingIntent4 =
PendingIntent.getService(context, 0, intent4, PendingIntent.FLAG_IMMUTABLE)
contentView.setOnClickPendingIntent(R.id.id_light, pendingIntent4)
expendView.setOnClickPendingIntent(R.id.id_light, pendingIntent4)
val nfIntent = Intent(context, NewMainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)
val builder = NotificationCompat.Builder(context, channelId)
builder.setCustomContentView(contentView)
builder.setCustomBigContentView(expendView)
builder.setContentIntent(pendingIntent) //设置PendingIntent
builder.setSmallIcon(R.drawable.easycleanjunk_noticeicon) //设置状态栏内的小图标
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
// builder.setOngoing(isOngoing) //设置持续(不消失的常驻通知)
// builder.setCategory(category) //设置类别
builder.setAutoCancel(false)
builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为:重要通知
builder.setWhen(System.currentTimeMillis())
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
...@@ -23,7 +23,8 @@ ...@@ -23,7 +23,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginStart="13dp" android:layout_marginStart="13dp"
android:src="@mipmap/fanhui" /> android:src="@mipmap/fanhui"
android:tint="@color/black" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
...@@ -31,7 +32,8 @@ ...@@ -31,7 +32,8 @@
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:text="Battery info" android:text="Battery info"
android:textSize="18sp" android:textSize="18sp"
android:textStyle="bold" /> android:textStyle="bold"
tools:ignore="HardcodedText" />
</RelativeLayout> </RelativeLayout>
<FrameLayout <FrameLayout
......
...@@ -33,9 +33,61 @@ ...@@ -33,9 +33,61 @@
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
</LinearLayout> </LinearLayout>
<!--app管理-->
<LinearLayout
android:id="@+id/id_app_manager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:src="@mipmap/guanli"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manager"
android:textColor="#666666"
android:textSize="12sp"
tools:ignore="HardcodedText" />
</LinearLayout>
<!--电池-->
<LinearLayout
android:id="@+id/id_battery_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:src="@mipmap/dianchi"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Battery"
android:textColor="#666666"
android:textSize="12sp"
tools:ignore="HardcodedText" />
</LinearLayout>
<!--手电--> <!--手电-->
<LinearLayout <LinearLayout
android:id="@+id/id_lighit" android:id="@+id/id_light"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
......
...@@ -24,9 +24,44 @@ ...@@ -24,9 +24,44 @@
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
</LinearLayout> </LinearLayout>
<!--app管理-->
<LinearLayout
android:id="@+id/id_app_manager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="4dp"
android:src="@mipmap/guanli"
tools:ignore="ContentDescription" />
</LinearLayout>
<!--电池-->
<LinearLayout
android:id="@+id/id_battery_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="4dp"
android:src="@mipmap/dianchi"
tools:ignore="ContentDescription" />
</LinearLayout>
<!--手电--> <!--手电-->
<LinearLayout <LinearLayout
android:id="@+id/id_lighit" android:id="@+id/id_light"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment