문제

well , I've been sitting all day with this common problem.... so here it is , this is the method that set the alarm:

public void fireAlarm(int Year,int Month,int Day,int Hour,int Min,int Sec)
{
    Calendar calendar=Calendar.getInstance();
    calendar.set(Year, Month, Day, Hour, Min,Sec);
    Intent intent = new Intent(this, AlarmReciver.class);
    PendingIntent penintent = PendingIntent.getBroadcast(this.getApplicationContext(),234324,intent,0);
    AlarmManager alm = (AlarmManager)getSystemService(ALARM_SERVICE);
    alm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), penintent);

this is the BroadcastReciver:

package il.co.galrom;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            Log.i("alarm","alarm worked");  
    }

}

and this is my Manifest:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="il.co.galrom.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:name="il.co.galrom.AlarmReciver"></receiver>

    </application>
</manifest

In LogCat I can see the brodcast is happening but the OnRecieve() never get called

Thank you!

도움이 되었습니까?

해결책

the code by itself is working.

a common pitfall is that the calander month parameter is zero indexed. if you are trying to set an alarm for february, you need to pass 1 as your month argument to the fireAlarm function

다른 팁

The problem most probably here is this line

alm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), penintent);

Your second parameter here should be time in milliseconds after which alarm should go off. For example , if you want it to fire at that moment, this should be 0, else 1000 for 1 second and so on.

It seems you are passing a big value to the parameter (judging from arguments like year, month etc.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top