我正在做一个警报应用程序,我接受了我在Internet上找到的示例代码,但它不起作用我不知道为什么。
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iiriondo.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".OnAlarmReceiver" ></receiver>
</application>
</manifest>
这里是收听闹钟的班级:
public class OnAlarmReceiver extends BroadcastReceiver{
private static int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "La Alarma está sonando",Toast.LENGTH_LONG).show();
}
}
最后我使用此代码设置闹钟:
Intent intent = new Intent(getApplicationContext(),OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (5 * 1000), pendingIntent);
答案 0 :(得分:1)
你的代码非常好。
只有你确保你的java文件位于同一个包package="iiriondo.activity"
答案 1 :(得分:0)
写下Intent Code而不是您的意图代码。
Intent intent = new Intent(MainActivity.this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (5 * 1000),pendingIntent);
答案 2 :(得分:0)
可以尝试传递给传递“this”而不是getApplicationContext()
new Intent(this,OnAlarmReceiver.class);