[关闭]
@ZeroGeek 2015-10-27T03:01:35.000000Z 字数 3101 阅读 668

阅读Android官网示例的笔记

未分类


1. Fragment嵌入在Acitvity中时,需要在OnCreate中添加

  1. if (savedInstanceState == null) {
  2. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  3. RecyclerViewFragment fragment = new RecyclerViewFragment();
  4. transaction.replace(R.id.sample_content_fragment, fragment);
  5. transaction.commit();
  6. }

2. Fragment中在onCreateView,或onViewCreated中初始化View数据

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. // 。。。
  5. return inflater.inflate(R.layout.fragment_notification_builder, container, false);
  6. }
  1. @Override
  2. public void onViewCreated(View view, Bundle savedInstanceState) {
  3. super.onViewCreated(view, savedInstanceState);
  4. mNotificationManager = (NotificationManager) getActivity().getSystemService(
  5. Context.NOTIFICATION_SERVICE);
  6. mNumberOfNotifications = (TextView) view.findViewById(R.id.number_of_notifications);
  7. // Supply actions to the button that is displayed on screen.
  8. View.OnClickListener onClickListener = new View.OnClickListener() {};
  9. view.findViewById(R.id.add_notification).setOnClickListener(onClickListener);
  10. }

3. 基本网络操作

得到流

  1. private InputStream downloadUrl(String urlString) throws IOException {
  2. // BEGIN_INCLUDE(get_inputstream)
  3. URL url = new URL(urlString);
  4. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  5. conn.setReadTimeout(10000 /* milliseconds */);
  6. conn.setConnectTimeout(15000 /* milliseconds */);
  7. conn.setRequestMethod("GET");
  8. conn.setDoInput(true);
  9. // Start the query
  10. conn.connect();
  11. InputStream stream = conn.getInputStream();
  12. return stream;
  13. // END_INCLUDE(get_inputstream)
  14. }

流转化为字符串

  1. private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
  2. Reader reader = null;
  3. reader = new InputStreamReader(stream, "UTF-8");
  4. char[] buffer = new char[len];
  5. reader.read(buffer);
  6. return new String(buffer);
  7. }

封装操作

  1. /** Initiates the fetch operation. */
  2. private String loadFromNetwork(String urlString) throws IOException {
  3. InputStream stream = null;
  4. String str ="";
  5. try {
  6. stream = downloadUrl(urlString);
  7. str = readIt(stream, 500);
  8. } finally {
  9. if (stream != null) {
  10. stream.close();
  11. }
  12. }
  13. return str;
  14. }

通过AysncTask执行

  1. private class DownloadTask extends AsyncTask<String, Void, String> {
  2. @Override
  3. protected String doInBackground(String... urls) {
  4. try {
  5. return loadFromNetwork(urls[0]);
  6. } catch (IOException e) {
  7. return getString(R.string.connection_error);
  8. }
  9. }
  10. /**
  11. * Uses the logging framework to display the output of the fetch
  12. * operation in the log fragment.
  13. */
  14. @Override
  15. protected void onPostExecute(String result) {
  16. Log.i(TAG, result);
  17. }
  18. }

4. 在OnCreate里面需使用Intent传递的数据时,最好在onNewIntent中也加入处理。

相关介绍:
http://www.cnblogs.com/zenfly/archive/2012/02/10/2345196.html
http://gundumw100.iteye.com/blog/2160467

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.sample_main);
  5. if (getIntent() != null) {
  6. handleIntent(getIntent());
  7. }
  8. }
  1. @Override
  2. protected void onNewIntent(Intent intent) {
  3. handleIntent(intent);
  4. }
  1. private void handleIntent(Intent intent) {
  2. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  3. String query = intent.getStringExtra(SearchManager.QUERY);
  4. Bundle bundle = new Bundle();
  5. bundle.putString(QUERY_KEY, query);
  6. ContactablesLoaderCallbacks loaderCallbacks = new ContactablesLoaderCallbacks(this);
  7. getLoaderManager().restartLoader(CONTACT_QUERY_LOADER, bundle, loaderCallbacks);
  8. }
  9. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注