ActivityNotFoundException: Unable to find explicit

2019-08-05 09:24发布

问题:

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