我已经使用Android Studio提供服务,但是当我点击我设置的按钮启动服务时,该服务无法启动。这是我的代码,我在getInfo()方法中启动服务:
import ...
public class BasicInformation extends AppCompatActivity {
EditText name;
EditText height;
EditText weight;
private String nameStr;
private String heightStr;
private String weightStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_information);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
boolean firstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isfirstrun", true);
if (firstRun) {
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isfirstrun", false).commit();
} else {
Intent launchHome = new Intent(this, MainActivity.class);
startActivity(launchHome);
}
}
public void getInfo(View v) {
// This where I want to start the service
Intent service = new Intent(getApplicationContext(), StepCounterService.class);
startService(service);
name = (EditText) findViewById(R.id.input_name);
height = (EditText) findViewById(R.id.input_height);
weight = (EditText) findViewById(R.id.input_weight);
nameStr = name.getText().toString();
heightStr = height.getText().toString();
weightStr = weight.getText().toString();
Intent goToMain = new Intent(this, MainActivity.class);
startActivity(goToMain);
}
}
这是我的StepCounterService:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class StepCounterService extends Service {
public StepCounterService() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Step Counting service started", Toast.LENGTH_LONG);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG);
}
}
我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nina.stepcounter3">
<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">
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:screenOrientation="nosensor"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".BasicInformation"
android:label="@string/title_activity_basic_information"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".StepCounterService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>