0

I am trying to follow the example given here on running some code in fixed time intervals. I used the code in this example (setting the waiting time to 1 minute instead), compiled it, started in on my phone, stopped the application on the phone, and waited for several minutes.

However, no Toast was shown and neither any log-entry. I also notice that the Service.onStart seems depreciated.

Is something missing in the example? Do I need to register something when the main activity of the app is started? Or call it from somewhere else in my app? Or maybe something is wrong in the manifest?

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage.test" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application>
        android:allowBackup="true"
        android:icon="@mipmap/stoxx"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".NewEntryActivity"
            android:label="@string/menu_add"
            android:theme="@style/AppTheme.NoActionBar"
        />
        <activity
            android:name=".UserSettingActivity"
            android:label="@string/menu_add"
            android:theme="@style/AppTheme.NoActionBar"
            />
        <receiver
            android:process=":remote"
            android:name=".Alarm">
        </receiver>

    </application>

</manifest>
Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469
  • How did you stop the app? What version of Android are you running it under? Btw, your first `` tag is closed prematurely. – Mike M. Nov 22 '15 at 08:37
  • There is a button on the phone which says "Clear all", which I suppose is to stop all running apps. I updated the Question with hopefully the correct closing tag... – Alex Nov 22 '15 at 08:40
  • 1)The `>` goes after the `theme` attribute. 2) If you mean "Close all", then you're forcibly stopping the app, and your alarms will be cleared. 3) If you're running on KitKat or above, `setRepeating()` is no longer exact. Check the docs for [`AlarmManager`](http://developer.android.com/intl/es/reference/android/app/AlarmManager.html). – Mike M. Nov 22 '15 at 08:47
  • I already started to read the documentation. But it does not explain HOW to implement this or HOW to call something! I can create a class derived from `AlarmManager` - but then what? Also, I want some code being called even if I kill the app. For example, I can kill my mail app. But still I get a notification when I receive an email. How does this work? – Alex Nov 22 '15 at 08:50
  • Is the manifest correct now? – Alex Nov 22 '15 at 08:55
  • 1
    Sure it does: "If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above." It means use `setExact()`, and when that fires, set it again for the desired interval. Also, your email app doesn't use `AlarmManager`. And, no, remove the `>` directly after ` – Mike M. Nov 22 '15 at 08:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95830/discussion-between-alex-and-mike-m). – Alex Nov 22 '15 at 09:03

1 Answers1

2

You forgot to include service in your Manifest. Here is all this project that works for me, check it.

Here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager.just_me.alarmmanager">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:allowBackup="true" android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher" android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:process=":remote" android:name=".Alarm"/>
    <service android:name=".MyService"/>
</application>

Here is Alarm:

public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Toast.makeText(context, "com.alarmmanager.just_me.alarmmanager.Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

    wl.release();
}

public void SetAlarm(Context context)
{
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi); // Millisec * Second
}

public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}}

Here is service:

public class MyService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.d("!~!", "Service started.");
        alarm.SetAlarm(this);
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        alarm.SetAlarm(this);
    }

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

And the last one MainActivity:

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
}
RevakoOA
  • 601
  • 1
  • 9
  • 20