0
需要一個將在後臺運行的服務,它將在30-120秒後喚醒並找到設備所在的位置,然後將該位置發送到數據中心並進入睡眠狀態。在30-120秒後服務清醒並確定位置並進入睡眠。我可以手動啓動和停止服務。但是我需要啓動(30到120秒之間)並自動停止服務。我不能保持服務生效,因爲它會耗盡電池。將在後臺自動運行和停止的服務
So,My question is how can i start and stop the service automatically?
感謝提前任何建議。
我的代碼是
public class Service extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.startButton);
Button stop = (Button)findViewById(R.id.stopButton);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(Service.this,SimpleService.class));
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(Service.this,SimpleService.class));
}
};
}
謝謝lucifer。我的代碼是 – Kabir 2012-07-16 06:41:37
歡迎你,高興地幫助你:) – Lucifer 2012-07-16 06:45:17
Mr.lucifer我是AlarmManager的新手。你能幫我解答一下嗎?上面給出了我的代碼來啓動和停止服務。 – Kabir 2012-07-16 06:49:34