簡體   English   中英

Android警報管理器不起作用

[英]Android alarm manager not working

好的,所以我遇到了一個問題,我希望可以得到一些幫助。

我遇到的問題是我無法激活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));
    }
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小時點火一次嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM