所以我正在设置重复警报以从服务器获取数据。问题是当警报响起时没有任何反应。启动它时没有错误,代码是正确的(据我所知)。
以下是设置闹钟的代码:
private void startRequestTimer() {
// This is what will be called when the alarm goes off (GetOperations)
Intent intent = new Intent(MainActivity.this, GetOperations.class);
PendingIntent operation = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// The alarm will go off in one second from now
long firstTime = SystemClock.elapsedRealtime();
firstTime += 1000;
// Set the alarm
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5*1000, operation);
}
这是GetOperations.java(警报响起时调用)
package se.jbhalmstad.ndroid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class GetOperations extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Oh! The alarm went off!", 2000).show();
}
}
清单,如果我在这里弄乱了一些东西(我怀疑,因为我没有收到任何错误):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/APP_NAME">
<activity android:name=".MainActivity"
android:label="@string/APP_NAME">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity" />
<activity android:name=".GetOperations" />
</application>
</manifest>
我错过了什么吗?
答案 0 :(得分:1)
您将GetOperations声明为BroadcastReceiver,但在您的Manifest中它是一个Activity。