1
這是我的拳頭應用程序,我試圖使Alarm manager
與BroadcastReceiver
一起使用。廣播接收器報警管理器
我用我希望Alarm manager
觸發onReceive()
的時間調用函數「SetFutureStartTime()」。但是當時機到來時,什麼也沒有發生。
這是個代碼:
public void SetFutureStartTime(int startHour, int startMin)
{
Context context = this;
m_alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver_SendOn.class);
intent.setAction("Broadcast");
m_alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 21:32 PM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, startHour);
calendar.set(Calendar.MINUTE, startMin);
m_alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), m_alarmIntent);
}
public class AlarmReceiver_SendOn extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
SendTcpMessage(TURN_ON_COMMAND); //I put here break point but nothing happens
}
}
這是manifast
末:
.....
</activity>
<receiver android:name=".MainActivity$AlarmReceiver_SendOn">
<intent-filter>
<action android:name="Broadcast">
</action>
</intent-filter>
</receiver>
</application>
我缺少什麼?
是'AlarmReceiver_SendOn'一個內部類的'MainActivity'的,就像清單中顯示的那樣? –
嗨,是的......它在'MainActivity'裏面。 – user1673206
如果你想讓Receiver類成爲一個嵌套類,它必須是'public static'。 –