編輯:關於Android編程
public abstract class AbstractLoader extends AsyncTaskLoader {
public AbstractLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() { //會自動調用 step2
super.onStartLoading();
forceLoad();//開始執行任務
}
}
public class BitmapLoader extends AbstractLoader {
static final String IMG_URL = "http://h.hiphotos.baidu.com/image/pic/item/dcc451da81cb39db599abb5bd2160924ab183061.jpg";
public BitmapLoader(Context context) {
super(context);
}
@Override
public Bitmap loadInBackground() { //後台執行任務 step3
InputStream ins = HTTPHelper.httpGet(IMG_URL);
return BitmapFactory.decodeStream(ins);
}
}
public final class HTTPHelper {
public HTTPHelper() {
}
public static InputStream httpGet(String url) {
HttpURLConnection connection = (HttpURLConnection) createConnection(url, 5000);
try {
if (connection != null && connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return connection.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static URLConnection createConnection(String url, int timeout) {
try {
URLConnection urlConnection = (URLConnection) new URL(url).openConnection();
urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);
urlConnection.setDoInput(true);
return urlConnection;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
在Frament中使用
public class MyFragment extends Fragment implements LoaderCallbacks{
static final String TAG = "MyFragment";
private ImageView imageView;
public MyFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_img_layout, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG,"onViewCreated>>>" + getActivity());
imageView = (ImageView) view.findViewById(R.id.img);
//初始化任務執行 step1
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override //創建異步加載Loader
public Loader onCreateLoader(int arg0, Bundle b) {
return new BitmapLoader(getActivity());
}
@Override //任務執行完成後 直接更新UI step4
public void onLoadFinished(Loader arg0, Bitmap bitmap) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoaderReset(Loader loader) {
}
}
public class MainActivity extends FragmentActivity {
static String TAG = MainActivity.class.getCanonicalName();
public MainActivity() {
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new MyFragment()).addToBackStack(null).commit();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
}
現階段,我們創建了最簡單的Android項目,現在在此公布github鏈接https://github.com/neuyu/android-best-practices,
效果如下:布局文件activity_main.xml如下: MainActivity.java代碼如下:import android.app.Act
華為榮耀X2手機系統版本更新到B013版以後,鎖屏和密碼中增加了一項智能解鎖功能,聽起來都很高大上的功能,比指紋解鎖還NB,那這個智能解鎖功能怎麼用呢?下面
android_apk的在線安裝,除了要設計Android 客戶端的代碼外,還要搭建服務器的代碼,仿真實現中Android軟件的在線升級。 Android 客