Android闹钟管理器无法正常工作

时间:2016-04-04 22:13:43

标签: android alarmmanager

好的,我有一个问题,我希望我能得到一些帮助。

我遇到的问题是我无法启动Android警报管理器事件,即使它似乎在功能上与我见过别人使用的相同。没有logcat输出表明它是一个错误。

我将在下面附上我的代码,我们将不胜感激。

向按钮添加onclick(我希望管理员从中激活)

 public void setOnClick() {
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent alarm = new Intent(getContext(), ActionHandler.class);
            alarm.putExtra("event", event);
            PendingIntent pi = PendingIntent.getBroadcast(getContext(), 0, alarm, 0);
            AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
            //alarmManager.set(AlarmManager.RTC_WAKEUP,1000*2*60, PendingIntent.getBroadcast(getContext(), 1, alarm, PendingIntent.FLAG_UPDATE_CURRENT));
            //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30, pi);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, 1000 * 60,
                    AlarmManager.INTERVAL_DAY, pi);
            Toast.makeText(getContext(), "buttong pushed for event " + event.eventName, Toast.LENGTH_SHORT).show();
        }
    });
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.daniel.myapplication">
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <receiver  android:process=":remote" android:name="ActionHandler"></receiver>
    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

警报侦听器

public class ActionHandler extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Event event = (Event)intent.getSerializableExtra("event");
        Log.d("the listener event", "i have entered the event as " + event.eventName);
        Toast.makeText(null, "hi, i'm an event called " + event.eventName, Toast.LENGTH_SHORT).show();
    }
}

事件按钮构造函数(类扩展Button)

public EventButton(Context context, Event pEvent) {
        super(context);
        eventDate = pEvent.eventDate;
        eventName = pEvent.eventName;
        eventHost = pEvent.eventHost;
        eventLocation = pEvent.eventLocation;
        event = pEvent;
        startingColor = button.getDrawingCacheBackgroundColor();

        if (FileManager.compare(event)) {
            button.setBackgroundColor(Color.RED);
        } else {
            button.setBackgroundResource(android.R.drawable.btn_default);
        }

        setOnClick();

        this.setText(eventName + "\n " + eventHost);
    }

创建一个新按钮(在创建时从主活动调用)

public void addButtonToList(String pHost, String pName,String pEventLocation, Calendar pDate){
        Event event = new Event(pHost, pName, pEventLocation, pDate);
        buttons.add(new EventButton(this, event));
    }

1 个答案:

答案 0 :(得分:0)

public void setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)


Schedule a repeating alarm that has inexact trigger time requirements; for example, 
an alarm that repeats every hour, but not necessarily at the top of every hour. 
These alarms are more power-efficient than the strict recurrences traditionally supplied 
by setRepeating(int, long, long, PendingIntent), since the system can adjust alarms' 
delivery times to cause them to fire simultaneously, avoiding waking the device from sleep 
more than necessary.

Your alarm's first trigger will not be before the requested time, but it might not occur 
for almost a full interval after that time. In addition, while the overall period of the 
repeating alarm will be as requested, the time between any two successive firings of the 
alarm may vary. If your application demands very low jitter, use one-shot alarms with an 
appropriate window instead;  

简单地说。您将闹钟设置为在1970年1月1日的纪元后一分钟第一次关闭,并在此后每24小时触发一次。当然第一场火灾没有发生......

请改为:

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000 * 60,
                    AlarmManager.INTERVAL_DAY, pi);

并注意间隔期。你确定要每24小时开一次吗?