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>