我有以下代码来设置警报
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
int [] progressVals = getPackets();
displayList(progressVals, values);
setAlarm();
}
private void setAlarm(){
DatabaseHandler db = new DatabaseHandler(this);
Queue top = db.getTopQueue();
if(top != null){
String p = top.getPacket();
double timestamp = top.getTimestamp();
int qNum = top.getExNum();
Intent intent = new Intent("com.dejalearn.algorithm.AlarmReciever.class");
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayAllActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND, 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), pendingIntent);
Toast.makeText(DisplayAllActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
Log.i("DISPLAY ALL", "ALARM SET UP");
}
}
由此,我尝试从实际运行的活动中运行的活动称为 AlarmReciever,它扩展了广播接收器,但是我的警报接收器中的 toast 永远不会运行。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("FOUND", "THE MESSAGE");
try{
Toast.makeText(context, "made it", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
为什么我的新吐司从来没有运行过。我查看了多个示例,但似乎没有任何效果。我不确定出了什么问题。