我正在做一个警报应用程序,我使用我在互联网上找到的示例代码,但它不起作用,我不知道为什么。
这是我的 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);