尝试在我的Android APP中启动服务时遇到错误 我在暂停Activity之前启动了服务,所以我可以将一些信息上传到服务器。
java.lang.RuntimeException: Unable to instantiate service can't instantiate class ; no empty constructor
当我启动服务时出现问题:
protected void onPause() {
super.onPause();
EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname);
EditText emailText = (EditText) findViewById(R.id.EditText_Email);
String strNickname = nicknameText.getText().toString();
String strEmail = emailText.getText().toString();
SharedPreferences.Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
editor.putString(GAME_PREFERENCES_EMAIL, strEmail);
editor.commit();
//Antes de salir lanzamos una actividad nueva para hacer el upload de la info
Intent uploadService = new Intent(getApplicationContext(),UploaderService.class);
startService(uploadService);
}
然后,服务将启动AsyncTask
public class UploaderService extends Service{
public UploaderService(){
super();
}
private UpLoadUserData upLoadUserData;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
upLoadUserData = new UpLoadUserData();
upLoadUserData.execute();
Log.d(DEBUG_TAG, "Settings and image upload requested");
return START_REDELIVER_INTENT;
}
@Override
public IBinder onBind(Intent intent) {
// no binding
return null;
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbook.btdt.hour6"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="10"></uses-sdk>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:name=".QuizSplashActivity"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="QuizHelpActivity"></activity>
<activity
android:name="QuizMenuActivity"></activity>
<activity
android:name="QuizScoresActivity"></activity>
<activity
android:name="QuizSettingsActivity"></activity>
<activity
android:name="QuizGameActivity"></activity>
<service android:name="QuizSettingsActivity$UploaderService"></service>
</application>
</manifest>
答案 0 :(得分:2)
首先,您不能拥有普通嵌套类的Service
。它必须是static
嵌套类。
其次,Service
是static
Activity
的{{1}}嵌套类非常奇怪。
第三,让Service
产生AsyncTask
而非仅使用IntentService
非常奇怪。
第四,将getApplicationContext()
替换为this
。只有当您知道使用getApplicationContext()
的原因时才使用getApplicationContext()
,而您在此处不需要它。
答案 1 :(得分:0)
非常感谢!
我可以通过从IntentService而不是Service
扩展服务来成功启动服务 public static class UploaderService extends IntentService{
String DEBUG_TAG = UploaderService.class.getSimpleName();
SharedPreferences mGameSettings;
并在Manifest.xml中定义Intent服务
<service android:name=".QuizSettingsActivity$UploaderService"/>