praneel
praneel

Reputation: 241

AlarmManager not working in Android wear

I am trying to invoke AlarmManager from my Android wear project. But for some reasons its failing. Following is my code for the same. I am sure I am calling setAlarm method in AlarmReceiver (my log is printing) but after that nothing happens. My aim is run the alarm every 30 secs. When the AlarmReceiver onReceive is called. SendBroadcast to my Service, which in turn will send the message to server. What am I doing wrong here.

Manifest Details:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Within Application

<receiver android:name=".alarm.AlarmReceiver" />
        <receiver
            android:name=".alarm.SampleBootReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

AlarmManager:

public class AlarmReceiver extends WakefulBroadcastReceiver {

    private static final String TAG = "AlarmReceiver";

    // The app's AlarmManager, which provides access to the system alarm services.
    private AlarmManager alarmMgr;
    // The pending intent that is triggered when the alarm fires.
    private PendingIntent alarmIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"AlarmReceiver onReceive*******");
        Intent sensorIntent = new Intent("sensor");
        LocalBroadcastManager.getInstance(context).sendBroadcast(sensorIntent);
    }

    public void setAlarm(Context context) {
        Log.d(TAG,"Alarm Manager started");
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                System.currentTimeMillis()+1000,60000,alarmIntent);

        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

    }
}

SampleBootReceiver:

public class SampleBootReceiver extends BroadcastReceiver {
    AlarmReceiver alarm = new AlarmReceiver();
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarm.setAlarm(context);
        }
    }
}

Upvotes: 2

Views: 845

Answers (2)

Ken Wolf
Ken Wolf

Reputation: 23269

Beginning with API level 19 Alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use.

This is especially true on wearable devices when in low power mode.

To guarantee delivery use setExactAndAllowWhileIdle

Like setExact(int, long, PendingIntent), but this alarm will be allowed to execute even when the system is in low-power idle modes. If you don't need exact scheduling of the alarm but still need to execute while idle, consider using setAndAllowWhileIdle(int, long, PendingIntent).

Upvotes: 1

Mr.Rebot
Mr.Rebot

Reputation: 6791

You may want to check Creating and Running a Wearable App and Packaging Wearable Apps to ensure you are able to pair your handheld to your wear device.

In wear development, proper communication/pairing between handheld and wearable device is important in sending and receiving messages. Make use of MessageApi, to send message to the wearable to start an activity. To be notified of received messages, implement the MessageListener interface to provide a listener for message events.

You can also see my answer here and another related SO question here. Also here is a tutorial on Wearable Notification and Android Wear Development.

Hope it helps!

Upvotes: 0

Related Questions