这是我的manifest
文件。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".DelayedMessageService"
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>
</application>
尽管服务活动已放入清单文件中,但发生了此异常
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.joke, PID: 11392
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.hfad.joke/com.hfad.joke.DelayedMessageService}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4544)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:4905)
at android.app.Activity.startActivity(Activity.java:4873)
at com.hfad.joke.MainActivity$1.onClick(MainActivity.java:24)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
此按钮代码转到DelayedMessageService
。
Button serviceButton = (Button)findViewById(R.id.button);
serviceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,DelayedMessageService.class);
i.putExtra(DelayedMessageService.EXTRA_MESSAGE,getResources().getText(R.string.button_response));
startActivity(i);
}
});
这是服务活动。
public class DelayedMessageService extends IntentService {
public static final String EXTRA_MESSAGE = "message";
public DelayedMessageService() {
super("DelayedMessageService");
}
@Override
protected void onHandleIntent(Intent intent) {
try{
synchronized (this){
wait(10000);
}
}catch (InterruptedException ie){
ie.printStackTrace();
}
String text = intent.getStringExtra(EXTRA_MESSAGE);
showText(text);
}
private void showText(final String text) {
Log.v("DelayedMessageService", "The message is: " + text);
}
}