开发者

Create reminder in android app

I am trying to create a weekly reminder in my app. For this I am using AlarmManager. Here is the code generating alarm

pendingIntent = PendingIntent.getBroadcast(SettingsActivity.this, 1234567, new Intent(SettingsActivity.this, WeeklyReminder.class), 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);                  
Calendar calendar = Calendar.getInstance();       
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
long updateFreq = 30*1000;//24*60*60*1000;
alarmManager.setRepeating(AlarmManager.RTC开发者_Python百科_WAKEUP, calendar.getTimeInMillis(), updateFreq, pendingIntent);

This is the weekly remainder class which extends broadcast

public class WeeklyReminder extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        try
        {
            System.out.println("Alarm Initiated");
            Toast.makeText(context, "Recieved Alarm", Toast.LENGTH_SHORT).show();
        } 
        catch (Exception e) 
        {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}

In androidmanifest.xml I have this entry before the tag

<receiver  android:name="WeeklyReminder">

I want the reminder to be invoked even when the app is closed. But now nothing is happening Is it the right way of doing it? Thanks


I resolved the issue and putting it for someone who may come across the same problem in future. In androidmanifest file I was missing '.' ie instead of ".WeeklyReminder" I was using "WeeklyReminder"

So the correct entry is

<receiver  android:name=".WeeklyReminder">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜