Reputation: 327
i have an inner class named DownloadJobNotification that extends JobService thats supposed to perform a notification showing once a day or when the download button is clicked. i added the service to the manifest and still for some reason i receive this error
java.lang.RuntimeException: Unable to instantiate service com.example.jobcodlabhomework.MainActivity$DownloadJobNotification: java.lang.InstantiationException: java.lang.Class<com.example.jobcodlabhomework.MainActivity$DownloadJobNotification> has no zero argument constructor
here is my code:
private const val CHANNEL_ID = "JOB_CHANNEL_ID"
private const val NOTIFICATION_CHANNEL_ID = "DOWNLOAD_NOTIFICATION"
private lateinit var mNotifyManager: NotificationManager
private lateinit var mScheduler: JobScheduler
private const val JOB_ID = 0
private lateinit var jobNotification: MainActivity.DownloadJobNotification
private const val NOTIFICATION_ID = 12
private lateinit var jobInfo: JobInfo.Builder
class MainActivity : AppCompatActivity() {
inner class DownloadJobNotification : JobService() {
fun createNotificationChannel() {
mNotifyManager =
[email protected](Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Download Job Notification0",
NotificationManager.IMPORTANCE_HIGH
)
notificationChannel.enableVibration(true)
notificationChannel.enableLights(true)
notificationChannel.description = "Download Job Notification"
mNotifyManager.createNotificationChannel(notificationChannel)
}
}
override fun onStopJob(params: JobParameters?): Boolean {
return true
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartJob(params: JobParameters?): Boolean {
Log.d("MainActivity", "Job Started")
val notification = Notification.Builder(this, CHANNEL_ID)
.setAutoCancel(true)
.setContentText("Download in progress")
.setContentTitle("Performing Work")
.setSmallIcon(R.drawable.ic_launcher_background)
mNotifyManager.notify(NOTIFICATION_ID, notification.build())
return false
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
jobNotification = DownloadJobNotification()
//Log.d("MainActivity", "$mNotifyManager")
jobNotification.createNotificationChannel()
jobInfo = JobInfo.Builder(
JOB_ID,
ComponentName(packageName, DownloadJobNotification::class.java.name)
)
//.setRequiresDeviceIdle(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPeriodic(86400000)
button1.setOnClickListener {
performDownload()
}
}
private fun performDownload() {
mScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
mScheduler.schedule(jobInfo.build())
}
}
here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jobcodlabhomework">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MainActivity$DownloadJobNotification"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>
here is the logcat:
2020-04-14 13:16:07.102 11817-11817/com.example.jobcodlabhomework E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jobcodlabhomework, PID: 11817
java.lang.RuntimeException: Unable to instantiate service com.example.jobcodlabhomework.MainActivity$DownloadJobNotification: java.lang.InstantiationException: java.lang.Class<com.example.jobcodlabhomework.MainActivity$DownloadJobNotification> has no zero argument constructor
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3176)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.jobcodlabhomework.MainActivity$DownloadJobNotification> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3173)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Upvotes: 0
Views: 459
Reputation: 4544
You cannot make the Job an inner
class. This also means you don't have access to the outer activity. But since JobService is a Service you can just use this
for context related actions (like getting system services).
So instead of
mNotifyManager =
[email protected](Context.NOTIFICATION_SERVICE) as NotificationManager
you can just use:
mNotifyManager =
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
It also doesn't really make that much sense to put it as a nested class. You can just have it separate from the Activity.
Upvotes: 1