編輯:關於Android編程
package com.example.message1; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { public static final String URL = "http://www.baidu.com/img/baidu_sylogo1.gif"; public static final int SUCCESS_CODE = 1; private Button button; private ImageView imageView; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); imageView = (ImageView) this.findViewById(R.id.imageView1); final ProgressDialog dialog = new ProgressDialog( MainActivity.this); dialog.setTitle("加載圖片"); dialog.setMessage("正在加載請稍後……"); dialog.setCancelable(false); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(new MyThread()).start(); dialog.show(); } }); // 前台UI的更新 handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub // 取得Bitmap圖片 Bitmap bitmap = (Bitmap) msg.obj; // 為圖片設置資源 imageView.setImageBitmap(bitmap); // 判斷圖片是否獲得,並關閉對話框 if (msg.what == SUCCESS_CODE) { dialog.dismiss(); } } }; } //後台線程 class MyThread implements Runnable { @Override public void run() { Bitmap bitmap = null; // TODO Auto-generated method stub try { URL url = new URL(URL);//創建Url對象 InputStream inputStream = url.openStream();//獲得輸入流 bitmap = BitmapFactory.decodeStream(inputStream);//將輸入流轉換為Bitmap對象 } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 創建Message對象,返回信息 Message message = Message.obtain(); message.obj = bitmap; message.what = SUCCESS_CODE; handler.sendMessage(message); } } }
package com.example.message2; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button button; private TextView textView; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); textView = (TextView) this.findViewById(R.id.info); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub int what = msg.what; int arg1 = msg.arg1; int arg2 = msg.arg2; Object obj = msg.obj; Listlist = null; Bundle bundle = null; bundle = msg.getData();//接受Bundle數據 list =bundle.getIntegerArrayList("num");//解析數據 for (Integer num : list) { System.out.print("--list:"+num); } System.out.println("what:" + what + ",arg1:" + arg1 + ",arg2:" + arg2 + ".obj:" + obj); } }; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(new MyThread()).start(); } }); } class MyThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub // Message message=new Message(); // message.arg1=1; // message.arg2=2; // message.obj=3; // message.what=4; // handler.sendMessage(message); // 第二種方法 // Message message=Message.obtain(); // message.what=1; // message.arg1=2; // message.arg2=3; // message.obj=4; // handler.sendMessage(message); // 第三種方法 // Message message=Message.obtain(handler); // message.what=1; // message.arg1=2; // message.arg2=3; // message.obj=4; // message.sendToTarget(); // 第四種方法 // Message message=Message.obtain(handler, 1); // message.arg1=2; // message.arg2=3; // message.obj=4; // message.sendToTarget(); // 第五種方法 // Message message=Message.obtain(handler, 1, 4); // message.arg1=2; // message.arg2=3; // message.sendToTarget(); // 第六種方法 // Message message=Message.obtain(handler, 1, 2, 3); // message.obj = 4; // message.sendToTarget(); // 第七種方法 // Message message=Message.obtain(handler, 1, 2, 3, 4); // message.sendToTarget(); // 第八種方法 Message message = Message.obtain(handler, 1, 2, 3, 4); Bundle bundle = new Bundle(); List list=new ArrayList (); list.add(5); list.add(6); list.add(7); bundle.putIntegerArrayList("num", (ArrayList )list); message.setData(bundle); message.sendToTarget(); } } }
package com.example.message4; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { public static final int SIGHT_TIME = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//設置應用沒有標題 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, //以上兩個設置都必須放在setContentView之前,不然會出現異常 WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, Two.class); startActivity(intent); finish(); } }, SIGHT_TIME); } }
package com.example.message4; import android.app.Activity; import android.os.Bundle; public class Two extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.main); } }
如上一篇博客《Android動畫之一:Drawable Animation》所說,android動畫主要分為三大部分,上一篇博客已經講解Drawable Animatio
一、View + ViewPager使用ViewPager和View實現切換效果,效果如下:主布局界面: top.xml &n
在前面提到Android網絡收音機--使用Vitamio解碼(一)實現了播放流媒體文件,但只能播放一次,在這裡可以隨機點擊左邊的menu來播放,但Vitamio還是不是很
本文實例講述了Android編程實現讀取手機聯系人、撥號、發送短信及長按菜單操作方法。分享給大家供大家參考,具體如下:1.Andrid項目結構圖↓主要操作圖中紅色方框內的