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