1 回答

TA貢獻1869條經驗 獲得超4個贊
為了實現您的要求,請將上下文對象傳遞給 TimerTask 類并使用它來調用 startService。
在活動代碼中,
Timer poll_timer = new Timer();
poll_timer.schedule(new Timertesttask(MainActivity.this),0, 1000);
定時器任務代碼,
public class Timertesttask extends TimerTask {
Context ctxObject = null;
public Timertesttask(Context ctx) {
ctxObject = ctx;
}
@Override
public void run() {
Intent gpsintent = new Intent(ctxObject, Gps.class);
ctxObject.startService(gpsintent);
}
}
你的意圖服務類,
public class Gps extends IntentService {
public Gps() {
super("Gps");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d("Testing","Testing");
}
}
將IntentService的入口放入AndroidManifest中
<service android:name=".Gps" />
添加回答
舉報