android 線程的開啟和停止(借鑒java的技術),androidjava
在onclick()點擊事件中的
// 上傳地理位置
case R.id.person_up_locat:
System.out.println("====輸出了。。。。====");
// startActivity(new Intent(PersonCenterActivity.this,
// AddedToolsActivity.class));
// 擷取地理位置--經緯度和座標
String addrStr = ShareData.getShareStringData("addrStr");
String latit = ShareData.getShareStringData("latit");
String longit = ShareData.getShareStringData("longit");
String urlLoca = HttpUrl.HttpLIPEI
+ "/yqw/w/c/post?app=survey&mdl=web&act=getSurveyLonLatList"
+ "&type=json&token=1";
// + "&col.surveyId="+ surveyId
urlLo = urlLoca + "&col.lon=" + longit + "&col.lat=" + latit
+ "&col.addressinfo=" + addrStr;
System.out.println("====輸出了。。。。==2==");
// final String LocationLLA = HttpService.postParamAndFile(urlLo);
// mtime.schedule(new RemindTask(), 100, 3000);
final Timer mtime = new Timer();
// TimerTask mtask;
mtime.schedule(new TimerTask() {
@Override
public void run() {
try {
System.out.println("=FLAG=0=" + FLAG);
// mtime.schedule(mtask, 100, 1000);//
// 0.1s後執行task,經過1s再次執行
System.out.println("上傳地理位置-------11--");
//使用do,while的方法,do---開始啟動線程,上傳資料
do {
LocationLLA = HttpService.postParamAndFile(urlLo);
System.out.println("上傳地理位置-------33--" + urlLo
+ "===" + LocationLLA);
} while (FLAG == 1);
//while (FLAG == 1);---停止關閉線程,關閉資料
mtime.cancel();
System.out.println("不上傳地理位置-------22--");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 100, 3000);
System.out.println("=FLAG=1=" + FLAG);
if (FLAG == 0) {
first_visib.setVisibility(View.GONE);
first_gone.setVisibility(View.VISIBLE);
FLAG = 1;
System.out.println("===00====0======");
} else if (FLAG == 1) {
System.out.println("===0===2======");
first_visib.setVisibility(View.VISIBLE);
first_gone.setVisibility(View.GONE);
FLAG = 0;
System.out.println("不上傳地理位置-----00--22--");
}
System.out.println("=FLAG=2=" + FLAG);
break;
default:
break;
java線程怎停止
你實現的有問題吧,
有繼承線程類,或實現Runnable嗎?
如果沒有這樣,那你做的想當於在同一個線程裡做了一個死迴圈。
下邊是個例子,你參考一下,
點開始過度條在動,點一下暫停,會停下來,再點一下暫停又會動起來。
點stop會停下來,你看一下。
----------------------------------------------------------------
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Table extends JFrame implements Runnable {
private JProgressBar progress = new JProgressBar();
private boolean start = false;
private boolean pause = false;
private JButton btnStart;
public Table() {
setResizable(false);
getContentPane().setLayout(null);
JToolBar toolBar = new JToolBar();
toolBar.add(new JLabel("state"));
toolBar.add(progress);
toolBar.setFloatable(false);
toolBar.setBounds(0, 247, 454, 21);
getContentPane().add(toolBar);
btnStart = new JButton("start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
btnStart.setBounds(12, 10, 91, 21);
getContentPane().add(btnStart);
JButton btnPause = new JButton("Pause");
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pause = !pause;
}
});
btnPause.setBounds(131, 10, 91, 21);
getContentPane().add(btnPause);
JButton btnStop = new JButton("stop"......餘下全文>>
android下怎停止正在啟動並執行線程
2.2之後 按住home鍵不放 進入 工作管理員 即可對線程就行操作