編輯:關於Android編程
android 異步任務的一個後台方法本質是開啟一個線程完成耗時操作,其他onPostExecute方法和onPreExecute方法運行在UI主線程用於更新UI界面。為了提高用戶體驗常見的異步任務加載方式現在總結如下:
1、異步加載界面效果如下:
關鍵代碼如下所示:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">/**
* 異步任務給列表加載數據
*/
private void fillData(){
new AsyncTask
loading對應的
loading=findViewById(R.id.ll_rublish_sms_info_loading);
activity_rubish_sms.xml
<frameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="100" > </frameLayout>
2、利用對話框實現
實現效果圖:
異步任務實現 邏輯代碼:
/** * 掃描病毒 並動態顯示在界面 */ private void scanVirus(){ new AsyncTask該邏輯處理對應的activity布局界面:() { List apps=new ArrayList (); String desc=null; Dialog dialog=new Dialog(KillVirusActivity.this); View viewKilling=View.inflate(KillVirusActivity.this,R.layout.killing_dialog,null); @Override protected void onPreExecute() { mKillVirusResult.setText("正在掃描中,請稍等..."); dialog.setCancelable(false); dialog.setContentView(viewKilling); dialog.show(); pm=getPackageManager(); packageInfos=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES|PackageManager.GET_SIGNATURES); // progressBar.setMax(packageInfos.size()); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { dialog.dismiss(); if(apps.size()>0){ mKillVirusResult.setTextColor(Color.RED); mKillVirusResult.setText("掃描完成!發現病毒"+apps.size()+"個"+","+"請及時清理"); for(final VirusApp app:apps){ //有病毒 View view=View.inflate(KillVirusActivity.this,R.layout.app_virus_info_item,null); view.setClickable(true); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //卸載程序 Intent intent = new Intent(); intent.setAction("android.intent.action.DELETE"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:" + app.getmAppVirusName())); startActivity(intent); } }); ImageView appIcon=(ImageView) view.findViewById(R.id.iv_app_icon); TextView appName=(TextView) view.findViewById(R.id.tv_app_name); TextView appDesc=(TextView)view.findViewById(R.id.tv_app_virus_desc); appIcon.setBackground(app.getmAppVirusIcon()); appName.setText(app.getmAppVirusName()); appDesc.setText(app.getmAPPVirusDesc()); mContainer.addView(view); } }else if(apps.size()==0){ mKillVirusResult.setText("掃描完成!恭喜沒有發現病毒!"); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { int total=0; //獲得數據庫操作對象 VirusDao virusDao=new VirusDao(); //獲得設備上所有應用的簽名 for(PackageInfo info:packageInfos){ total++; Signature[] signature=info.signatures; try { String md5=Md5Util.encode(signature[0].toString()); desc=virusDao.findVirus(md5); Log.i("殺毒MD5<<<", md5); desc=virusDao.findVirus(md5); } catch (Exception e) { // TODO: handle exception } //Log.i("殺毒<<<", (signature[0]).toString()); //簽名用MD5編碼獲得MD5加密後的應用的簽名信息 //Log.i("殺毒MD5<<<", md5); //查詢本地病毒簽名數據庫 // desc=virusDao.findVirus(md5); if(desc!=null){//查詢到病毒 VirusApp app=new VirusApp(); //獲得應用的圖標 Drawable drawable=pm.getApplicationIcon(info.applicationInfo); app.setmAppVirusIcon(drawable); //獲得應用名 String appName=(String) pm.getApplicationLabel(info.applicationInfo); app.setmAppVirusName(appName); //獲得該應用的病毒描述 app.setmAPPVirusDesc(desc); apps.add(app); app=null; //publishProgress(total,app); }else{ Log.i("殺毒MD5<<<","不是病毒"+total); } } //progressBar.setProgress(total); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute(); }
該界面有一個線性布局容器,可以實現動態加載數據的效果。
實現自定義對話框的布局:
package com.example.yqqmobilesafe; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.Dialog; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import com.example.yqqmobilesafe.db.dao.VirusDao; import com.example.yqqmobilesafe.domain.VirusApp; import com.example.yqqmobilesafe.utils.CopyFileToSystem; import com.example.yqqmobilesafe.utils.Md5Util; //本地病毒查殺 public class KillVirusActivity extends Activity { private LinearLayout mContainer; private TextView mKillVirusResult; private PackageManager pm; private ProgressBar progressBar; private ListpackageInfos; public KillVirusActivity() { } @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_kill_virus); mContainer=(LinearLayout) this.findViewById(R.id.ll_container); mKillVirusResult=(TextView) this.findViewById(R.id.tv_kill_virus_result); //progressBar=(ProgressBar) this.findViewById(R.id.pb_scanning_virus); copyVirusDB();//拷貝本地病毒簽名到系統目錄 scanVirus(); super.onCreate(savedInstanceState); } /** * 掃描病毒 並動態顯示在界面 */ private void scanVirus(){ new AsyncTask () { List apps=new ArrayList (); String desc=null; Dialog dialog=new Dialog(KillVirusActivity.this); View viewKilling=View.inflate(KillVirusActivity.this,R.layout.killing_dialog,null); @Override protected void onPreExecute() { mKillVirusResult.setText("正在掃描中,請稍等..."); dialog.setCancelable(false); dialog.setContentView(viewKilling); dialog.show(); pm=getPackageManager(); packageInfos=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES|PackageManager.GET_SIGNATURES); // progressBar.setMax(packageInfos.size()); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { dialog.dismiss(); if(apps.size()>0){ mKillVirusResult.setTextColor(Color.RED); mKillVirusResult.setText("掃描完成!發現病毒"+apps.size()+"個"+","+"請及時清理"); for(final VirusApp app:apps){ //有病毒 View view=View.inflate(KillVirusActivity.this,R.layout.app_virus_info_item,null); view.setClickable(true); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //卸載程序 Intent intent = new Intent(); intent.setAction("android.intent.action.DELETE"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:" + app.getmAppVirusName())); startActivity(intent); } }); ImageView appIcon=(ImageView) view.findViewById(R.id.iv_app_icon); TextView appName=(TextView) view.findViewById(R.id.tv_app_name); TextView appDesc=(TextView)view.findViewById(R.id.tv_app_virus_desc); appIcon.setBackground(app.getmAppVirusIcon()); appName.setText(app.getmAppVirusName()); appDesc.setText(app.getmAPPVirusDesc()); mContainer.addView(view); } }else if(apps.size()==0){ mKillVirusResult.setText("掃描完成!恭喜沒有發現病毒!"); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { int total=0; //獲得數據庫操作對象 VirusDao virusDao=new VirusDao(); //獲得設備上所有應用的簽名 for(PackageInfo info:packageInfos){ total++; Signature[] signature=info.signatures; try { String md5=Md5Util.encode(signature[0].toString()); desc=virusDao.findVirus(md5); Log.i("殺毒MD5<<<", md5); desc=virusDao.findVirus(md5); } catch (Exception e) { // TODO: handle exception } //Log.i("殺毒<<<", (signature[0]).toString()); //簽名用MD5編碼獲得MD5加密後的應用的簽名信息 //Log.i("殺毒MD5<<<", md5); //查詢本地病毒簽名數據庫 // desc=virusDao.findVirus(md5); if(desc!=null){//查詢到病毒 VirusApp app=new VirusApp(); //獲得應用的圖標 Drawable drawable=pm.getApplicationIcon(info.applicationInfo); app.setmAppVirusIcon(drawable); //獲得應用名 String appName=(String) pm.getApplicationLabel(info.applicationInfo); app.setmAppVirusName(appName); //獲得該應用的病毒描述 app.setmAPPVirusDesc(desc); apps.add(app); app=null; //publishProgress(total,app); }else{ Log.i("殺毒MD5<<<","不是病毒"+total); } } //progressBar.setProgress(total); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute(); } /** * 拷貝本地病毒庫數據庫到app目錄 */ private void copyVirusDB(){ new AsyncTask (){ @Override protected Void doInBackground(Void... params) { //獲得要拷貝到目的地的文件 File file=new File(getFilesDir(),"antivirus.db"); if(file.exists()&&file.length()>0){ }else{ //獲得文件輸入流 InputStream is=null; try { is=getResources().getAssets().open("antivirus.db"); CopyFileToSystem.copyFile(is, file.getAbsolutePath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }.execute(); } }
前言Android Studio對於快捷鍵的設置比較的靈活,開發者在從不同的平台轉移到Android Studio進行Android開發的時候,應該都能找到合適的KeyM
Android 的一個特色就是 application A 的 activity 可以啟動 application B 的 activity,盡管 A 和 B 是毫無干系
shape和selector是Android UI設計中經常用到的,比如我們要自定義一個圓角Button,點擊Button有些效果的變化,就要用到shape和select
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/39480503,本文出自:【張鴻洋的博客】上一篇已經