I have an app that invokes a service in the background from an alarm:

        // Ok, let's try running the service via a repeating alarm...
        // We need to make a PendingIntent from the service intent...
        Intent si = new Intent(this, xxxService.class);
PendingIntent pi = PendingIntent.getService(this, 0, si, PendingIntent.FLAG_UPDATE_CURRENT);

// Set the first time the alarm should trigger to be an hour ago in milliseconds...
        long when = new Date().getTime() - 360000;
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarm.setInexactRepeating(alarm.RTC_WAKEUP, when, alarm.INTERVAL_HOUR, pi);

It does a web service call approximately every hour after the app is run the first time. Because it uses the Inexact method it is likely the phone is doing something else also. The user never sees it run.

If you are launching an Activity that needs the screen then I think that is when you need the NEW_ACTIVITY_FLAG? I have the following in use to start an activity that displays a record selected on a list:

            // display the whole record in its own activity.
            Intent detailIntent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse(Sample.CONTENT_URI+"/"+id),
                                ctx,
                                xxxActivity.class);
            detailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                ctx.startActivity(detailIntent);
            }
            catch (Exception e) {
                String foo = e.getMessage();
                Log.e("Caught It:",foo);
            }

I'm not yet sure I'm clear myself on when the flag is required.

On 1/29/2012 1:03 PM, Kostya Vasilyev wrote:
Correct.

Keep in mind though, that, except for some specific cases (an alarm clock and the like), this -- an activity popping out of nowhere -- can be an unexpected and jarring experience for the user.

I can see it as a plot device for Final Destination 6: someone's bleeding to death, trying to call 911 on his Android phone, and is interrupted by a popup window: "5 days to Jane's birthday". Cut to black.

:)

-- Kostya

29 января 2012 г. 21:41 пользователь Rudolf Polzer <[email protected] <mailto:[email protected]>> написал:

    The LogCat contents after the crash is

    ERROR/AndroidRuntime(19994): java.lang.RuntimeException: Unable to
    start receiver irp.plan.OnAlarmReceiver:
    android.util.AndroidRuntimeException: Calling startActivity() from
    outside of an Activity  context requires the
    FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    ...
    ERROR/AndroidRuntime(19994): Caused by:
    android.util.AndroidRuntimeException: Calling startActivity() from
    outside of an Activity  context requires the
    FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    So I guess I have to add the FLAG_ACTIVITY_NEW_TASK flag to the
    intent to start the activity, because the startActivity() is
    called from inside the message receiver OnAlarmReceiver.


    Am 29.01.2012 16:09, schrieb Kostya Vasilyev:

        Press F8 (Resume) in Eclipse a few times until you get the
        crash dialog
        on the device.

        Then check the logcat.

        Look for the part after "Caused by:"

        -- Kostya

        29.01.2012 15:21, Rudolf Polzer пишет:

            The manifest file contains
            <activity android:name=".WakeActivity"></activity>
            within the<application> tag.

            The stack is
            ActivityThread.handleReceiver(ActivityThread$ReceiverData)
            line:
            2639
            ActivityThread.access$3100(ActivityThread, ActivityThread
            $ReceiverData) line: 119
            ActivityThread$H.handleMessage(Message) line: 1913
            ActivityThread$H(Handler).dispatchMessage(Message) line: 99
            Looper.loop() line: 123



-- You received this message because you are subscribed to the Google
    Groups "Android Developers" group.
    To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:android-developers%[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/android-developers?hl=en


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to