发布于 2016-04-08 10:45:09 | 120 次阅读 | 评论: 0 | 来源: 网友投递
Android移动端操作系统
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
使用未设限制的Executor(Executors.newCachedThreadPool()):
public class AsyncTaskDemoActivity extends Activity {
private static int ID = 0;
private static final int TASK_COUNT = 9;
private static ExecutorService SINGLE_TASK_EXECUTOR;
private static ExecutorService LIMITED_TASK_EXECUTOR;
private static ExecutorService FULL_TASK_EXECUTOR;
static {
SINGLE_TASK_EXECUTOR = (ExecutorService) Executors.newSingleThreadExecutor();
LIMITED_TASK_EXECUTOR = (ExecutorService) Executors.newFixedThreadPool(7);
FULL_TASK_EXECUTOR = (ExecutorService) Executors.newCachedThreadPool();
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.asynctask_demo_activity);
String title = "AsyncTask of API " + VERSION.SDK_INT;
setTitle(title);
final ListView taskList = (ListView) findViewById(R.id.task_list);
taskList.setAdapter(new AsyncTaskAdapter(getApplication(), TASK_COUNT));
}
private class AsyncTaskAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mFactory;
private int mTaskCount;
List<SimpleAsyncTask> mTaskList;
public AsyncTaskAdapter(Context context, int taskCount) {
mContext = context;
mFactory = LayoutInflater.from(mContext);
mTaskCount = taskCount;
mTaskList = new ArrayList<SimpleAsyncTask>(taskCount);
}
@Override
public int getCount() {
return mTaskCount;
}
@Override
public Object getItem(int position) {
return mTaskList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mFactory.inflate(R.layout.asynctask_demo_item, null);
SimpleAsyncTask task = new SimpleAsyncTask((TaskItem) convertView);
/*
* It only supports five tasks at most. More tasks will be scheduled only after
* first five finish. In all, the pool size of AsyncTask is 5, at any time it only
* has 5 threads running.
*/
// task.execute();
// use AsyncTask#SERIAL_EXECUTOR is the same to #execute();
// task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
// use AsyncTask#THREAD_POOL_EXECUTOR is the same to older version #execute() (less than API 11)
// but different from newer version of #execute()
// task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
// one by one, same to newer version of #execute()
// task.executeOnExecutor(SINGLE_TASK_EXECUTOR);
// execute tasks at some limit which can be customized
// task.executeOnExecutor(LIMITED_TASK_EXECUTOR);
// no limit to thread pool size, all tasks run simultaneously
task.executeOnExecutor(FULL_TASK_EXECUTOR);
mTaskList.add(task);
}
return convertView;
}
}
private class SimpleAsyncTask extends AsyncTask<Void, Integer, Void> {
private TaskItem mTaskItem;
private String mName;
public SimpleAsyncTask(TaskItem item) {
mTaskItem = item;
mName = "Task #" + String.valueOf(++ID);
}
@Override
protected Void doInBackground(Void... params) {
int prog = 1;
while (prog < 101) {
SystemClock.sleep(100);
publishProgress(prog);
prog++;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
@Override
protected void onPreExecute() {
mTaskItem.setTitle(mName);
}
@Override
protected void onProgressUpdate(Integer... values) {
mTaskItem.setProgress(values[0]);
}
}
}
class TaskItem extends LinearLayout {
private TextView mTitle;
private ProgressBar mProgress;
public TaskItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TaskItem(Context context) {
super(context);
}
public void setTitle(String title) {
if (mTitle == null) {
mTitle = (TextView) findViewById(R.id.task_name);
}
mTitle.setText(title);
}
public void setProgress(int prog) {
if (mProgress == null) {
mProgress = (ProgressBar) findViewById(R.id.task_progress);
}
mProgress.setProgress(prog);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:orientation="vertical" >
<ListView android:id="@+id/task_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#cccccc"
android:dividerHeight="0.6dip"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.hilton.effectiveandroid.os.TaskItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:textSize="26sp" />
<ProgressBar android:id="@+id/task_progress"
android:layout_width="fill_parent"
android:layout_height="15dip"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal" />
</com.hilton.effectiveandroid.os.TaskItem >