Wie Prüfen ob ein Service bereits läuft ?

  • Antworten:3
Mac Systems
  • Forum-Beiträge: 1.727

21.07.2009, 18:00:00 via Website

Installiert man eine APP z.b über den Market greifen Mechanismen wie ein BootCompleted BroadcastReciever nicht, wie stelle Ich denn fest das ein erwarteter Service bereits läuft um ihn ggf. zu starten ?

Danke,
mac

— geändert am 22.07.2009, 05:47:47

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

21.07.2009, 18:37:40 via Website

Die Frage hätte Ich mir wohl sparen können:

1final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
2 final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
3
4 boolean isServiceFound = false;
5
6 for (int i = 0; i < services.size(); i++)
7 {
8 Log.d(LOG_TAG, "Service Nr." + i + ":" + services.get(i).service);
9
10 if ("de.macsystems.windroid".equals(services.get(i).service.getPackageName()))
11 {
12 if ("SpotService".equals(services.get(i).service.getClassName()))
13 {
14 isServiceFound = true;
15 }
16 }
17 }
18
19 if (!isServiceFound)
20 {
21 Log.d(LOG_TAG, "Starting Service");
22 final Intent startServiceIntent = new Intent();
23 startServiceIntent.setAction("de.macsystems.windroid.START_SPOT_SERVICE_ACTION");
24 // startService(startServiceIntent);
25 }
26 else
27 {
28 Log.i(LOG_TAG, "Service already running !!!!!");
29 }



PS: Euere Vorschau macht wohl den Codeformatter sorgen, kein wunder das hier so wenig passiert!

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

22.07.2009, 05:44:36 via Website

Update nach harter Nacht:

Geändert hat sich letztendlich nur die zweite Bedingung. Mir fiel das anfangs nicht auf, somit startete der Service immer :mad:

1private void isServiceRunning()
2 {
3 final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
4 final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
5
6 boolean isServiceFound = false;
7
8 for (int i = 0; i < services.size(); i++)
9 {
10 Log.d(LOG_TAG, "Service Nr. " + i + " :" + services.get(i).service);
11
12 Log.d(LOG_TAG, "Service Nr. " + i + " package name : " + services.get(i).service.getPackageName());
13 Log.d(LOG_TAG, "Service Nr. " + i + " class name : " + services.get(i).service.getClassName());
14
15 if ("de.macsystems.windroid".equals(services.get(i).service.getPackageName()))
16 {
17 Log.d(LOG_TAG, "packagename stimmt überein !!!");
18 // Log.d(LOG_TAG, "SpotService" + " : " +
19 // services.get(i).service.getClassName());
20
21 if ("de.macsystems.windroid.SpotService".equals(services.get(i).service.getClassName()))
22 {
23 Log.d(LOG_TAG, "getClassName stimmt überein !!!");
24
25 isServiceFound = true;
26 }
27 }
28 }

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

29.07.2009, 06:24:29 via Website

Hab da eine weiter möglichkeit gefunden, allerdings wird der Service gestartet sollte er nicht laufen.
Sollte das ComponentName null sein, ging wohl wirklich was schief

1final Intent startServiceIntent = new Intent();
2 startServiceIntent.setAction("de.macsystems.windroid.START_SPOT_SERVICE_ACTION");
3 final ComponentName name = _context.startService(startServiceIntent);
4 if (name == null)
5 {
6 Log.e(LOG_TAG, "Failed to start SpotService.");
7 }
8 else
9 {
10 Log.i(LOG_TAG, "SpotService on boot launched.");
11 }

ComponentName android.content.Context.startService(Intent service)

public abstract ComponentName startService (Intent service)

Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

Every call to this method will result in a corresponding call to the target service's onStart(Intent, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

Using startService() overrides the default service lifetime that is managed by bindService(Intent, ServiceConnection, int): it requires the service to remain running until stopService(Intent) is called, regardless of whether any clients are connected to it. Note that calls to startService() are not nesting: no matter how many times you call startService(), a single call to stopService(Intent) will stop it.

The system attempts to keep running services around as much as possible. The only time they should be stopped is if the current foreground application is using so many resources that the service needs to be killed. If any errors happen in the service's process, it will automatically be restarted.

This function will throw SecurityException if you do not have permission to start the given service.
Parameters
service Identifies the service to be started. The Intent may specify either an explicit component name to start, or a logical description (action, category, etc) to match an IntentFilter published by a service. Additional values may be included in the Intent extras to supply arguments along with this specific start call.
Returns

* If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

Throws SecurityException
See Also

* stopService(Intent)
* bindService(Intent, ServiceConnection, int)[/code]

— geändert am 29.07.2009, 06:25:32

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten