Android服务计时器崩溃

时间:2015-09-26 11:55:12

标签: java android database service timer

我正在开发一款应用。用户可以输入他朋友的信息 - 姓名和出生日期。当添加信息时,应该开始一项服务,该服务应该计算剩余的时间并在朋友即将过生日时发送通知。服务从数据库获取信息。当我添加信息时,服务就会崩溃。这是我的服务代码

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.

                    }
                    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);
    //db = new DB2(this);
    //db.open();
}





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");
}

当我有这样的代码时

class UpdateTimeTask extends TimerTask{
    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.

                    }
                    while (cursor.moveToNext());//takes next item

                }

        cursor.close();
    }
}

没有崩溃。只是没有通知

0 个答案:

没有答案