我想在我的应用程序中创建一个通知,每次在我的SQL Server数据库中有UPDATE或新条目时都会发出警告。 我使用Timer()并将新数据与旧数据进行比较,但问题是它只在我登录时通知我并且它会持续发送通知。 即使我不在应用程序中,我也希望得到警告。
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
String user = "", pass = "";
Bundle b = ActivityTwo.this.getIntent().getExtras();
if (b != null) {
user = b.getString("user");
pass = b.getString("pass");
}
String q12 = "Select TOP(6) readingdate,hp07,hp10 from Workers,TLD where card=cardID AND username='" + user + "' AND password='" + pass + "' order by readingdate desc ";
try {
connect = ConnectionHelper(username, password, db, ipaddress);
if (connect == null) {
new AlertDialog.Builder(ActivityTwo.this).setTitle("Error Message")
.setMessage("Error in connection!!")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
} else {
s12 = connect.prepareStatement(q12);
r12 = s12.executeQuery();
int[] ka = new int[6];
int c = 0;
while (r12.next()) {
String h07 = r12.getString("hp07");
String h10 = r12.getString("hp10");
int l07 = Integer.valueOf(h07);
ka[c] = l07;
c++;
//Toast.makeText(ActivityTwo.this, hp071 + " " + h07 + " " + hp101 + " " + h10, Toast.LENGTH_SHORT).show();
for (int i = 0; i < 6; i++) {
if (ka[i]!=hp071[i]) {
final Intent emptyIntent = new Intent(ActivityTwo.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ActivityTwo.this, 0, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
android.support.v7.app.NotificationCompat.Builder mBuilder =
(android.support.v7.app.NotificationCompat.Builder) new android.support.v7.app.NotificationCompat.Builder(ActivityTwo.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("NEW UPDATE")
.setContentIntent(pendingIntent);
mBuilder.setTicker("this is the notif");
mBuilder.setAutoCancel(true);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setOngoing(true);
Notification not = mBuilder.build();
not.defaults |= Notification.DEFAULT_VIBRATE;
not.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, not);
hp071[i]=ka[i];
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}, 0);
所以我的错误在哪里?使用谷歌云消息传递应用程序并不复杂,只是一个简单的警告。