简体   繁体   English

对于服务类,我看到 android.content.ActivityNotFoundException: Unable to find explicit activity class

[英]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.我正在使用 Android 并在创建服务并从主活动线程运行它时遇到以下错误。 I did go through all the threads related to this error but the error they see mostly for the Activity task.我确实浏览了与此错误相关的所有线程,但他们看到的错误主要是针对 Activity 任务。 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我确实检查了我的 AndriodManifest.xml,我确实看到了声明的服务

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:主活动.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.我确实尝试在 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这是 Gradle 的版本: 3.5.3 ,我使用的是 SDK 版本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 . MyBgService是一个服务 It is not an Activity.它不是一个活动。 You can't do as你不能这样做

startActivity(displayTost);

instead of doing而不是做

startService(displayTost);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.ActivityNotFoundException:无法在Android中找到明确的活动类 - android.content.ActivityNotFoundException: Unable to find explicit activity class in Android android.content.ActivityNotFoundException:无法找到明确的活动类 - android.content.ActivityNotFoundException: Unable to find explicit activity class android.content.ActivityNotFoundException:无法找到显式活动 class {您是否在 AndroidManifest.xml 中声明了此活动? - android.content.ActivityNotFoundException: Unable to find explicit activity class { have you declared this activity in your AndroidManifest.xml? android.content.ActivityNotFoundException:无法找到显式活动类; 您是否在 AndroidManifest.xml 中声明了此活动? - android.content.ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml? Android:ActivityNotFoundException无法找到明确的活动类 - Android: ActivityNotFoundException Unable to find explicit activity class android.content.ActivityNotFoundException,无法开始活动 - android.content.ActivityNotFoundException ,unable to start activity ActivityNotFoundException:无法找到显式活动类? - ActivityNotFoundException: Unable to find explicit activity class? ActivityNotFoundException:无法找到显式活动类 - ActivityNotFoundException: Unable to find explicit activity class ActivityNotFoundException:无法找到显式活动类GoogleDriveProxeyActivity是否已声明此内容 - ActivityNotFoundException: Unable to find explicit activity class GoogleDriveProxeyActivity have you declared this Android错误:“ android.content.ActivityNotFoundException:未找到用于处理Intent的活动” - Android error : “android.content.ActivityNotFoundException: No Activity found to handle Intent”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM