Reputation: 595
i have used two services in single Activity in Android and start the services when my activity is start. I'm trying to downloading some images in onStartCommand() in Both services. But right now i want to stop the services automatically or stop it self after onStartCommand() finished his work.How do I stop service it self after finishing the onStartCommand().Thanks !!
This start service in onCreate() in Activity
Intent intent_Service = new Intent(CustomActionActivity.this , DownLodProfileSrvice.class);
startService(intent_Service);
Intent intent = new Intent(CustomActionActivity.this, MyService.class);
startService(intent);
Here is my service code
public class DownLodProfileSrvice extends Service
{
protected SQLiteDatabase db;
public Runnable mRunnable=null;
MyDbHelper myDBHelper;
String namespace="http://103.24.4.60/xxxxx/xxxxx.svc";
File newFolder;
public ImageLoader imageLoader;
DisplayImageOptions options;
String str_Authentication_Token,result;
public DownLodProfileSrvice(){
}
@Override
public IBinder onBind(Intent intent){
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate(){
super.onCreate();
Log.e("TAG","ScreenListenerService---OnCreate ");
myDBHelper=new MyDbHelper(this);
myDBHelper.onOpen(db);
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
final Handler mHandler=new Handler();
mRunnable=new Runnable(){
@Override
public void run()
{
new LoadProfilePic().execute();
mHandler.postDelayed(mRunnable,10*1000);
}
};
mHandler.postDelayed(mRunnable, 10 *1000);
return super.onStartCommand(intent,flags,startId);
}
private class LoadProfilePic extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
LoadProfilePics();
return null;
}
}
public void LoadProfilePics() {
db = myDBHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from ALL_Post ", null);
if (cursor.moveToFirst()) {
do {
String strProfile_Pics = cursor.getString(cursor.getColumnIndex("ProfilePICURL"));
String strDownLoadStatus = cursor.getString(cursor.getColumnIndex("DownLoad_Status"));
if(strDownLoadStatus.equals("0"))
{
String URL_downLoadProfilePic = namespace + "/DownloadFile/FilePathMobile/PROFILEPICPATH/FileName/" + strProfile_Pics;
newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classNKK_ProfilePic");
download_PngFileImgLoader(URL_downLoadProfilePic, newFolder, strProfile_Pics);
db.execSQL("update ALL_Post set DownLoad_Status='1' where ProfilePICURL ='" + strProfile_Pics + "'");
Log.e("URL_downLoadProfilePic ", " ==========>" + URL_downLoadProfilePic);
}
} while (cursor.moveToNext());
}
cursor.close();
}
}
Upvotes: 0
Views: 188
Reputation: 329
Actually you don't need a service for downloading a file!
Services are a tool for performing long running tasks which is not your case. The best practice for downloading a file is to just use an AsyncTask
.
Move LoadProfilePics
to your activity and execute it just once.
Also you can check the result of doInBackground
of the AsyncTask in onPostExecution
.
See this link for more information on AsyncTask
Upvotes: 0
Reputation: 542
The service can call stopSelf() when it is done. If you use IntentService, this will happen automatically.
Upvotes: 1