編輯:關於Android編程
package com.jrhcode.morethreadtest; import java.util.concurrent.Executors; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; /** * * 主要功能是:在子線程中來更改TextView的顯示內容 * * android 多線程編程 1:通過handler 在新啟動的線程中更新UI組件 Handler.post(new Runnable()); 2:handler.postdelayed(new Runnable, delay); 3: Activity.runOnUiThread(); 4:AsyncTask 5:線程池 * * @author Administrator * */ public class MainActivity extends Activity { TextView tv; private Handler mHandler; private String TAG = MainActivity.class.getSimpleName(); int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); mHandler = new Handler(); // initHandler(); // initHandler2(); // initHandler3(); // updateTv(); // updateTVV(); threedPool(); } private void threedPool() { // TODO Auto-generated method stub Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println(Thread.currentThread().getName()); tv.setText(TAG); mHandler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub tv.setText("this is handler post"); } }); } }); } /** * */ private void updateTVV() { // TODO Auto-generated method stub new MyTask().execute(); } /** * 方法3 */ private void updateTv() { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub tv.setText(TAG); } }); } }).start(); } /** * 延遲3秒 */ private void initHandler3() { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mHandler.postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub tv.setText(TAG); } }, 3000); } }).start(); } /** * 在子線程中方法 */ private void initHandler2() { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mHandler.post(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); tv.setText(TAG); } }); } }).start(); } /** * Mothod 1; 該方法是在注線程中操作UI; */ private void initHandler() { // TODO Auto-generated method stub mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, Thread.currentThread().getName(), 0).show(); tv.setText(TAG); } }); } class MyTask extends AsyncTask{ @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); tv.setText(TAG); } } }
在本文當中,我將會與大家分享一個封裝了PopupWindow實現彈出菜單的類,並說明它的實現與使用。 因對界面的需求,android原生的彈出菜單已不能滿足我們的需求,自
Filter閱讀是我最近寫的一個Android的閱讀器,用於看英文的書(中文暫時不支持,會亂碼,下一版再改).在這裡開源給大家(Android studio的)源碼下載地
雖然NFC並非什麼新技術,但它至今仍僅是中高端手機的專利。令人遺憾的是,很多用戶對NFC的態度卻是“永不錄用”,一方面是擔心它的高耗
Android的UI訪問是沒有加鎖的,這樣在多個線程訪問UI是不安全的。所以Android中規定只能在UI線程中訪問UI。但是有沒有極端的情況?使得我們在子線程中訪問UI