Develop Alarm Application
Asked Answered
M

4

13

I'd like develop an Alarm Application. The application should work like this:

  • launch it
  • the activity show me the time
  • I can set the alarm
  • I can close the application
  • when the alarm time comes , it starts an activity (even if the device is locked)

I have tried to adapt this sample https://github.com/commonsguy/cwac-wakeful but I cannot launch an activity when the alarm time comes.

I use this code to setup the alarm (for test I have inserted this code on an onCreate method of activity):

Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);

this is the OnAlarmReceiver class:

public class OnAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
        WakefulIntentService.sendWakefulWork(context, AlarmService.class);
    }
}

this is the service class:

public class AlarmService extends WakefulIntentService {

    public AlarmService(String name) {
        super(name);
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
        Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.setAction(ClockActivity.ALARM_ACTION);
        getApplicationContext().startActivity(newIntent);
    }
}

this is the portion of Manifest where are setup the service and the receiver:

<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>

the doWakefulWork method is never called!

Malchy answered 19/12, 2010 at 7:38 Comment(3)
Post your code, preferably where you try to launch your activity, and also report any errors that your getting...that'll help us help you.Coxswain
does it work when the screen is on? I'm just trying to find out if the issue relates to the wake lockCrux
as @PayPal_Tim asked in an "answer" (instead of adding a comment here) does Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive"); run ?Gadmann
E
3

I have made this application:

AlarmActivity.java

package com.foo;
import pack.breceiver.MyBroadcastReceiver;
import android.app.Activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import android.app.Activity;
import android.os.Bundle;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",
                Toast.LENGTH_LONG).show();
    }
}

MyBroadcastReceiver.java

package pack.breceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;


public class MyBroadcastReceiver extends BroadcastReceiver  {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Don't panik but your time is up!!!!",
                Toast.LENGTH_LONG).show();

        /*// Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);  */
    }
}
Eppie answered 4/6, 2012 at 6:21 Comment(0)
W
1

check this out : http://blog.nelsondev.net/?p=124

using"alarmmanager"

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
    pendingAlarmIntent);

Willis answered 1/5, 2012 at 12:19 Comment(0)
S
0

I had a similar problem and I resolved removing the receiver from manifest and set in my code registering him with registerReceiver.

Scheld answered 19/3, 2012 at 23:35 Comment(0)
G
0

Do you get an instantiation exception maybe ?

You should have a public no arg constructor in your service :

public class AlarmService extends WakefulIntentService {

    public AlarmService() {
        super("AlarmService");
    }
    // etc
}
Gadmann answered 3/4, 2013 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.