Nikhil Wagh
Nikhil Wagh

Reputation: 1498

service cannot be cast to android.app.Activity

I am trying to access a editText and a Button in a Service. I've seen this answer and this answer, and tried to merge and work out, but no gain. What am I missing here? I have seen other similar answers too and been trying to get it done from last 2 days but all in vain. Any other way of accomplishing it will also be welcome.

Here is the relevant code: MainActivity.java

btn_start.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View view) {
                startService(new Intent(getApplicationContext(), MyService.class));
            }
        });

MyService.java

        Context context = MyService.this;
        Activity activity = (Activity) context;
        Button stopButton = activity.findViewById(R.id.btn_stop);
        EditText editTextUsername = activity.findViewById(R.id.edit_name);

AndroidManifest.xml

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
        </service>

But I get this error: LOGCAT

11-14 02:55:07.835 5906-5906/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nwagh.myapplication, PID: 5906 java.lang.RuntimeException: Unable to start service com.example.nwagh.myapplication.MyService@32189af with Intent { cmp=com.example.nwagh.myapplication/.MyService }: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3060) at android.app.ActivityThread.access$2300(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) Caused by: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at com.example.nwagh.myapplication.MyService.onStartCommand(MyService.java:68) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3043) at android.app.ActivityThread.access$2300(ActivityThread.java:153)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:5527)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)  11-14 02:55:07.844 5906-5906/? E/MQSEventManagerDelegate: failed to get MQSService.

Here is the complete code if you need any more reference.

Upvotes: 2

Views: 5421

Answers (2)

David Wasser
David Wasser

Reputation: 95568

You are barking up the wrong tree ;-) Your Service shouldn't know anything about your UI, nor should it want to muck around with it. If you want to change the UI from your Service then you need to delegate that task to the Activity. Activity is responsible for the UI. There are several well documented approaches you can use:

  • Activity binds to Service and Service executes a callback on the Activity
  • Service sends a broadcast Intent and Activity registers a listener that reacts to the Intent when it is broadcast
  • Use an Event Bus implementation

Upvotes: 4

Chris Merrick
Chris Merrick

Reputation: 110

Services are NOT activities. Services have no UI related to them.

So:

1) you can't cast a service to an activity. 2) there is no UI for your to find in your service.

Why would a service need a button? services run in the background, with no user interaction.

I don't know what you are doing, but in Android it works like this:

1) activity launches a service (like your button press) 2) service carries out a task in the background 3) service reports back to the activity or terminates

Upvotes: 1

Related Questions