ActivityNotFoundException: Unable to find explicit activity class service

天大地大妈咪最大 提交于 2020-01-15 15:29:28

问题


This is how my MainActivity and RecorderService Java class files looks like. I'm adding the AndroidManifest file too.

MainAcitivity.java

protected void onCreate(Bundle savedInstanceState) {

      btnSendSOS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSMS();

                Intent intent = new Intent(getBaseContext(), RecorderService.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                finish();

                new CountDownTimer(5000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    stopService(new Intent(getApplicationContext(), RecorderService.class));
                }
            }.start();
      });


}

RecorderService.java

public class RecorderService extends Service {
     // Service code here
}

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:enabled="true"
            android:name=".RecorderService" />
</application>

I have added the service to the Manifest file. But still I'm getting the error: android.content.ActivityNotFoundException: Unable to find explicit activity class {.RecorderService}; have you declared this activity in your AndroidManifest.xml?

Please help me out. Thanks in advance


回答1:


RecorderService is Service. is not an Activity. And Start Service like

  Intent i = new Intent(MainActivity.this, RecorderService.class);
  startService(i);

Go to Official Docs for more information regard Services in Android



来源:https://stackoverflow.com/questions/29485078/activitynotfoundexception-unable-to-find-explicit-activity-class-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!