2016-04-04 212 views
0

好吧,我有一个问题,我希望我能得到一些帮助。Android闹钟管理器不工作

我遇到的问题是我无法得到一个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)); 
    } 
+0

你为什么把你的接收器的过程改为“:remote”? –

+0

那是我试图按照指示尝试让它工作,我可能会误解 – daniel

+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小时开始闹钟。 Ofcourse级第一火灾不会发生......

而是执行此操作:

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

而且要注意间隔周期。你确定要每隔24小时发射一次吗?