2013-03-30 43 views
1

我intentservice在myDataBsae發現價值時通知,但如果數據爲空應該不通知我,所以如何使Intentservice重複一項工作?

  1. 我怎樣才能使MYDATABASE我Intentservice檢查內容每50秒(我用AlarmManager但我的服務通知我每50秒儘管我的數據庫=空,)?
  2. 我希望我的工作intentservice單獨線程,當我觸發它

我的服務是:

public class ShowTimeServer extends IntentService { 
NotificationManager nm; 
static final int uniqueID=326534; 
public ShowTimeServer() { 
    super("ShowTimeServer"); 
    // TODO Auto-generated constructor stub 
} 

/* (non-Javadoc) 
* @see android.app.IntentService#onHandleIntent(android.content.Intent) 
*/ 
@Override 
protected void onHandleIntent(Intent arg0) { 
    // TODO Auto-generated method stub 

    ShowTimeDB RetrieverDB =new ShowTimeDB(this); 
    RetrieverDB.open(); 
    String data = RetrieverDB.getShowNotify(); 
    RetrieverDB.close(); 



    if (!(data.equals(null))){ 

     nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     Intent intent=new Intent(this,TodayShow.class); 
     PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0 ); 
     CharSequence x=(CharSequence) data; 
     Notification n=new Notification(R.drawable.ic_launcher,"YOU HAVE SHOW" ,System.currentTimeMillis()); 
     n.setLatestEventInfo(this, "ShowTime", x, pi); 
     n.defaults=Notification.DEFAULT_ALL; 

     nm.notify(uniqueID,n); 
     //pi.cancel(); 
     //nm.cancel(uniqueID); 

    } 
} 

} 

回答

0
  1. intentService是不應該做的事情每X秒。爲此,有很多其他解決方案。如果您堅持爲此任務使用服務,則可以將其設置爲常規的前臺/後臺服務(具體取決於您是否希望在用戶離開應用程序時執行此操作),然後在此處執行此操作。

  2. intentService已在另一個線程上運行。這就是爲什麼你可以對onHandleIntent方法進行長時間操作的原因。

+0

您的意思是我不能使其中的runnable或計時器 –

+0

我不明白你指的是什麼。請詳細解釋。 –

+0

我的意思是,我可以使用國家文物局下面這段代碼,並把我的通知代碼在它 ***> 私人的TimerTask mTask =新的TimerTask(){ @覆蓋 公共無效的run(){// 不管你想要什麼 postDelayed (this,REPEAT_INTERVAL); //沖洗並重復... } }; –

相關問題