这是我的MainActivity和RecorderService Java类文件的样子。我也在添加AndroidManifest文件。
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>
我已将该服务添加到Manifest文件中。但我仍然收到错误:android.content.ActivityNotFoundException: Unable to find explicit activity class {.RecorderService}; have you declared this activity in your AndroidManifest.xml?
请帮帮我。提前致谢
答案 0 :(得分:6)
RecorderService
服务。不是Activity
。 Start Service
喜欢
Intent i = new Intent(MainActivity.this, RecorderService.class);
startService(i);
转到Official Docs了解有关 Android
中Services
的更多信息