简体   繁体   中英

For a Service class I see android.content.ActivityNotFoundException: Unable to find explicit activity class

I am working with Android and facing the following error when I create a service and run it from the main activity thread. I did go through all the threads related to this error but the error they see mostly for the Activity task. Below are the details for error and code

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.serviceexample/com.example.serviceexample.MyServiceBg}; have you declared this activity in your AndroidManifest.xml?

I did check my AndriodManifest.xml and I do see the service declared

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="ServiceExample"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".MyIntentService"
            android:exported="false"></service>

        <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=".MyBgService"/>
    </application>

</manifest>

MainActivity.java:

package com.example.serviceexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button btn = (Button)findViewById(R.id.displayBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent displayTost  = new Intent(MainActivity.this, MyBgService.class);
                startActivity(displayTost);
            }
        });
    }

My background service:

package com.example.serviceexample;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyBgService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int i;
        for (i = 1; i< 10; i++) {
            Toast.makeText(getApplicationContext(), "Number = "+i, Toast.LENGTH_LONG).show();
        }
        return Service.START_NOT_STICKY;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

I did try chaining the path in the AndroidManifest.xml. But still, getting the same error. Not sure what is the issue.

Here is the version of Gradle: 3.5.3 and I am using SDK version 29

Any help will be highly appreciated.

Basically on button click, you are starting service. Instead of this

startActivity(displayTost);

use

startService(new Intent(MainActivity.this, MyBgService.class));

MyBgService is a service . It is not an Activity. You can't do as

startActivity(displayTost);

instead of doing

startService(displayTost);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM