我试图在我的 android 应用程序中设置闹钟。但它失败了。我已经阅读了一些教程,但它们对我不起作用,我看不出我的错误在哪里。
这是我的代码:
显现 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byethost6.jessy_barthelemy.planificate">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CreateTask"></activity>
<receiver android:name="com.byethost6.jessy_barthelemy.planificate.HourReceiver" android:process=":remote"/>
</application>
</manifest>
我这样设置闹钟:
AlarmManager alarmManager;
PendingIntent alarmIntent;
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, HourReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();
这是我的广播接收器:
package com.byethost6.jessy_barthelemy.planificate;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.byethost6.jessy_barthelemy.planificate.enumeration.TriggerEnum;
import com.byethost6.jessy_barthelemy.planificate.helper.Task;
public class HourReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ALARM TRIGGERED", Toast.LENGTH_LONG).show();
}
}
请你帮助我好吗?:)