0

In my application, I am trying to start a service using AlarmManager. The code for AlarmManager is below:

public class Main extends Activity {

        ...

        @Override
        protected void onStart() {
            super.onStart();

            Intent intent = new Intent(this, MyService.class);
            PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5*1000, 5*1000, pintent);
        }

        ...
}

below is the Service Class:

public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "MyService.onCreate()", Toast.LENGTH_LONG).show();
    }

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

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

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "MyService.onStart()", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "MyService.onStartCommand()", Toast.LENGTH_LONG).show();
    }
}

and the Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.max.android.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.max.android.myapp.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <service android:enabled="true" android:name="com.max.android.myapp.SubsService" />
</manifest>

running adb shell dumpsys alarm I can see my alarm:

  RTC_WAKEUP #0: Alarm{41878860 type 0 com.max.android.myapp}

    type=0 when=+3s954ms repeatInterval=5000 count=1

    operation=PendingIntent{41878850: PendingIntentRecord{41878760 com.max.android.myapp startService}}

But MyService is never started. Why is this not working?

Mauker
  • 11,237
  • 7
  • 58
  • 76
Max
  • 262
  • 2
  • 8
  • I would expect some sort of a clue in the `adb logcat` output. – Richard Le Mesurier Aug 20 '15 at 15:21
  • 1
    Bingo! `W/ActivityManager( 1506): Unable to start service Intent { flg=0x4 cmp=com.max.android.myapp/.SubsService (has extras) }: not found` any suggestion? – Max Aug 20 '15 at 15:31
  • @RichardLeMesurier thanks for your help.... I haven't seen this logcat entry in eclipse logcat viewer... the problem was in Android Manifest as pointed in [link here](http://stackoverflow.com/questions/4570743/unable-to-start-service-intent-error-when-starting-service-from-an-activity-in) – Max Aug 20 '15 at 15:44

2 Answers2

0

Solved. After looking at adb logcat I've found this warning: W/ActivityManager( 1506): Unable to start service Intent { flg=0x4 cmp=com.max.android.myapp/.SubsService (has extras) }: not found In AndroidManifest.xml, I had the < service> declaration after < application> instead of nested inside it.

Max
  • 262
  • 2
  • 8
-1

SystemClock.elapsedRealtime

Returns milliseconds since boot, including time spent in sleep.

You can get current time in milliseconds by calling

new Date().getTime()

see https://developer.android.com/training/scheduling/alarms.html

Kirill
  • 7,580
  • 6
  • 44
  • 95
  • The alarm is scheduled correctly: `type=0 when=+3s954ms repeatInterval=5000 count=1` I think the problem is not here – Max Aug 20 '15 at 15:35