执行完所有步骤后前台服务未显示

问题描述

我想在我的应用中显示一个前台服务。

我有需要的一切-

class LocationService : Service(){


    override fun onStartCommand(intent: Intent?,flags: Int,startId: Int): Int {

        val CHANNEL_ID = "ForegroundServiceChannel"

        val input = intent?.getStringExtra("inputExtra")
        createNotificationChannel()
        val notificationIntent = Intent(this,MainFragment::class.java)
        val pendingIntent = PendingIntent.getActivity(this,notificationIntent,0)
        val notification = NotificationCompat.Builder(this,CHANNEL_ID)
            .setContentTitle("Fetching Location like a boss")
            .setContentText(input)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentIntent(pendingIntent)
            .build()
        startForeground(1,notification)
        return START_NOT_STICKY
    }

    private fun createNotificationChannel(){
        if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.O) return
        val serviceChannel = NotificationChannel("channel","Alon Shlider",notificationmanager.IMPORTANCE_DEFAULT)
        val manager = getSystemService(notificationmanager::class.java)
        serviceChannel.description = "Best channel ever"
        serviceChannel.enableLights(true)
        serviceChannel.lightColor = Color.RED
        serviceChannel.enableVibration(true)
        manager.createNotificationChannel(serviceChannel)
    }

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

}


class MainFragment : Fragment() {
    
    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)

        init()
        if (handlePermissions()) return

        val intent = Intent(requireContext(),LocationService::class.java)
        intent.putExtra("inputExtra","Alon Shlider")
        requireActivity().startService(intent)

    }

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.basicapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <application
        android:name=".utils.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/Theme.BasicApplication">
        <activity android:name=".ApplicationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="LocationService"
            android:enabled="true"
            android:exported="true" />
<!--            android:foregroundServiceType="location" />-->

    </application>

</manifest>

前台服务应该在应用启动时启动,但由于某种原因没有启动。

我已经尝试了几次来弄清楚我缺少什么,但我没有找到任何有用的解决方案......

到目前为止,我在互联网上找到的所有解决方案都具有相同的实现方式,但似乎不起作用。

有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...