• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How can I stop my Widget/Service??

Shelly22

Newbie
Hello Forum.

How can I stop my widget / service if I take it from the home screen.
If I repeat it, then it is started twice.

But I do not get it stopped.
I want its start new, when I put it on the home screen.

Here the code.

[HIGH]
public class AppWidget extends AppWidgetProvider {


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);

Intent intent = new Intent(context, UpdateServiceForAppWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
intent.setAction("de.mk.android_motionwidget.SECONDS_UPDATE");

context.startService(intent);

}
}
[/HIGH]
And the Service
[HIGH]
public class UpdateServiceForAppWidgetProvider extends Service {

@Override
public void onStart(Intent intent, int startId) {

// <<<< What here is stopp please. But how?

}
super.onStart(intent, startId);
}
}
[/HIGH]

So please. How can i stop this service for my widget. If I take it from my screen.
 
First, you need to decide what event(s) should trigger the service to stop. Then, in each of these situations, simply call stopService(new Intent(context, UpdateServiceForAppWidgetProvider.class));
 
If you look at this site as reference:
http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html#onDeleted(android.content.Context, int[])

There is a callback for onDeleted() just like you have for onUpdate(). Inside AppWidget override the onDeleted method and implement the stopService code jonbonazza mentions.

This should get you the behavior you expect.

One thing to consider is if there are multiple widgets, do you want the service to stay going if one is removed.

Hope this helps!
 
I tried it. But it is not work. My Timer and TimerTask continue again if I have the widget views again on the screen. But it should restart. Not continue.
 
I tried it. But it is not work. My Timer and TimerTask continue again if I have the widget views again on the screen. But it should restart. Not continue.

I would look into using the AlarmManager (api link) to schedule repeating events. This also has a cancel method that would stop the repeating once you called that method on your onDelete.
 
This should work?
My problem is. I want to repeat an animation showing the TimerTask.
Don't stop the animation. A infinity. Its work.
But...When i stopped.
So he stops everything.
But not the TimerTask.


[HIGH]
public class TimerEndlos extends TimerTask{

@Override
public void run() {

handler.postDelayed(new B1(), 0000);
handler.postDelayed(new B2(), 2000);

}
}
[/HIGH][HIGH]
public class Hintergrund extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... params) {

timer.scheduleAtFixedRate(new TimerEndlos(), 0, 5000);
return null;
}
}
[/HIGH][HIGH]
public Handler handler = new Handler();
public Timer timer = new Timer();

@Override
public void onStart(Intent intent, int startId) {

new Hintergrund().execute();

super.onStart(intent, startId);
}

@Override
public void onDestroy() {
super.onDestroy();

stopService(new Intent(getApplicationContext(),UpdateServiceForAppWidgetProvider.class));

}
[/HIGH]

Now I know the problem. The timer starts.
Whether the widget I have on the front of the display or not. He starts in the background.
So how can I prevent this in the service? He should start, if I have the widget on the screen.
 
I have another idea. Timer sucks.
The timer is stopped (timer.cancel ()), when he ran it 1x.
But it must immediately.

You know how to put animations in the widget?
Do you know the app: AndroGochi?
The app has a widget. The widget has animations.
How does it work?

And you know how can i stop the service, If I turn off the screen?

Thanks shredcode that you help me again.
 
So i have it. I take a ViewFlipper. Very nice.
But the problem is..Some phones cannot show many pictures the ViewFlipper.
Max 10. On my Phone (Sony Xperia Z) can more.

Or have you another idea for animation in widget?
 
I have another idea. Timer sucks.
The timer is stopped (timer.cancel ()), when he ran it 1x.
But it must immediately.

You know how to put animations in the widget?
Do you know the app: AndroGochi?
The app has a widget. The widget has animations.
How does it work?

And you know how can i stop the service, If I turn off the screen?

Thanks shredcode that you help me again.

I was looking into a screen off event a little while ago for an app i was writing. this is one site i came across:

Handling Screen OFF and Screen ON Intents | Think Android
 
Thank you very much.

So i have now a viewflipper for my animation.
And its very nice. My app is on the Google Store.
You can test it if you like.

Look at "AndroidMotion-Widget".
 
Back
Top Bottom