Android,防止杀死服务/线程

时间:2014-05-14 09:29:43

标签: android multithreading service

我怎么能阻止这个服务与线程不要从Android杀死,我需要这个通知总是runnig,但什么时候移动锁定,什么都不会发生。我认为android杀死服务或线程或类似的东西

onCreate中的MainActivity

startService(new Intent(this, NotifyService.class));

我的服务

public class NotifyService extends Service {

private DatabaseOp mDbHelper;
public Vibrator vibrator;

String username;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate ()
{
    mDbHelper = new DatabaseOp(this);
    final boolean cyklus = true;

    Thread vlakno = new Thread (new Runnable()
    {
        @Override
        public void run()
        {
            while (cyklus)
            {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String sysDate = getSysDate();
                String sysDate2 = getSysDate2();
                String time = getSysTime();

                mDbHelper.open();

                Log.v( "sysDate", sysDate );

                Cursor cursorU = mDbHelper.fetchUlohaS(0, sysDate);
                if (cursorU.getCount() > 0)
                {
                    String idU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
                    String dbDateU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_DATE));
                    String menoU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));

                    String mHodina = getResources().getString(R.string.cas)+" "+cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_HODINA));

                    Log.v( "task", dbDateU+"/"+sysDate );

                    if (dbDateU.equals(sysDate))
                    {
                        Notify(menoU, mHodina, idU, 0);                 
                    }
                }

                Cursor cursorS = mDbHelper.fetchSviatokS(3, sysDate2);
                if (cursorS.getCount() > 0)
                {
                    String idS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
                    String dbDateS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_DATUM));
                    String menoS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));

                    if (dbDateS.equals(sysDate2) && time.equals("09:00")) 
                    {
                        Notify(menoS,getResources().getString(R.string.title_section4), idS, 3);
                    }
                }
                mDbHelper.close();
            }
        }
    });

    vlakno.start();
}
}

3 个答案:

答案 0 :(得分:0)

您是否尝试过使用ForgroundService?

查看此回购以获取示例 - https://github.com/supercurio/foreground-service-sample-app

答案 1 :(得分:0)

我认为你应该考虑AlarmManager。请参阅http://developer.android.com/reference/android/app/AlarmManager.html

答案 2 :(得分:0)

要让系统尽可能长时间保持Service活着(即在RAM非常短或用户通过应用程序信息屏幕手动杀死服务之前),您需要将其作为前台服务运行 - - 使用startForeground()方法。

如果您正在寻找在设备关闭时运行的方法,请阅读Keeping The Device Awake培训页面,如果您的任务不是很关键,请考虑使用@ AlarmManager在计时精度方面。