java作为一个服务类,我看到了安卓。所容纳之物ActivityNotFoundException:找不到显式活动类
我正在使用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?
我确实检查了我的安德里奥德曼尼斯特。xml和我确实看到了服务声明
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.serviceexample">
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="ServiceExample"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<service
安卓:name=".MyIntentService"
安卓:exported="false"></service>
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service 安卓:name=".MyBgService"/>
</application>
</manifest>
主要活动。爪哇:
package com.example.serviceexample;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.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);
}
});
}
我的后台服务:
package com.example.serviceexample;
import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.os.IBinder;
import 安卓.widget.Toast;
import 安卓x.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;
}
}
我确实试过在AndroidManifest中链接路径。xml。但仍然会出现同样的错误。 不确定是什么问题
这是Gradle的版本:3.5.3
,我使用的是SDK版本29
任何帮助都将不胜感激
# 1 楼答案
基本上一按按钮,你就开始服务了。 而不是这个
使用