Android的AlarmManager发送通知每次应用程序启动,而不是只有一次 [英] Android AlarmManager send notification every time app starts instead of only once

查看:212
本文介绍了Android的AlarmManager发送通知每次应用程序启动,而不是只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用 AlarmManager 的启动广播接收器
广播接收器生成一个提醒通知给用户。

My app use an AlarmManager that start a BroadcastReceiver. The BroadcastReceiver generate a reminder notification to the user.

这是启动报警在MainActivity

This is the MainActivity that start the Alarm

public class MainActivity extends ActionBarActivity {

    private PendingIntent pendingIntent;
    private AlarmManager alarmManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set the alarm to start at approximately 2:00 p.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int curHr = calendar.get(Calendar.HOUR_OF_DAY);

// Checking whether current hour is over 14
        if (curHr >= 14)
        {
            // Since current hour is over 14, setting the date to the next day
            calendar.add(Calendar.DATE, 1);
        }

        calendar.set(Calendar.HOUR_OF_DAY, 14);
// Schedule alarm manager

        Intent myIntent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

        alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

        // With setInexactRepeating(), you have to use one of the AlarmManager interval
        // constants--in this case, AlarmManager.INTERVAL_DAY.
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);


    }

和这是广播接收器生成通知

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag");

        //Acquire the lock
        wl.acquire();

        Log.v("ADebugTag", "It work!");

        int mId = 0;
        //Show the notification here.
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_action_edit)
                        .setContentTitle("Diario Scolastico")
                        .setContentText("You have homeworks for tomorrow!")
                        .setAutoCancel(true)
                        .setDefaults(-1);

// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, MainActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

        //Release the lock
        wl.release();
    }
}

清单的权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

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

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

正如你所看到的报警必须在14日下午开始,但我收到通知每当我启动应用程序的时间。
也有一些是错误的,我code?

As you can see the alarm must start at 14 PM, but i receive the notification every time that i start the app. There is something wrong in my code?

我使用调度重复报警了解每天在相同的设定报警小时,但不能正常工作

I use the Scheduling Repeating Alarms for setting the alarm every day at same hour, but don't work properly

推荐答案

这个问题是您的时间设置为当前日期。所以,如果你在14:00后打开你的应用程序,你报警经理会立即触发。

The issue is you are setting the time to current day. So if you are opening your app after 14:00, your alarm manager will immediately fire.

您需要检查的时间是否超过14点​​当天,如果是你需要的日期更改为第二天:

You need to check whether the time is over 14:00 for the current day, if yes you need to change the date to the next day:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);

// Checking whether current hour is over 14
if (curHr >= 14)
{
   // Since current hour is over 14, setting the date to the next day
   calendar.add(Calendar.DATE, 1);
}

calendar.set(Calendar.HOUR_OF_DAY, 14);
// Schedule alarm manager

这篇关于Android的AlarmManager发送通知每次应用程序启动,而不是只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆