我创建了一个通过startService()方法从活动中调用的服务。在服务类上,我通过比较两个字符串将当前日历时间与数据库中的日期和时间进行比较。如果字符串匹配,则服务类应该触发警报广播意图。但由于某种原因,服务类正在启动,但警报没有被触发。谁能帮我这个?。提前致谢。我已将我的代码包含在下面
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final SqliteController db = new SqliteController(getApplicationContext());
new Thread(new Runnable() {
@Override
public void run() {
List<Greetings>greetings = db.getAllGreetings();
for(Greetings g:greetings){
Calendar cal = Calendar.getInstance();
d1 = cal.get(Calendar.DAY_OF_MONTH);
d2 = cal.get(Calendar.MONTH);
d3 = cal.get(Calendar.YEAR);
t1 = cal.get(Calendar.HOUR);
t2 = cal.get(Calendar.MINUTE);
t3 = cal.get(Calendar.AM_PM);
if(t3 == 1){
t4 = "PM";
}else {
t4 = "AM";
}
StringBuilder strt = new StringBuilder().append(t1).append(" : ").append(t2)
.append(" ").append(t4);
tc = strt.toString();
StringBuilder builder=new StringBuilder().append(String.valueOf(d3)+"-").append((String.valueOf(d2+1))+"-")
.append(String.valueOf(d1));
d = builder.toString();
cdt = d + "\t" + tc ;
time = g.getTime();
date = g.getDate();
phone = g.getPhoneNumber();
msg = g.getMessage();
id = g.getID();
dnt = date + "\t" + time ;
if (dnt.equals(cdt)){
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),id,aint,PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg",msg);
aint.putExtra("phone",phone);
alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
}
}
k = db.getGreetingsCount();
if(k == 0){
stopSelf();
}
}
}).start();
return START_NOT_STICKY;
}
警报接收器类位于
之下@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"Testing it",
Toast.LENGTH_LONG).show();
}
我的logcat上也没有显示错误。