1.效果图:打开app,会自动执行线程,自动增加进度条,直到加满停止
点击restart,清空进度条,重新开始
点击wait,暂停线程,进度条不在增加
点击start,开启线程,进度条在刚才停止的地方,继续启动线程
2.
ui_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:max="500" />
<Button
android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="restart" />
<Button
android:id="@+id/waittime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="wait" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="start" />
</LinearLayout>
3.
MainActivity主界面
package com.example.administrator.testz;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new UiFragment()).commit();
}
}
}
4.
UiFragment
package com.example.administrator.testz;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class UiFragment extends Fragment {
private static final String LOG_TAG = UiFragment.class.getSimpleName();
private WorkerFragment mWorkerFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.ui_fragment, container, false);
v.findViewById(R.id.restart).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mWorkerFragment.restart();
}
});
v.findViewById(R.id.start).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mWorkerFragment.start();
}
});
v.findViewById(R.id.waittime).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mWorkerFragment.waitTime();
}
});
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getFragmentManager();
mWorkerFragment = (WorkerFragment) fm.findFragmentByTag("worker");
if (mWorkerFragment == null) {
mWorkerFragment = new WorkerFragment();
mWorkerFragment.setTargetFragment(this, 0);
fm.beginTransaction().add(mWorkerFragment, "worker").commit();
}
}
}
5.
WorkerFragment
package com.example.administrator.testz;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.ProgressBar;
public class WorkerFragment extends Fragment {
private static final String LOG_TAG = UiFragment.class.getSimpleName();
private ProgressBar mProgressBar;
private int mProgress;
private boolean mReady;
private boolean mQuiting;
final Thread mThread = new Thread() {
@Override
public void run() {
int maxProgress = 10000;
while (true) {
synchronized (this) {
while (!mReady || mProgress >= maxProgress) {
if (mQuiting) {
return;
}
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
maxProgress = mProgressBar.getMax();
mProgressBar.setProgress(++mProgress);
try {
wait(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mThread.start();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mProgressBar = (ProgressBar) getTargetFragment().getView()
.findViewById(R.id.progress_horizontal);
synchronized (mThread) {
mReady = true;
mThread.notify();
}
}
@Override
public void onDestroy() {
synchronized (mThread) {
mQuiting = true;
mReady = false;
mThread.notify();
}
super.onDestroy();
}
@Override
public void onDetach() {
synchronized (mThread) {
mReady = false;
mThread.notify();
}
super.onDetach();
}
public void restart() {
synchronized (mThread) {
mProgress = 0;
mReady = true;
mThread.notify();
}
}
public void waitTime() {
synchronized (mThread) {
mReady = false;
}
}
public void start() {
synchronized (mThread) {
mReady = true;
mThread.notify(); //唤醒线程
}
}
}
end
新增一个activity,独立出来的线程等待和唤醒 ,switch开控制线程的等待和唤醒,可以通过日志详细看出效果
package com.example.administrator.testz;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
public class TextActivity extends FragmentActivity {
private Switch aSwitch;
private boolean mReady;
private boolean mQuiting;
private int mProgress = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aSwitch = (Switch) findViewById(R.id.switch_main);
//启动线程
mThread.start();
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!buttonView.isPressed()) {
//如果不是手动点击的switch开关 忽略
return;
}
if (isChecked) {
synchronized (mThread) {
mReady = true;
mThread.notify(); //唤醒线程
}
} else {
synchronized (mThread) {
//线程等待
mReady = false;
}
}
}
});
}
@Override
protected void onStart() {
super.onStart();
synchronized (mThread) {
mReady = true;
mThread.notify();
}
}
final Thread mThread = new Thread() {
@Override
public void run() {
while (true) {
synchronized (this) {
while (!mReady) {
if (mQuiting) {
return;
}
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
try {
++mProgress;
Log.e("TextActivity", "mProgress: " + mProgress);
Thread.sleep(1000);
wait(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
};
@Override
protected void onStop() {
super.onStop();
synchronized (mThread) {
mQuiting = true;
mReady = false;
mThread.notify();
}
}
@Override
public void onDestroy() {
super.onDestroy();
synchronized (mThread) {
mReady = false;
mThread.notify();
}
}
}
布局效果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.testz.MainActivity">
<Switch
android:id="@+id/switch_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="true" />
</LinearLayout>