过去一个月我一直在尝试慢慢学习Android API。我已阅读文档,查看过的示例,似乎无法弄清楚我做错了什么。基本上,应用程序在应用程序启动后准确地设置了1分钟的警报。
我目前遇到的问题是在设置闹钟后,它没有收到。这就是我所拥有的:
SetAlarmActivity.java
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: rgb(255, 255, 255);">
<small style="left: 0px; transition: background-color 0.4s, left 0.2s;">0px</small>
</span>
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: rgb(255, 255, 255);">
<small style="left: 1px; transition: background-color 0.4s, left 0.2s;">1px</small>
</span>
AlarmReceiver.java
public class SetAlarmActivity extends AppCompatActivity {
private final String TAG = "AlarmTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_alarm);
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
Log.d(TAG, "Alarm time is " + c.getTime());
// set the intent to launch our SetAlarmActivity
Intent alarmIntent = new Intent(SetAlarmActivity.this, SetAlarmActivity.class);
// retrieve a PendingIntent to perform a Broadcast
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(SetAlarmActivity.this, 0, alarmIntent, 0);
// grab the Alarm Service and tell it to call our Intent at the set time
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingAlarmIntent);
}else{
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingAlarmIntent);
}
}
}
的AndroidManifest.xml
public class AlarmReceiver extends WakefulBroadcastReceiver {
private final String TAG = "AlarmTest";
@Override
public void onReceive(Context context, Intent intent){
Log.d(TAG, "Received broadcast!");
}
}
答案 0 :(得分:1)
您必须在当前activity
和receiver
类之间创建意图。像这样改变你的代码。
Intent intent = new Intent(SetAlarmActivity.this, AlarmReceiver.class);
答案 1 :(得分:0)
更改
Intent alarmIntent = new Intent(SetAlarmActivity.this, SetAlarmActivity.class);
到
Intent alarmIntent = new Intent(SetAlarmActivity.this, AlarmReceiver.class);
然后它的工作正常