Android TimerTask或Handler只触发一次

时间:2015-09-26 17:33:54

标签: android service handler timertask

我正在尝试开发一款应用。用户可以输入他朋友的姓名和生日(包括小时和分钟)。在朋友的生日那天,申请人应该发送通知。首先,我尝试使用timerTask在需要时发送通知,但它只发送一次,并且只有在我输入的时间相同时(意味着时间任务正在工作)。但是当我输入至少1分钟时,1分钟后没有通知。处理程序也是如此。这是我的TimerTask代码

Timer timer = new Timer();
DB2 db;
Cursor cursor;
Calendar x = Calendar.getInstance();
int y = x.get(Calendar.YEAR);
int m = x.get(Calendar.MONTH) + 1;
int d = x.get(Calendar.DAY_OF_MONTH);
int h = x.get(Calendar.HOUR_OF_DAY);
int min = x.get(Calendar.MINUTE);
int s = x.get(Calendar.SECOND);
Handler handler;
NotificationManager nm;
int l, p, a, i, month, hours, minutes, seconds, m22, d22, v, g, t;
int[] k = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class UpdateTimeTask extends TimerTask{
public void run(){
    MS2.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            db.open();
            cursor = db.getAllData();
            cursor.moveToFirst();
            if (cursor.moveToFirst()) {
                do {
                    int d110 = cursor.getInt(cursor.getColumnIndex("day")); //here it should do some manipulations with every item in DB.

                    if (m22 == 0)
                        if (d22 == 0)
                            if (v == 0)
                                if (g == 0)
                                    not();
                }
                while (cursor.moveToNext());//takes next item

            }cursor.close();
        }
    });

}
}
private void runOnUiThread(Runnable runnable) {
handler.post(runnable);
}

public void onCreate() {
super.onCreate();
handler = new Handler();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

public void not() {
Intent resultIntent = new Intent(this, TEST2.class);
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
db = new DB2(this);
db.open();
Toast.makeText(getApplicationContext(), "Service Running ", 1).show();
timer.schedule(new UpdateTimeTask(), 0, 1000);

return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

和处理程序代码

Timer timer = new Timer();
DB2 db;
Cursor cursor;
Calendar x = Calendar.getInstance();
int y = x.get(Calendar.YEAR);
int m = x.get(Calendar.MONTH) + 1;
int d = x.get(Calendar.DAY_OF_MONTH);
int h = x.get(Calendar.HOUR_OF_DAY);
int min = x.get(Calendar.MINUTE);
int s = x.get(Calendar.SECOND);
private Handler handler = new Handler();
NotificationManager nm;
int l, p, a, i, month, hours, minutes, seconds, m22, d22, v, g, t;
int[] k = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

private void runOnUiThread(Runnable runnable) {
    handler.post(runnable);
}

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        db.open();
        cursor = db.getAllData();
        cursor.moveToFirst();
        if (cursor.moveToFirst()) {
            do {
                int d110 = cursor.getInt(cursor.getColumnIndex("day"));
                if (m22 == 0)
                    if (d22 == 0)
                        if (v == 0)
                            if (g == 0)
                                not();

            }
            while (cursor.moveToNext());//takes next item
            cursor.close();
        }
        handler.postDelayed(this, 30000);
    }

};


public void onCreate() {
    super.onCreate();
    handler.postDelayed(runnable, 30000);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}





public void not() {
    Intent resultIntent = new Intent(this, TEST2.class);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
    int mNotificationId = 001;
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    handler.postDelayed(runnable, 30000);
    db = new DB2(this);
    db.open();

    Toast.makeText(getApplicationContext(), "Service Running ", 1).show();
    return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

0 个答案:

没有答案