編輯:關於Android編程
一、runOnUiThread的用法
runOnUiThread是Activity的內部方法,使用時最好指定當前的環境變量(Context)。
new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { public void run() { Toast.makeText(mainActivity.this,"UI操作。。。",0).show(); } }); } }).start();
執行runOnUiThread這個方法會調用父類中的
public final void runOnUiThread(Runnable action){ if(Thread.currentThread()!=mUiThread){ mHandler.post(action); }else{ action.run(); } }
二、新線程中View直接在UI線程中更新的方法
textView.postDelayed(new Runnable() { @Override public void run() { textView.setText("Test View.post(Runnable)"); } }, 1000); - textView.post(new Runnable() { @Override public void run() { textView.setText("Test View.postDelay(Runnable,long)"); } });
三、Handler(消息傳遞機制)使用
Handler myHandler = new Handler(){ public void handleMessage(Message msg){ super.handleMessage(msg); } };
也可以繼承handler
class MyHandler extends handler{ public MyHandler(){ } @Override public void handleMessage(Message msg){ super.handleMessage(msg); } }
分發Message或者Runnable對象到handler所在的線程中一般handler在主線程中。
handler中一些分發消息的方法:
post方式添加一個實現Runnable接口的匿名對象到消息對列中,在目標收到消息後就可以以回調的方式在自己的線程中執行
Message對象所具有的屬性:
屬性
類型
描述
arg1
int
用來存放整型數據
arg2
int
用來存放整型數據
obj
Object
用來存放發送給接收器的Object任意對象
replyTo
Messager
用來指定此Message發送到何處的可選Message對象
what
int
用於指定用戶自定義的消息代碼這樣接受者就可以了解這個消息的信息
Message message = Message.obtain(); message.arg1 = 1; message.arg2 = 2; message.obj = "Demo"; message.what = 3; Bundle bundle = new Bundle(); bundle.putString( "name","Lucy"); message.setData(bundle);
下面貼上一段示例代碼(開啟新線程實現電子廣告牌)
public class MainActivity extends Activity implements Runnable { private ImageView iv; private TextView tv; private Handler handler; private int[] path = new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04, R.drawable.img05,R.drawable.img06}; private String[] title = new String[]{"編程詞典系列","高效開發","快樂分享","用戶人群","快速學習","全方位查詢"}; private int index =0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); tv = (TextView) findViewById(R.id.textView1); Thread t = new Thread(this); t.start(); handler = new Handler(){ public void handleMessage(Message msg){ if (msg.what ==1) { tv.setText(msg.getData().getString("title")); iv.setImageResource(path[msg.arg1]); } super.handleMessage(msg); } }; } @Override public void run() { while(!Thread.currentThread().isInterrupted()){ index = new Random().nextInt(path.length); Message m = handler.obtainMessage(); m.arg1 = index; Bundle bundle = new Bundle(); m.what = 1; bundle.putString("title", title[index]); m.setData(bundle); handler.sendMessage(m); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
四、AsyncTask異步任務的用法
AsyncTask定義的三種泛型
private class MyTask extends AsyncTask<params, progress, result> { ... }
示例代碼:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }
獲取網絡圖片的示例代碼:
public class MainActivity extends ActionBarActivity { private ImageView iv; private Button bt; private String imagePath = "http://192.168.1.1/sa"; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); bt = (Button) findViewById(R.id.button1); dialog = new ProgressDialog(this); dialog.setTitle("提示信息:"); dialog.setMessage("正在下載。。。"); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new MyTask().execute(imagePath); } }); } public class MyTask extends AsyncTask<String, Void, Bitmap>{ @Override protected Bitmap doInBackground(String... params) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); Bitmap bitmap = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==200) { HttpEntity httpEntity = httpResponse.getEntity(); byte[] data = EntityUtils.toByteArray(httpEntity); bitmap= BitmapFactory.decodeByteArray(data, 0, data.length); } } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPreExecute() { super.onPreExecute(); dialog.show(); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); iv.setImageBitmap(result); dialog.dismiss(); } } }
關於android的圖表,這裡就換作chart吧,如果要自己實現的話,那工作量可是很大的,好在有好幾個開源的框架可以拿來使用,首先是achartengin
上一篇學習了基本用法,今天學一下稍微復雜一點的,先看一下效果圖 為了完成上面的效果還是要用到上一期開頭的四步
最近在機頂盒上做一個gridview,其焦點需要在item的子控件上,但gridview的焦點默認在item上,通過android:descendantFocusabil
嘗試了用android studio中使用gradle打多渠道的程序包,果然如傳說中那樣神奇。本文中部分是參考Google上的內容,權當學習記錄在這裡吧 1