TheBrillowable
TheBrillowable

Reputation: 319

Why doesn't my service start?

I use an AlarmManager to start a service, but for some reason I can't figure out the service isn't starting.

The code in my onResume() for starting the service is:

Intent appIntent = new Intent(cxt, NotificationService.class);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            PendingIntent penIntent = PendingIntent.getService(this, 0,
                    appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.cancel(penIntent);

            penIntent = PendingIntent.getService(this, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 5);
            cal.set(Calendar.MINUTE, 00);
            cal.set(Calendar.SECOND, 00);

            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
                    10 * 1000, penIntent);

The Manifest contains the following:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
     android:debuggable="true"
     android:icon="@drawable/icon"
     android:label="@string/app_name" >
      <service
          android:name="de.brillow.mensa.Speiseplan$NotificationService"
          android:enabled="true" />

And finally the service itself looks like this:

public class NotificationService extends Service {

        private WakeLock mWakeLock;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        private void handleIntent(Intent intent) {
            // obtain the wake lock
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "wakelocktag");
            mWakeLock.acquire();

            Log.i("PowerManager", "acquired");

            // do the actual work, in a separate thread
            new PollTask().execute();
        }

        private class PollTask extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {
                // do stuff!
                Log.i("Background", "stuff");
                return null;
            }


            @Override
            protected void onPostExecute(Void result) {
                // handle your data
                int id = 001;
                Log.i("Noti", "fication");
                NotificationManager mNotifyMng = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        Speiseplan.this).setSmallIcon(R.drawable.icon)
                        .setContentTitle("Test").setContentText("Test!");

                mNotifyMng.notify(id, mBuilder.build());
                Log.i("Notification", "sent");
                stopSelf();
                Log.i("Service", "stopped");
            }
        }


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


        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            handleIntent(intent);
            return START_NOT_STICKY;
        }


        public void onDestroy() {
            super.onDestroy();
            mWakeLock.release();
        }
    }

For some reason neither the notification nor the Log.i-message shows up. I hope you can help.

Thanks in Advance!

Upvotes: 0

Views: 344

Answers (1)

Trinimon
Trinimon

Reputation: 13967

You shouldn't use an AsyncTask inside of a Service. AsyncTask should only be used for tasks that return a result into the current activity, i.e. it should be defined in an Activity or Fragment. That is what it is designed for.

In your case, you can put everything from doInBackground(...) and onPostExecute(...) into onCreate(...) of your service. Use a Thread if you want. If you need to visualize your service results in an Activity, invoke an Intent and pass any details into extras, e.g.

Invocation in service ...

Intent intent=new Intent(context,VisualizingActivity.class);
intent.putExtra("A", "Data A");
intent.putExtra("B", "Data B");
context.startActivity(intent);

Processing in activity ...

String A= intent.getStringExtra("A");
String B= intent.getStringExtra("B");

If you just want to create Notifications you can do that from the service directly. There's no need for an AsyncTask or Activity.

Hope this helps a bit ... Cheers!

Upvotes: 1

Related Questions