android


How to stop Intent Service in middle of my program?


Calling stopService(Intent) does not work, even though I am pressing mStopButton.
MainActivity
public class MainActivity extends AppCompatActivity {
MyReceiver myReceiver = new MyReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(myReceiver, new IntentFilter("Start"));
Button mStartButton = (Button) findViewById(R.id.start_button);
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
}
});
Button mStopButton = (Button) findViewById(R.id.stop_button);
mStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
intent.setAction("stop");
stopService(intent);
}
});
}
private class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int res = intent.getIntExtra("count", -1);
Toast.makeText(MainActivity.this,"Count is " + res,Toast.LENGTH_SHORT).show();
}
}
}
MyIntentService
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
for (int i=0; i<=100; i++){
Intent intent1 = new Intent();
intent1.setAction("Start");
intent1.putExtra("count",i);
sendBroadcast(intent1);
Log.v("abc",""+i);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
How can I stop the IntentService?
You cant stop your IntentService that way. IntentService is stopped itself automatically.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
You can define a boolean isStop value in your IntentService and change the value when stop service button is clicked. You have to check the boolean in the for loop like:
// when click button
YourIntentService.isStop = true;
// in for loop
for (int i=0; i<=100; i++){
if(isStop) break;
Intent intent1 = new Intent();
intent1.setAction("Start");
intent1.putExtra("count",i);
sendBroadcast(intent1);
Log.v("abc",""+i);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
For best pratice, you should use LocalBroadCastReceiver instead of static boolean value.
Intent service stops by itself when work is done you don't need to explicitly stop it.

Related Links

Flex Mobile List
intent to share text to email clients (only email clients)
How to set android TabHost default view to be the second(or third) tab instead of the first tab to be the default view?
Java.net.UnknownHost Exception Android Download
Android multi touch gesture recognition
animation-list with 510 images causes OutOfMemoryError
why View class object showing null?
Broadcast receiver independent from the application? [closed]
Number Picker problems
What happens to the port when IP changes?
how to start a browser with yahoo search query in android
Call google TTS Cloud API on Android
android listview , the checkbox change random
Collision detection with rotated sprites, how to get correct bounding Rect?
change color of line in surfaceview and retain the color of the previous line
onClick inside fragment giving me NPE

Categories

HOME
go
amp-html
redux-form
obfuscation
gradient
replace
entity-framework-core
emscripten
nested-if
zip
jquery-ui
banner
retrofit2
lanczos
elasticsearch-5
32bit-64bit
openstreetmap
endeca
line
ycsb
symbol
modbus-tcp
dreamweaver
wolframalpha
identity-management
windows-xp
googlebot
parcelable
aggregation
angular-translate
complex-numbers
utorrent
wacom
yowsup
batching
numpy-broadcasting
xajax
openproject
git-rewrite-history
loading
metaprogramming
comparison-operators
magento-1.9.3
odoo-website
outputcache
facebook-social-plugins
monkey-testing
opencmis
httrack
sonarlint-eclipse
reportservice2010
launchctl
insight
aquamacs
ngmaterial
boo
filenet
polyline
vorpal.js
freepbx
angular-ui-typeahead
tracker-enabled-dbcontext
bizagi
contentcontrol
design-principles
directdraw
computer-architecture
oracle-spatial
lasso
zscript
sql-server-data-tools
pyobjc
public-html
mruby
structuremap3
tweenlite
mdichild
gae-quotas
ie8-compatibility-mode
tlf
workitem
route-provider
onload-event
curb
os.system
virtualpathprovider
convention
procedures
django-paypal
collectionviewsource
cardspace
wspbuilder

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App