【问题标题】:Android alarm manager not workingAndroid警报管理器不起作用
【发布时间】:2016-04-05 08:45:17
【问题描述】:

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

我遇到的问题是我无法激活 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();
    }
}

事件按钮构造器(类扩展按钮)

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));
    }

【问题讨论】:

  • 你为什么将接收者的进程改为":remote"?
  • 那是我试图按照说明尝试让它工作,我可能误解了
  • 您可以尝试删除该属性并检查吗?
  • 你从哪里得到 getContext() ?
  • 我把它改成了那个,但还是没有运气

标签: android alarmmanager


【解决方案1】:
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 小时触发一次吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多