Home > Enterprise >  Unable to Create Service in Android
Unable to Create Service in Android

Time:06-07

I started learning android app development in Java very recently. I am having trouble creating a service that plays the default ringtone of the mobile. Following are the code files and LogCat for my app. Kind help will be highly appreciated.

AudioService.java

package com.example.audioservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.provider.Settings;

public abstract class AudioService extends Service {

    MediaPlayer player;

    @Override
    public int onStartCommand(Intent intent, int flags, int StartID) {
        player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
        player.setLooping(false);
        player.start();
        return START_NOT_STICKY;
    }
}

MainActivity.java

package com.example.audioservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, AudioService.class));
    }
}

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AudioService"
        tools:targetApi="31">
        <service
            android:name=".AudioService"
            android:enabled="true">
        </service>

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

LogCat

2022-06-07 11:38:26.452 12102-12102/com.example.audioservice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.audioservice, PID: 12102
    java.lang.RuntimeException: Unable to create service com.example.audioservice.AudioService: java.lang.InstantiationException: java.lang.Class<com.example.audioservice.AudioService> cannot be instantiated
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4500)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.InstantiationException: java.lang.Class<com.example.audioservice.AudioService> cannot be instantiated
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
        at androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:75)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4469)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7839) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

CodePudding user response:

You've declared AudioService as an abstract class, which obviously cannot be instantiated. Remove abstract keyword and override onBind instead.

public class AudioService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    ...
}
  • Related