私のアプリケーションはバッテリーを消耗します.リクエストがurlに送信されるサービスを使用しました.サービスは、Application.Belowが私のコードを拡張する私のクラスから呼び出されます.
public class GApplication extends Application {
private static final String TAG ="GApplication";
private HttpClient httpClient;
private DatabaseHelper databaseHelper;
@Override
public void onCreate(){
super.onCreate();
startService(new Intent(this, GService.class));
httpClient = createHttpClient();
databaseHelper = new DatabaseHelper(this);
}
@Override
public void onLowMemory(){
super.onLowMemory();
shutdownHttpClient();
}
@Override
public void onTerminate(){
super.onTerminate();
stopService(new Intent(this, GService.class));
shutdownHttpClient();
databaseHelper.close();
}
private void shutdownHttpClient(){
if(httpClient != null && httpClient.getConnectionManager() != null){
httpClient.getConnectionManager().shutdown();
}
}
public DatabaseHelper getDatabaseHelper(){
if(databaseHelper == null){
databaseHelper = new DatabaseHelper(this);
}
return databaseHelper;
}
public HttpClient getHttpClient(){
return httpClient;
}
public HttpClient createHttpClient(){
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));
return new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
}
public boolean isOnline(){
boolean isConnected = false;
try{
ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
isConnected = (info != null && info.isAvailable() && info.isConnected());
}
catch(Exception e){
isConnected = false;
if(e.getMessage() != null) Log.e(TAG, e.getMessage());
}
return isConnected;
}
}
私のGserviceクラス
public class GService extends Service {
private static final String TAG = "Gservice";
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
super.onCreate();
Log.i(TAG, "starting GService");
if(isOnline()){
URI uri = URI.create("http://myserver/Android/UploadImage/newAlert.php");
new UpdateCheckAsyncTask(getHttpClient()).execute(uri);
}
}
boolean isOnline(){
return ((GApplication)getApplication()).isOnline();
}
HttpClient getHttpClient(){
return ((GApplication)getApplication()).getHttpClient();
}
DatabaseHelper getDatabaseHelper(){
return ((GApplication)getApplication()).getDatabaseHelper();
}
class UpdateCheckAsyncTask extends WebAsyncTaskBase{
public UpdateCheckAsyncTask(HttpClient httpClient) {
super(httpClient);
}
protected String doInBackground(URI... params) {
return getHttpContent(params[0]);
}
protected void onProgressUpdate(Integer... progress){
}
protected void onPostExecute(String result){
if(result == null){
Log.i(TAG, "Call returned null");
return;
}
try {
Log.i(TAG, "Processsing request");
JSONObject json = new JSONObject(result);
new BlogDbAsyncTask(getDatabaseHelper()).execute(json);
} catch (JSONException e) {
if(e.getMessage() != null) Log.e(TAG, e.getMessage());
}
}
class BlogDbAsyncTask extends DbAsyncTaskBase<JSONObject, Boolean, BlogInfo>{
public BlogDbAsyncTask(DatabaseHelper database) {
super(database);
}
@Override
protected BlogInfo doInBackground(JSONObject... json) {
BlogInfo blogInfo = new BlogInfo();
BlogDAO dao = new BlogDAO(GService.this, getDatabaseHelper());
try {
Log.i(TAG, "Adding new blog entry");
Blog blog = dao.Select(json[0].getInt("FeedId"));
if(blog.UID == null){
blog.UID = json[0].getInt("FeedId");
blog.Text = json[0].getString("Text");
blog.Title = json[0].getString("Header");
blog.PostedOn = json[0].getString("DisplayDate");
blog.PostedBy = "Gservice";
dao.Insert(blog);
blogInfo.Blog = blog;
blogInfo.IsNew = true;
}
} catch (JSONException e) {
if(e.getMessage() != null) Log.e(TAG, e.getMessage());
}
return blogInfo;
}
protected void onPostExecute(BlogInfo result){
}
}
class BlogInfo{
public Blog Blog;
public boolean IsNew;
}
}
おそらく、サービスを継続的に実行する GApplication クラスで Onterminate メソッドが呼び出されることはありません。