频繁启动服务每X秒螺纹的帮助,因为数据库更新 [英] Start service every X second with the help of thread as database update frequently

查看:134
本文介绍了频繁启动服务每X秒螺纹的帮助,因为数据库更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序与 SQL SERVER 数据库,其中每日更新相连,所以,当我开始我的活动,登录记录的形式弹出和用户。现在,当我的数据库更新任何第二个,我想为执行查询每隔X秒,这样在数据库中的任何变化通知,并发送通知给用户。所以,我觉得线程会在玩,这样的查询运行的每一秒。现在,我想知道的如何实现线程在这一点,并通知执行服务,以便每当数据在数据库中更新的用户将通过推送通知的通知。

My app is connected with SQL SERVER database which updates daily, so when i start my activity, log-in form pop-up and user logged in . Now as my database update any second , i want to run a query every X second so that any change in database is notified and send notification to user. So , I think thread will come in play so that query run every second . Now, i want to know how to implement Thread in this and run Service for notification so that whenever data is updated in database user will be notified via push notification.

推荐答案

您可以使用的还有一个定时器如下IntentService

public class MyBackgroundService extends IntentService
{
private Timer mBackGroundTimer;
public MyBackgroundService()
    {
        super("myservice");
        this.mBackGroundTimer=new Timer();
    }
@Override
protected void onHandleIntent(Intent intent) 
    {
        // TODO Auto-generated method stub
        mBackGroundTimer.schedule(new TimerTask() 
            { 
                public void run() 
                    { 

                        try 
                            {
                                //your db query here
                            } 
                        catch (Exception e) 
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    } 
            },startTime,repeatTime); 
    } // END onHandleIntent()
private void mStopTimer()
    {
        //Call this whenever you need to stop the service
        mBackGroundTimer.cancel();
    }
}

和调用此您的活动里,如下:

and call this inside your activity as below:

Intent mInt_BckGrndSrv =  new  Intent(getApplicationContext(),MyBackgroundService.class);
        startService(mInt_BckGrndSrv);

,不要忘记将其指定为您的manifest.xml作为一种服务,

and don't forget to specify it as a service in your manifest.xml as,

<service android:name="com.example.MyBackgroundService" android:enabled="true"></service>

P.S。对于教程不同的服务,请

这篇关于频繁启动服务每X秒螺纹的帮助,因为数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆