我的应用程序与 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。对于教程不同的服务,请这。
上一篇:在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems适用于、鼠标、但不、控件
下一篇:我的ListView(我的ViewFilipper)填充在底部的父母和按钮获得隐藏我的、按钮、父母、ViewFilipper