| | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.IntentFilter; |
| | | import android.content.pm.ServiceInfo; |
| | | import android.graphics.BitmapFactory; |
| | | import android.graphics.Color; |
| | | import android.os.Build; |
| | |
| | | */ |
| | | public class ForeService extends Service { |
| | | |
| | | final String TAG=ForeService.class.getName(); |
| | | final String TAG = ForeService.class.getName(); |
| | | |
| | | @Override |
| | | public IBinder onBind(Intent intent) { |
| | | return null; |
| | | } |
| | | |
| | | |
| | | @SuppressLint("ForegroundServiceType") |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | Log.d(TAG, "ForeService onCreate() 进程Id:"+android.os.Process.myPid() ); |
| | | startForeground(1, getNotification(getString(R.string.app_name), "Running")); |
| | | // Log.d(TAG, "ForeService onCreate() 进程Id:" + android.os.Process.myPid()); |
| | | // try { |
| | | // // Kotlin 示例 |
| | | // if (Build.VERSION.SDK_INT >= 34) { |
| | | // startForeground(1, getNotification(getString(R.string.app_name), "Running"), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); |
| | | // } else { |
| | | // startForeground(1, getNotification(getString(R.string.app_name), "Running")); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // } |
| | | } |
| | | |
| | | @Override |
| | |
| | | builder = new Notification.Builder(this, "f_channel_id"); |
| | | } |
| | | builder.setAutoCancel(true)//设置是否允许自动清除 |
| | | .setSmallIcon(R.drawable.logo)//设置状态栏里的小图标 |
| | | .setSmallIcon(R.drawable.notification_logo)//设置状态栏里的小图标 |
| | | .setContentTitle(title)//设置通知栏里面的标题文本 |
| | | .setContentText(message);//设置通知栏里面的内容文本 |
| | | //根据消息构造器创建一个通知对象 |