这就是我的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();
});
}
Run Code Online (Sandbox Code Playgroud)
RecorderService.java
public class RecorderService extends Service {
// Service code here
}
Run Code Online (Sandbox Code Playgroud)
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" /> …Run Code Online (Sandbox Code Playgroud)