20个帮助提高开发技巧的开源 Android App

学习的最好方法是阅读,对于开发人员也是如此。如果你想成为一个更好的开发人员,你必须阅读更多的代码。就这么简单。

书籍、博客、论坛在某种程度上都是不错的,但一些功能齐全的详解的开源项目依然是无可替代的,整个应用中包含的所有资源都展现在你面前。

所以你需要做的就是坐下来、喝杯咖啡、拜读下很棒的代码。在这篇文章中,我们提供了一些来自各种类别和风格的最好的开源 Android 应用程序,以满足你所有的学习和开发需求。

在深入代码之前,你可以直接从 Play Store 中试用这些应用程序。每个应用程序附带的难度级别将帮助你判断是否应该立即深入了解它或暂时放在一边。

LeafPic

(Github | Play Store | 难度: 入门级)

照片和视频画廊应用是安卓系统最常见的一种应用。有没有想过他们是如何制作的? LeafPic 是最好的开源画廊应用之一,你可以试着使用它学习。

它非常简单,容易理解,而且完全适合任何初学者开发人员。同时我发现这个应用最好的一件事情就是实现了动态主题。这是许多 Android 开发人员难以正确实现的。

Simple Calendar

(Github | Play Store | 难度: 入门级)

一个纯粹使用 Kotlin 开发的又简单又易用的日历应用。如果你想要学习 Kotlin ,这可能是最好的入门方法之一。

这个应用的目标非常简单,就是让你亲力亲为的通过开发 Android 应用来学习一门全新的语言。还有一件更酷的事情就是你能够学习到如何开发一个自定义的 Android 桌面工具。

Amaze File Manager

(Github | [ur=”https://play.google.com/store/apps/details?id=com.amaze.filemanager”l]Play Store[/url] | 难度: 中等)

一个非常常见的安卓文件管理器,你能在几乎所有安卓设备上使用它。

尽管开发一个文件管理器应用初步看起来很简单,但是实际上想要良好地运行在所有安卓平台和设备上是很难的。

从这个应用中你可以学到很多东西,尤其是怎么适当处理 SD 卡中的文件。但是我不建议你遵循这个项目中的编码标准,因为那是没法达到的。

Easy Sound Recorder

(Github | Play Store | 难度: 入门级)

一个简单的、容易使用的、漂亮的安卓录音应用。如果你想要学习一些关于安卓音频记录和操作方面的东西,那么这个项目最适合你了。

这个项目非常小(仅仅只有一个简单的 Activity ),同样也很容易理解。初学者可以从这个项目中学到原质化设计的基础知识。

MLManager

(Github | Play Store | 难度: 入门级)

MLManager 是一个简单直观的安卓设备应用管理器。如果你想要学习一些关于你设备上已安装应用的详细信息,或者是从这些应用中提取 APK 、卸载应用等,那么这个项目是你的理想选择。

这个应用中使用的编码标准很好,应该被遵守。而且遵循它的原质化设计原则,也能给你在设计干净简单的应用时提供一些好点子。

PhotoAffix

(Github | Play Store| 难度: 入门级)

这个一个设计很简洁的应用,它能够水平或垂直地拼接照片。是不是听起来很简单?确实如此。

对于任何想要学习安卓开发基础的初学者来说,这是一个理想的项目。它的编码标准是一流的,而且是我心中最好的开发实践。

通过这个项目你还可以学习实现一些简单有用的自定义视图,为你打好基础,以便将来设计更复杂的视图。

MovieGuide

(Github | 难度: 中等)

这个应用的功能相当简单,就是列出所有流行电影的预告片和评论,但是这个项目真正有趣的地方是它的实现方式。

这个应用展示了一些非常棒的开发项目,像 MVP、Bob 的 Clean Architecture、Dagger 2 实现的依赖注入,和 RxJava 。

这个应用程序非常简单,但实现方式非常棒,绝对值得一看。

AnExplorer

(Github | | Play Store | 难度级别:中等)

另一个简单、轻量级和简约的文件管理器,专为手机和平板电脑设计。

从这个项目中可以学到很多事情,比如文件处理、root权限管理、装载程序、自定义视图等。 它内部代码设计良好,你不需要花费太多时间来掌握其内部发生的事情。

Minimal ToDo

(Github | Play Store | 难度: 入门级)

对于一个初学者,这是一个可以用来试水的项目,它非常简单而且很酷。 通过这个项目您将有机会了解安卓开发的大部分基础。

作为初学者的良好起点,这个应用程序的设计是非常得体的。 但是不要遵循它的编码标准和打包结构,因为它们不符合标准,应该避免使用。

Timber

(Github | Play Store | 难易程度: 高级)

Timber 是一款设计精美、功能齐全的 Android 音乐播放器。如果你想要建立自己的音乐播放器或任何音乐相关的应用程序,那么这是你需要查看的项目。

该项目规模相当大,正处于密集开发状态。对于初学者来说,掌握所有代码可能会有点困难,但对于任何中级或高级 Android 开发人员来说,这应该是非常有趣的。

AnotherMonitor

(Github | Play Store | 难度等级: 中等)

如果你想了解如何监控 Android 进程、内存使用情况、CPU 使用情况以及与之相关的部分,那么这就是开始学习的完美项目。

它的规模比较小并且很容易理解,但编码规范、之后的架构和总体设计并不符合规范,不宜参考。

InstaMaterial

(Github | 难度: 入门级)

如果您正在寻找一个项目来学习或提高您的材料设计语言(Material Design)技能,那么这个很合适。 该项目试图用材料设计语言复制 Instagram 应用程序的部分内容。

在这个应用中使用了大量的材料设计语言元素、动画和过渡效果,您可以在自己的项目中学习和实现。

它非常简单,易于理解,非常适合任何想要提高设计技能的 Android 开发人员。

CoCoin

(Github | 难度级别: 简单)

CoCoin 是一个详尽的个人财务和记账解决方案,运行在一个干净漂亮的用户界面之上。

如果你想了解如何正确管理大量的用户数据,并从这些数据中绘制漂亮的图表,制作一些很酷的自定义视图,那么这个开源仓库就是为你而设的。

OmniNotes

(Github | Play Store | 难度: 中等)

如果您正在建立一个功能齐全的,类似 Evernote 笔记本的 Android 应用程序,那么用它开始是个正确的决定。

该项目有相当多的功能,如共享和搜索笔记、附加图像、视频、音频、草图、添加提醒功能等等。

您可以从这个项目中学到的另一件很酷的事情是将您的应用程序与 Google Now 无缝集成。

Clip Stack

(Github | Play Store | 难度级别: 入门级)

Android 上一个简单、干净和优美的剪贴板管理器应用。该项目相当小,同时简单易懂。

但是,本项目中所使用的包结构、架构、命名约定和编码规范并不符合标准。它是按照一个非常简单和初学者友好的方式来构建的。

Super Clean Master

(Github | 难度级别: 高级)

如果你曾使用过 Android 设备,那么肯定需要从设备中清理一些垃圾数据。 Clean master 是全球最受欢迎的选择之一。

这个应用程序,顾名思义,尝试以非常干净和优雅的方式模拟 Clean Master 的大部分功能。但整体项目有点复杂,可能需要花一点时间来理解所有的代码。

Travel Mate

(Github | 难度: 中等)

如果您正在寻找构建基于旅行的应用程序,并且需要大量依靠位置和地图,那么这个项目可能是最好的起点。

该应用程序的设计和代码质量尚未达到标准,但总体设计非常好,有很多东西值得一些初学者甚至中级 Android 开发人员来学习。

KISS

(Github | Play Store | 难度:中等)

一个简单、超快速、轻便的 Android 应用。可以从这个项目中学到的几个很酷很漂亮的功能。

所以如果你想制作 Android 的启动器,这可能是最好的开始。该应用程序相当小,并且该项目也是相当简单的。

Turbo Editor

(Github | Play Store | 难度: 中等)

一个简单但功能强大的 Android 编辑器应用程序。 您当然也可以使用此编辑器编写代码,并支持不同编程语言的语法高亮。

我甚至尝试打开大文本文件,大多数应用都崩溃或打开失败了,这个应用则相当优雅地处理了它们。 从这个项目,您将有机会学习制作一个非常可靠和强大的文本(或代码)编辑器应用。

Wally

(Github| 难易程度: 入门级)

Android 上快速、简单和高效的壁纸应用程序。这个项目有很多可学习的东西,尤其是对于初学者。

在应用中使用的架构相当不错,这使得该应用很容易扩展和维护。这个应用的目标非常简单,但是为实现这个目标而采取的方法是值得借鉴的。

Pedometer

(Github | 难易程度: 入门级)

一个简单、轻量级的计步器应用程序,它使用硬件传感器来计算步数,这对电池性能几乎没有什么影响。

这是一个开始学习步数跟踪的好项目,但是编码标准和设计不够好,不建议参考。

我已经分享了几种来自各种渠道的开源 Android 应用程序,以满足几乎所有人的需求。 还有适合各类 Android 开发者的应用程序,难度从入门级到更专业的。

我希望你会发现这些开源项目真的很有用。 本文最初发布在TechBeacon上。

译文:20+ Awesome Open-Source Android Apps To Boost Your Development Skills

免费的Android UI库及组件推荐

短短数年时间Android平台就已经形成了一个庞大而活跃的开发者社区。许多社区开发的项目业已进入成熟阶段,甚至可以用于商业的软件生产中,且不用担心质量问题。

本文编译自androiduipatterns,意在分享一些免费、开源的Android UI库及组件资源。

一、UI组件库

1. GreenDroid 

该项目可以实现下列的UI设计:

  • 动作栏(Action Bar)
  • 快捷动作(Quick Action)
  • 工作区(Workspace)
  • 动态列表(Dynamic List)

以及许多更有用的组件。

项目网站 / 示例

2. ActionBarSherlock 

在Google针对手机的动作条API推出前,该项目一直与ICS一起使用。Jake最近将项目拓展到了动作条之外。这里也有针对工作区等的组件。

项目网站 / 示例

3. Android Compatibility Package

这是一个来自Google Android库的官方扩展。Compatibility Package顾名思义就是Android官方针对旧平台版本所不支持的API或是Android Framework没有提供的函数库,将其打包发布。它包含了许多非常有用的API。该包同样包含工作区UI设计的实现。

项目网站

二、UI组件

1. iosched 

这是一个官方的Google I/O的应用(同样可用在GDD,Google Developer Day)(注:Google I/O是Google每年都会举办的历时两天的开发者大会,该大会谈及各种技术问题。开发人员会提供非常多的示例来展示他们的技术。这个项目专门为了这个会议提供了一个Android应用程序。)。该应用程序实现了大量的UI设计,成为Google Android设备拥护者的参考标准。强烈推荐使用该应用程序的仪表盘UI设计模式。(Google代码链接:Dashboard.java

项目网站:http://code.google.com/p/iosched/

2. Android-Viewflow 

该项目的工作区UI设计模式与Google Docs所使用非常相似。

项目网站:https://github.com/pakerfeldt/android-viewflow

3. android-coverflow 

该项目可以用于完成一个cover flow。在这个项目网站的视频中可以得到更多的信息。

项目网站:http://code.google.com/p/android-coverflow/

4. android-viewbadger 

该项目让devs无需修改布局文件(layout file)即可显示badges。

项目网站:https://github.com/jgilfelt/android-viewbadger

5. android-pulltorefresh 

该项目实现动态列表/下拉刷新(pull to refresh)的UI设计。

项目网站:https://github.com/johannilsson/android-pulltorefresh

6. Android-ViewPagerIndicator 

该项目可以用于实现工作区的UI设计。

项目网站/示例

VIA http://www.androiduipatterns.com/2011/10/free-android-ui-library-component.html

Android模拟器镜像文件介绍

. Android模拟器采用的是开源工程Qemu,Qemu是Linux 下的著名模拟器

2. kernel-qemu是内核镜像

3. ramdisk.img是文件系统,如果你自己编译了android的源代码,它的内容为
out/debug/target/product/generic/root,mount到android的/

4. system.img主要存放系统数据,android启动后mount到/system
它包含的内容为out/debug/target/product/generic/system

5. 一般来说我们不直接使用userdata.img,而是使用userdata_qemu.data,用来存放用户数据,可读写,android启动后mount到 /data。只有使用-wipe-data参数启动模拟器是时候才会用到userdata.img,它会使用userdata.img的内容覆盖 userdata-qemu.img
userdata.img的内容为out/debug/target/product/generic/system

6. 如果您没有编译android的源代码,我们也可以知道ramdisk.img、system.img和userdata.img的内容。
1). 查看ramdisk.img内容
terminal中进入sdk的images目录,执行file *


我看可以看到ramdisk是一个gzip文件,我们可以解压这个gzip文件,解压前先备份一下
然后在terminal中运行
localhost:images wuvincent$ gzip -d -S .img ramdisk.img
然后运行
localhost:images wuvincent$ file ramdisk
执行后会显示这个文件是一个cpio文件,于是我们可以把它解压到一个目录下,依次执行如下命令
localhost:images wuvincent$ mkdir ramdiskdir
localhost:images wuvincent$ cd ramdiskdir
localhost:ramdiskdir wuvincent$ cpio -idmv <../ramdisk
我们就可以进入ramdiskdir看到ramdisk.img的内容了。
其中最主要的几个文件时init init.rc init.goldfish.rc
2). 查看system.img和userdata-qemu.img內容
terminal中進入sdk的tools目錄下運行adb shell
localhost:tools wuvincent$ adb shell
adb server is out of date.  killing…
* daemon started successfully *
#
然後運行cd /和ls -l,執行結果如下:

 


我們可以看到ramdisk.img mount到/,init init.rc init.goldfish.rc都在/目錄下。
而/system就是system.img mount後的目錄,/data 就是userdata-qemu.img mount後的目錄,大家可以進入看看內容。

我們也可以在terminal中運行mount命令查看文件系統mount情況。

原文:http://www.devdiv.com/home.php?mod=space&uid=1&do=blog&id=1488

Android 盘点所有Dialog 对话框 大合集 详解

雨松MOMO带大家盘点Android 中的对话框

今天我用自己写的一个Demo 和大家详细介绍一个Android中的对话框的使用技巧。


1.确定取消对话框

对话框中有2个按钮   通过调用 setPositiveButton 方法 和 setNegativeButton 方法 可以设置按钮的显示内容以及按钮的监听事件。

我们使用AlerDialog 创建对话框

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);

使用builder设置对话框的title button icon 等等

  1. builder.setIcon(R.drawable.icon);
  2.        builder.setTitle(“你确定要离开吗?”);
  3.        builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
  4.            public void onClick(DialogInterface dialog, int whichButton) {
  5.                //这里添加点击确定后的逻辑
  6.                showDialog(“你选择了确定”);
  7.            }
  8.        });
  9.        builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
  10.            public void onClick(DialogInterface dialog, int whichButton) {
  11.                //这里添加点击确定后的逻辑
  12.                showDialog(“你选择了取消”);
  13.            }
  14.        });
  15.        builder.create().show();

这个dialog用于现实onClick后监听的内容信息

  1. private void showDialog(String str) {
  2. w AlertDialog.Builder(MainDialog.this)
  3.      .setMessage(str)
  4.      .show();
  5. }

2.多个按钮信息框

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2. builder.setIcon(R.drawable.icon);
  3. builder.setTitle(“投票”);
  4. builder.setMessage(“您认为什么样的内容能吸引您?”);
  5. builder.setPositiveButton(“有趣味的”, new DialogInterface.OnClickListener() {
  6.     public void onClick(DialogInterface dialog, int whichButton) {
  7.         showDialog(“你选择了有趣味的”);
  8.     }
  9. });
  10. builder.setNeutralButton(“有思想的”, new DialogInterface.OnClickListener() {
  11.     public void onClick(DialogInterface dialog, int whichButton) {
  12.         showDialog(“你选择了有思想的”);
  13.     }
  14. });
  15. builder.setNegativeButton(“主题强的”, new DialogInterface.OnClickListener() {
  16.     public void onClick(DialogInterface dialog, int whichButton) {
  17.         showDialog(“你选择了主题强的”);
  18.     }
  19. });
  20. builder.create().show();

3.列表框

这个数组用于列表选择

  1. final String[] mItems = {“item0″,”item1″,”itme2″,”item3″,”itme4″,”item5″,”item6”};
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2.         builder.setTitle(“列表选择框”);
  3.         builder.setItems(mItems, new DialogInterface.OnClickListener() {
  4.             public void onClick(DialogInterface dialog, int which) {
  5.                 //点击后弹出窗口选择了第几项
  6.                 showDialog(“你选择的id为” + which + ” , ” + mItems[which]);
  7.             }
  8.         });
  9.         builder.create().show();

4.单项选择列表框

mSingleChoice 用于记录单选中的ID

  1. int mSingleChoiceID = -1;
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2. mSingleChoiceID = -1;
  3. builder.setIcon(R.drawable.icon);
  4.     builder.setTitle(“单项选择”);
  5.     builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
  6.         public void onClick(DialogInterface dialog, int whichButton) {
  7.                 mSingleChoiceID = whichButton;
  8.                 showDialog(“你选择的id为” + whichButton + ” , ” + mItems[whichButton]);
  9.         }
  10.     });
  11.     builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
  12.         public void onClick(DialogInterface dialog, int whichButton) {
  13.             if(mSingleChoiceID > 0) {
  14.             showDialog(“你选择的是” + mSingleChoiceID);
  15.             }
  16.         }
  17.     });
  18.     builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
  19.         public void onClick(DialogInterface dialog, int whichButton) {
  20.         }
  21.     });
  22.    builder.create().show();

5.进度条框

点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100
Progress在小于100的时候一直在线程中做循环++ 只到读取结束后,停止线程。

  1.           mProgressDialog = new ProgressDialog(MainDialog.this);
  2.      mProgressDialog.setIcon(R.drawable.icon);
  3.      mProgressDialog.setTitle(“进度条窗口”);
  4.      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  5.      mProgressDialog.setMax(MAX_PROGRESS);
  6.      mProgressDialog.setButton(“确定”, new DialogInterface.OnClickListener() {
  7.          public void onClick(DialogInterface dialog, int whichButton) {
  8.              //这里添加点击后的逻辑
  9.          }
  10.      });
  11.      mProgressDialog.setButton2(“取消”, new DialogInterface.OnClickListener() {
  12.          public void onClick(DialogInterface dialog, int whichButton) {
  13.              //这里添加点击后的逻辑
  14.          }
  15.      });
  16.      mProgressDialog.show();
  17.      new Thread(this).start();
  18. ic void run() {
  19. int Progress = 0;
  20. while(Progress < MAX_PROGRESS) {
  21. try {
  22.     Thread.sleep(100);
  23.     Progress++;
  24.     mProgressDialog.incrementProgressBy(1);
  25. } catch (InterruptedException e) {
  26.     // TODO Auto-generated catch block
  27.     e.printStackTrace();
  28. }
  29. }

6.多项选择列表框

MultiChoiceID 用于记录多选选中的id号 存在ArrayList中
选中后 add 进ArrayList
取消选中后 remove 出ArrayList

  1. ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2. MultiChoiceID.clear();
  3. builder.setIcon(R.drawable.icon);
  4.     builder.setTitle(“多项选择”);
  5.     builder.setMultiChoiceItems(mItems,
  6.             new boolean[]{false, false, false, false, false, false, false},
  7.             new DialogInterface.OnMultiChoiceClickListener() {
  8.                 public void onClick(DialogInterface dialog, int whichButton,
  9.                         boolean isChecked) {
  10.                    if(isChecked) {
  11.                        MultiChoiceID.add(whichButton);
  12.                        showDialog(“你选择的id为” + whichButton + ” , ” + mItems[whichButton]);
  13.                    }else {
  14.                        MultiChoiceID.remove(whichButton);
  15.                    }
  16.                 }
  17.             });
  18.     builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
  19.         public void onClick(DialogInterface dialog, int whichButton) {
  20.             String str = “”;
  21.             int size = MultiChoiceID.size();
  22.             for (int i = 0 ;i < size; i++) {
  23.             str+= mItems[MultiChoiceID.get(i)] + “, “;
  24.             }
  25.             showDialog(“你选择的是” + str);
  26.         }
  27.     });
  28.     builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
  29.         public void onClick(DialogInterface dialog, int whichButton) {
  30.         }
  31.     });
  32.    builder.create().show();

7.自定义布局


讲到自定义布局我就得多说一说了,为什么要多说一说呢?
其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。

自定义dialog有什么好处?

比如我们在开发过长当中 要通过介绍系统发送的一个广播弹出一个dialog . 但是dialog必需是基于activity才能呈现出来 如果没有activity 的话 程序就会崩溃。所以我们可以写一个自定义的 dialog 把它定义成一个activity
这样我们收到一条打开dialog的广播后 直接启动这个 activity  程序正常运行~~

这就是自定义dialog的好处。

注明:下面这个例子只是写了自定义dialog 没有把它单独的写在一个activity中 如果须要的话 可以自己改一下。

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2.  LayoutInflater factory = LayoutInflater.from(this);
  3.  final View textEntryView = factory.inflate(R.layout.test, null);
  4.      builder.setIcon(R.drawable.icon);
  5.      builder.setTitle(“自定义输入框”);
  6.      builder.setView(textEntryView);
  7.      builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
  8.          public void onClick(DialogInterface dialog, int whichButton) {
  9.          EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
  10.          EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
  11.          showDialog(“姓名 :”  + userName.getText().toString()  + “密码:” + password.getText().toString() );
  12.          }
  13.      });
  14.      builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
  15.          public void onClick(DialogInterface dialog, int whichButton) {
  16.          }
  17.      });
  18.    builder.create().show();
  1. <span style=”color:#000000;”><?xml version=”1.0″ encoding=”utf-8″?>
  2. <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_height=”wrap_content”
  4. android:layout_width=”wrap_content”
  5. android:orientation=”horizontal”
  6. android:id=”@+id/dialog”>
  7. <LinearLayout
  8. android:layout_height=”wrap_content”
  9. android:layout_width=”wrap_content”
  10. android:orientation=”horizontal”
  11. android:id=”@+id/dialogname”>
  12. <TextView android:layout_height=”wrap_content”
  13.    android:layout_width=”wrap_content”
  14.   android:id=”@+id/tvUserName”
  15.   android:text=”姓名:” />
  16. <EditText android:layout_height=”wrap_content”
  17.   android:layout_width=”wrap_content”
  18.   android:id=”@+id/etUserName”
  19.   android:minWidth=”200dip”/>
  20. </LinearLayout>
  21. <LinearLayout
  22. android:layout_height=”wrap_content”
  23. android:layout_width=”wrap_content”
  24. android:orientation=”horizontal”
  25. android:id=”@+id/dialognum”
  26.  android:layout_below=”@+id/dialogname”
  27. >
  28.   <TextView android:layout_height=”wrap_content”
  29.    android:layout_width=”wrap_content”
  30.   android:id=”@+id/tvPassWord”
  31.   android:text=”密码:” />
  32. <EditText android:layout_height=”wrap_content”
  33.   android:layout_width=”wrap_content”
  34.   android:id=”@+id/etPassWord”
  35.   android:minWidth=”200dip”/>
  36.  </LinearLayout>
  37.   </RelativeLayout></span>


8.读取进度框

显示一个正在转圈的进度条loading

 

  1. mProgressDialog = new ProgressDialog(this);
  2.  mProgressDialog.setTitle(“读取ing”);
  3.  mProgressDialog.setMessage(“正在读取中请稍候”);
  4.  mProgressDialog.setIndeterminate(true);
  5.  mProgressDialog.setCancelable(true);
  6.  mProgressDialog.show();

最后如果你还是觉得我写的不够详细 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习 雨松MOMO希望可以和大家一起进步。

Android开发几个必去的网站

1 Eoe Android开发者门户

简介:有许多技术性文章,人气很旺,经常有些很酷的android设置,android新闻报道的不是很及时

网址: http://www.eoeandroid.com/portal.ph

移动App资讯站(推荐)

简介: Android开发者必去的一个网站,可以及时了解android开发全球最新消息,同时也有最新手机软件开发业内报道有句很经典的话
只有NEWS.

网址: http://www.ydapp.com/

3 Android Police

简介:国外的网站,主要报道最新Android产品

网址:http://www.androidpolice.com/

有米广告

简介:这个应该大家不陌生吧国内第一家移动APP广告商 ,广告单价很高

网址: http://www.youmi.net/

移动Labs(中国移动研究院)

简介:经常出一些经典的文章比较深入的说明移动开发行业动态.了解行业走向,及发展趋势.

网址: http://labs.chinamobile.com/

Android Developer Income Report

A lot of people says that there is no real money in the Android development. They say that if you want to make money you should write for iPhone, iPad and all other iThings… This is not true! I am not a one of guys that is making thousands of dollars but my income seems to be steady and is still growing.

Moreover I am not one of top developers nor any of my apps have been promoted by Android Market. I am just an one among of thousands of Android developers with not to well known apps. And what may be really surprising all my apps are free as Google do not allow developers from my country (Poland) to sell apps via Android Market!

So keep in mind these facts:

  1. None of my apps has been ever promoted in Top of Android Market
  2. I am providing only free apps (mostly due of Android Market limitations)
  3. Even if I would be able to sell apps I would not use it as main income source… (I believe that you still can make more from ads…)

All my income comes from ads that are included in my mobile apps for Android. Here is the list with current number of downloads:

  1. X-Ray Scanner (over 268 000 downloads)
  2. Cracked Screen (over 182 000 downloads)
  3. Virtual Drums (over 20 000 downloads)
  4. Daily Beauty Tips (over 11 000 downloads)
  5. Don’t push it (over 6 500 downloads)
  6. WP Stats (over 4 000 downloads)

I have started to learn Android Development on April 2010. My first application was ready to be published on May. And it bring me first few dollars… I was not satisfied as I have been expecting that this app (WP Stats) will be really popular… Unfortunately it wasn’t… Anyway I have published a few more apps that got a lot more popularity… So here is my total income breakdown:

  • May 2010 – $4.92
  • June 2010 – $138.87
  • July 2010 – $538.26
  • August 2010 – $920.00
  • September 2010 – $1545.45
  • October 2010 – $1059.31

October looks to be lower in earning but it happened only because I have not been updating any of my apps in this month (I have been moving to new house and had no time for it…).

So as you may see income has not been high on the begging but with each month with regular updates and new apps it has been growing very rapidly!

And I will say it again… I have not made a single cent from selling apps – all my income is only from ads. This month I am preparing new updates for my apps and I am finalizing some new ideas… so my income should start growing up again…

All comments are welcome and very desired.

Android application四大组件的作用

我简要的介绍下这四大组件,希望与你们一起学习共同进步!
Activity:Activity是Android程序与用户交互的窗口,是Android构造块中最基本的一种,它需要为保持各界面的状态,做很多持久化的事情,妥善管理生命周期以及一些跳转逻辑

service:后台服务于Activity,封装有一个完整的功能逻辑实现,接受上层指令,完成相关的食物,定义好需要接受的Intent提供同步和异步的接口

Content Provider:是Android提供的第三方应用数据的访问方案,可以派生Content Provider类,对外提供数据,可以像数据库一样进行选择排序,屏蔽内部数据的存储细节,向外提供统一的借口模型,大大简化上层应用,对数据的整合提供了更方便的途径

BroadCast Receiver:接受一种或者多种Intent作触发事件,接受相关消息,做一些简单处理,转换成一条Notification,统一了Android的事件广播模型

Android多线程断点续传下载文件类设计

对于Android平台,很多网友可能考虑开发一个软件商店,对于Android平台上如何实现断点续传操作呢? 这里给大家一些思路和原理的介绍,同时在Android手机上要考虑的一些事情。

1. 流量控制,获取运营商的接入方式,比如说使用移动网络接入,尽可能的提示用户切换WiFi或提示,限制下载的流量以节省话费。

2. 屏幕锁控制,屏幕锁屏后导致应用会被挂起,当然Android提供了PowerManager.WakeLock来控制。

3. 对于断点续传,这要追溯到Http 1.1的特性了,主要是获取文件大小,如果这个无法读取的话,那么就无法断点续传了只能使用chunked模式了,当然获取远程服务器上文件的大小可以通过Http的响应头查找Content-Length。

4. 获取上次文件的更改时间,对于断点续传来说比较有风险的就是 继续下载的文件和早期下载的在server上有变动,这将会导致续传时下载的文件版本和原始的不同,一般有两种解决方法,早期我们配置服务器时通过Last-Modified这个http header获取文件上次修改时间,不过本次Android开发网推荐使用更为强大的ETag,ETag一般用于解决同一个URL处理不同返回相应,比如Session认证,多国语言,以及部分黑帽的SEO中。具体的实现大家可以参考RFC文档。

5. 考虑服务器的3xx的返回,对于专业的下载文件服务器会考虑到负载平衡问题,这就涉及到重定向问题,处理重定向使用Android的Apache库处理比较好。

6. 至于多线程,这里CWJ提示大家可能存在独立的线程下载一个文件,和多个线程分块下载单个文件之分,其中后者需要考虑上次下载数据是否存在问题,同时如果服务器不支持文件大小获取,则无法通过分段下载数据,因为不知道如何分段,所以在chunked模式中,只能使用一个线程下载一个文件,而不是多个线程下载一个文件。

7. 下载后的数据效验,可以考虑CRC等方式,当然对于一般的传输只要逻辑不出现问题,基本上不会有偏差。

8. 考虑DRM问题,这个问题在国内用的比较少,而国外的受数字保护的音乐和视频,需要额外的获取证书等。

9. 重试次数,对于一个文件可能在本次网络传输中受到问题,尤其是移动网络,所以可以设置一定的重试次数,让任务单独的走下去。

10. 线程开发方式,这里如果你的Java基础比较好,推荐直接使用Java并发库API比较好,如果过去只做过Java开发使用Thread即可,如果Java技术不过关可以Android封装的AsyncTask。

原文地址:http://www.android123.com.cn/androidkaifa/932.html

android 如何保存簡單的配置信息(SharedPreferences、File和Properties)

我們知道在android的開發中,保存項目私有數據的存儲方式我們可以使用:SharedPreferences,File,SQLite,Network.四種方式,而要用到應
用程序之間數據的共享要使用ContentProvider 。那今天我們只敘述一下僅僅保存一些我們登錄等的一些配置信息的數據,也就是說用到的數
據量都不是很大,那麼我們就可以選擇SharedPreferences和File的方式。這裡只針對性的結合File和Properties進行敘述。
一。SharedPreferences

1. 它可以保存上一次用戶所做的修改或者自定義參數的設定,當再次啟動程序後依然可以保持原有的設置。這裡只說明一下使用方式。比如下
面的代碼在OnCreate中使用:

SharedPreferences mSharedPreferences = getSharedPreferences(“list”,MODE_PRIVATE);

String mTempString = mSharedPreferences.getString(“config”,”default”);

其中”list”是SharedPreferences的文件的名字,SharedPreferences是以鍵值映射的關係存放數據。不過多解釋,你也可以這樣用:

SharedPreferences mSharedPreferences = getPreferences(MODE_PRIVATE);

這樣默認的文件名是activity的名字。

2. 退出activity的時候保存數據,在OnPause中使用:

SharedPreferences mSharedPreferences = getSharedPreferences(“list”,    MODE_PRIVATE);

mSharedPreferences.edit().putString(“config”,”data” ).commit();

3. SharedPreferences 是以xml文件的方式自動保存的,在DDMS中的FileExplorer中展開/data/data/包名/shared-prefs下面就是SharedPreferences文件。

4. SharedPreferences文件只可以用來存放基本的數據類型。
二。結合File和Properties進行保存。

A Properties object is a Hashtable where the keys and values must be Strings. Each property can have a default Properties list which specifies the default values to be used when a given key is not found in this Properties instance.

1.所以,Properties對象也是一個哈希表,也是一個鍵值對應的關係,因此和上面的操作相似。下面看具體的程序。

public class File_ByProperties extends Activity {

private boolean mStatus;

private TextView mShowStatus;
/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mShowStatus = (TextView) findViewById(R.id.show);

load();

}
private void load() {

// TODO Auto-generated method stub

Properties mProperties = new Properties();

try {

FileInputStream mInputStream = openFileInput(“configuration”);

mProperties.load(mInputStream);

mStatus = Boolean.valueOf(mProperties.get(“status”).toString());

mShowStatus.setText(“the status is : ” + mStatus);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

System.out.println(e.toString());

} catch (IOException e) {

System.out.println(e.toString());

}

}
@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

// TODO Auto-generated method stub

if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {

mStatus = !mStatus;

mShowStatus.setText(“the status is : ” + mStatus);

}

return super.onKeyDown(keyCode, event);

}
@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

Properties mProperties = new Properties();

if (mProperties.containsKey(“status”)) {

mProperties.remove(“status”);

}

mProperties.put(“status”, String.valueOf(mStatus));

try {

FileOutputStream mOutputStream = openFileOutput(“configuration”,     MODE_WORLD_WRITEABLE);

mProperties.store(mOutputStream, null);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}
}

2. 在DDMS中的FileExplorer中展開/data/data/包名/files,可以查看到該文件。
三。你還可以將一個靜態的文件放到res/raw/下面,然後通過getResources().openRawResource(R.raw.文件);來得到一個InputStream對象,然後讀取文件的內容。

Google Android Tutorial

Google has recently released the Android platform for developing mobile applications. The language used for developing Android programs is Java, but it is not Java Micro Edition.  No wireless application developer can ignore  Android. Google is the best known brand name, among the users of the web and Android comes from Google. 


I am presenting this hands-on tutorial, as a sequel to my j2me series. Adequate knowledge of core-java ,especially Event-handling, Swing and inner-classes is assumed. Though Android does not make use of Swing, it uses  similar ideas.


We can develop Android lessons and applications in Eclipse environment. Google have provided an Eclipse-plugin for Android. This is the popular method. Google has not given direct support to Netbeans. But some Netbeans users have developed a method for running Android in Netbeans . It is available at  http://undroid.nolimit.cz/. You can find more screenshots and guidance in http://eppleton.com/blog/.


We can develop Android lessons without using either Eclipse or Netbeans. The necessary command-line tools have been provided by Google. I found that using these command-line tools is easier than either Eclipse or Netbeans method. So, I am basing all the following lessons on these tools. I think, most readers will agree with my view, after they try this method as well as Eclipse method. The Android site at ‘code.google.com/android’ has already given step-by-step instructions about Android in Eclipse. You can also get more details with screen shots from  a wonderful website at www.tanguay.info/web/welcome.php titled ‘Edward’s Web Developer site’. He gives excellent guidance with plenty of screen shots 


The Android site lacks clarity about the command-line method. Hence, I think I am adding something useful by writing on the command-line method instead of simply repeating the material in Android site.
Let us start from the beginning. The first step is downloading the Android SDK (version m5-rc14, Feb-12, 2008). Android was released in November, 2007 . It has been revised in the Feb-2008 version.Some of the earlier examples may not work in the newer version.


I am working in Windows-2000 and so I downloaded the windows version. The supported platform in Windows is either Windows-XP or Vista.(Mac OS 10 & Ubuntu Linux are the other platforms mentioned). However, it works well in my Win-2000. It is advisable to have at least 512MB memory. The android SDK is a zip file. I unzipped it to C:\unzipped\android and later, I copied that folder to D:\android. If you want, you can simply copy it to another drive like G:\android also. In the following lessons D:\android is used.


If you want to develop using Eclipse, you must have installed either Eclipse3.2 or Eclipse3.3(Europa). I have tested with Eclipse3.2. No problem.It works. But, we require ADT  (ie) Android Development Tools plugin for Eclipse, if you are using Eclipse.You can get this plugin from http://code.google.com/android/adt_download. You have to be careful about the ADT version number.It is ADT-0.3.3.



As my present focus is on command-line method, let me begin straight away and give a simple demo.The procedure given here is common for all our experiments and so I will not be repeating it in each demo. So, please note it down carefully.


Demo 1 – TextBox


In my first demo, I will have a customary button and textbox ( called EditField in Android). When I click the button, I want the message “SUCCESS!” to be displayed in textbox. Just as an exercise, I am using two buttons and two textboxes.



The first step is to start the Emulator


cd to d:\android\tools

d:\android\tools>emulator


It will take a long time to get started. Do not be in a hurry. Wait till it gets fully started. And do not  close that window carelessly by mistake. In that case, you will have to start it again and wait for a long time again. Finally, we get the emulator screen
 


The second step is to give the following command, from another command window.


d:\android\tools>

activityCreator –out  demo                                   mypack.mydemos.demo


This means that my project is ‘demo’ and my package is ‘mypack.mydemos’. A number of folders are created automatically by this command:



  • tools\demo\src
  • tools\demo\bin
  • tools\demo\res.  

We need to note  the src and res folders carefully. We will place the java source file in src folder and main.xml file in res\layout, overwriting any files that are generated automatically. For the moment, we can think of the res\layout folder as the one which decides the gui design. As in asp.net, flex etc, the gui details are specified in xml file. But how shall we write the XML file? by hand? Not too difficult .But….luckily, there is an open-source gui designer named ‘DroidDraw’ available in http://www.droiddraw.org/ .It is a nice tool and if you like it, you can send your appreciation to brendan.d.burns@gmail.com. He has given a simple tutorial too, on how to use this gui tool.


I downloaded this software from the above site. I unzipped it. ( any folder). When we click on the icon, we get the screen as given below.


Droid DrawDroid Draw


 


Drawing Canvas AreaDrawing Canvas Area



Toolbox &amp; Blank AreaToolbox & Blank Area 


Thus we get a window, showing the drawing canvas on leftside and toolbox and a blank area in the rightside. ( for printing purpose, I have split them into two screens) as above.



From the toolbox, we learn that we are having controls like button,check,radio,spinner,edittext(textbox) and  textview (label) etc. There is also a combo  to choose  layout, in the canvas screen.  I am choosing ‘absolute layout’. I simply drag and drop a button and an editview on the canvas.(Drag and drop, do not click and drop! It won’t work).


You will notice a number of tabs in toolbox. Select ‘properties’ tab.. After clicking the button on the canvas, give the id property of button as @id/button1. Similarly, for editview as @id/text1. Also, button2 and text2 After this, just click the ‘generate’ button at the bottom of  the blank window. We get the following XML file(main.xml) automatically generated.





  1. <?xml version=“1.0” encoding=“utf-8”?>  

  2. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  

  3.     android:layout_width=“fill_parent” android:layout_height=“wrap_content”  

  4.     android:padding=“10px”>  

  5.     <EditText android:id=“@+id/text1” android:layout_width=“fill_parent”  

  6.         android:layout_height=“wrap_content” />  

  7.     <Button android:id=“@+id/button1” android:layout_width=“wrap_content”  

  8.         android:layout_height=“wrap_content” android:layout_below=“@id/text1”  

  9.         android:layout_alignParentRight=“true” android:layout_marginLeft=“10px”  

  10.         android:text=“click” />  

  11.     <EditText android:id=“@+id/text2” android:layout_width=“fill_parent”  

  12.         android:layout_height=“wrap_content” android:layout_below=“@id/button1” />  

  13.     <Button android:id=“@+id/button2” android:layout_width=“wrap_content”  

  14.         android:layout_height=“wrap_content” android:layout_below=“@id/text2”  

  15.         android:layout_alignParentRight=“true” android:layout_marginLeft=“10px”  

  16.         android:text=“click” />  

  17. </RelativeLayout>  

We can now create our java source file. The code refers to main.xml for id of the controls.
(d:\android\mydemos\ex1\demo.java).This is our work folder.





  1. package mypack.mydemos;   

  2.   

  3.   

  4. import android.app.Activity;   

  5. import android.os.Bundle;   

  6. import android.view.View;   

  7. import android.widget.*;   

  8.   

  9.   

  10. public class demo extends Activity     

  11. {   

  12.   

  13. Button     button1,button2;   

  14. EditText   text1,text2;   

  15.   

  16.     @Override  

  17.   

  18.     public void onCreate(Bundle icicle)   

  19.     {   

  20.         super.onCreate(icicle);   

  21.         setContentView(R.layout.main);   

  22.     text1=  (EditText) findViewById(R.id.text1);   

  23.     button1 = (Button) findViewById(R.id.button1);   

  24.   

  25.     text2=  (EditText) findViewById(R.id.text2);   

  26.     button2 = (Button) findViewById(R.id.button2);   

  27.     button1.setOnClickListener(new clicker());   

  28.     button2.setOnClickListener(new clicker());   

  29.    }   

  30.   

  31.  //———————————–   

  32.   

  33.  class  clicker implements Button.OnClickListener   

  34.   {                  

  35.   public void onClick(View v)   

  36.      {   

  37.      if(v==button1){text1.setText(“welcome”); }   

  38.      if(v==button2){text2.setText(“hello”);   }        

  39.      }   

  40.   

  41.   }   

  42.   

  43. }  

The Android documentation and sample programs use anonymous inner class. I think it is quite unnecessary and is very tedious to follow. Instead, I have used user-defined ‘clicker’. This makes the code cleaner and more readable. This is just like any Swing program. In Android Terminology, an Activity is like a frame in swing (a screen). Just like ActionListener, here also we have, OnClickListener. I am not going into detailed theory now. I just  want to show how to develop and run a program first. Theory will follow later.


We have to copy this file(demo.java) to D:\android\tools\demo\src\mypack\mydemos


Go to d:\android\tools\demo

Give path= c:\winNT\system32;c:\jdk1.5\bin;e:\ant1.6\bin

(carefully note that this will not work with jdk1.4.2. It requires jdk1.5).


Secondly, how about the reference to Ant? Ant is a famous build tool from Apache Software foundation. Android requires the latest version of Ant for Windows(ie) Ant1.6. Do I have to know how to write the Ant’s build.xml file? NO. It is automatically created by the command.


So, I downloaded ant1.6 from the Apache website. It is a compact zip file. I have unzipped it and placed it as E:\ant1.6). Now use the command ‘ant’


d:\android\tools\demo>ant

We will get a series of messages. If we had done the previous steps correctly, we will get the message ‘BUILD SUCCESSFUL”.  Otherwise, we will get error messages, with line numbers where the errors occurred. We can correct them and build again.


The build process would have created a zip file named ‘demo.apk’ in demo\bin folder. All that remains now is to run the program in the emulator. As you remember, we have already started the emulator and it is running.


Now copy d:\android\tools\demo\bin\demo.apk  to d:\android\tools. After copying, give the command as:


…\tools>adb   install    demo.apk

After giving this command. go to the emulator window. You will find a checkpattern displayed for a while. Then an additional button appears in the screen with caption ‘demo’. Our program has been installed. We can test it by clicking on this ‘demo’button.


 



You can execute the program ‘demo’ now. Two textboxes and two buttons will appear. Click on button1. ‘welcome’ will appear in text1. Click on button2.’how are you?’ will appear in text2. The result is shown  below.



That completes our first  demo in  android.


We will be following exactly same procedure for all our demos. We will hereafter, see xml file and java files only, for the various demos. After a few more demos, it will be enough if I give the imports, declaration, definition and event handler.


The Android SDK comes with a number of sample applications. Within the APIDemos folder, we have a folder named ‘Views‘. I read it as ‘GUI’. It deals with layouts and controls and animations etc. There are a lot of demos. It may be confusing at first. Though, we may like to modify and simplify the code later, it is instructive to try each one of the sample programs by clicking on ‘apidemos’ button in the emulator screen and getting familiarity . I will list and give a brief comment on these, later.


 


Demo 2 – Spinner


As usual, the standard widgets are label(textview), textbox(edittext), combo(spinner), check, radio, ticker etc. I will now give a  demo for spinner. I have provided a spinner, button and a text to display the selected item in edittext.


We can design our layout as before using DroidDraw and get the following main.xml.


(obtained by using DroidDraw).





  1. <?xml version=“1.0” encoding=“utf-8”?>  

  2. <AbsoluteLayout android:id=“@+id/widget0”    

  3.     android:layout_width=“fill_parent”    

  4.      android:layout_height=“fill_parent”    

  5.       xmlns:android=“http://schemas.android.com/ apk/res/android”>                                               

  6. <Spinner  android:id=“@+id/spinner1” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_x=“70px” android:layout_y=“42px”>                                               

  7. </Spinner>  

  8. <Button android:id=“@+id/button1” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“confirm” android:layout_x=“70px” android:layout_y=“112px”>                                           </Button>                                      

  9. <EditText android:id=“@+id/text1” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“EditText” android:textSize=“18sp” android:layout_x=“70px” android:layout_y=“182px”>  

  10. </EditText>  

  11. </AbsoluteLayout>  

The corresponding java source file is given below.





  1. package  mypack.mydemos;         

  2.   

  3. import android.app.Activity;                     

  4. import android.os.Bundle;                        

  5. import android.widget.*;      

  6. import android.view.View;   

  7.   

  8. public class  demo extends Activity              

  9. {                                                       

  10.         Spinner   spinner1;  

  11.         Button    button1;             

  12.         EditText   text1;    

  13.   

  14.       @Override                                                  

  15.       protected void onCreate(Bundle icicle)     

  16.   

  17.       {         

  18.                 super.onCreate(icicle);                             

  19.                 setTheme(android.R.style.Theme_Dark);  

  20.                 setContentView(R.layout.main);     

  21.     spinner1 = (Spinner)    

  22.     findViewById  (R.id.spinner1);     

  23.     button1    = (Button);  

  24.     findViewById (R.id.button1);                                                 

  25.     text1      = (EditText)    

  26.     findViewById  (R.id.text1);          

  27.     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);           

  28.     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ item);                      

  29.     spinner1.setAdapter(adapter);           

  30.     button1.setOnClickListener(  new clicker());      

  31.    }            

  32.   

  33.          private static final String[] array = {        “sunday”“monday”“tuesday”“wednesday”,  

  34.                          “thursday”“friday”“saturday” };     

  35.   

  36. class  clicker implements  Button.OnClickListener   

  37.   

  38. {   

  39.     public   void  onClick(View   v)  

  40.               {           

  41.            String       s = (String) spinner1.getSelectedItem();   

  42.              text1.setText(s);                                  

  43.     }                                            

  44.   

  45. }      

  46.   

  47.   

  48. }  

As before place demo.java in d:\android\tools\demo\src\mypack\mydemos. Place main.xml in d:\android\tools\demo\res\layout


Build using ant. Deploy demo.apk to the emulator exactly as in previous demo. The original demo gets overwritten. But, our work folder , where we have our xml and java files is d:\android\mydemos\ex1.They  are intact. So, no problem..The current java and xml files are in d:\android\mydemos\ex2.


When we click on the spinner, we get the items displayed as drop-down. We select an item and confirm. The selected item appears in text1. I am not going to explain the code. It is simple enough, if we remember our core java.



Demo 3 – Ticker


The third demo is a ‘ticker’ having a textbox for entering the ticker’s text, a ticker(timer) , two labels(editview) one for status and the other for diplaying the sliding message.





  1. <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android   

  2.    android:layout_width=“fill_parent”       

  3.    android:layout_height=“fill_parent”       

  4.    android:orientation=“vertical”>                                                                              

  5. <EditText android:id=“@+id/text1”           

  6.    android:layout_width=“fill_parent”           

  7.    android:layout_height=“wrap_content” />                                                                             

  8. <Ticker android:id=“@+id/ticker1”           

  9. android:layout_width=“fill_parent”           

  10. android:layout_height=“wrap_content”    

  11. android:layout_marginTop=“20dip” />                                                                      

  12. <TextView android:id=“@+id/label1”               

  13.   android:layout_width=“wrap_content”               

  14.   android:layout_height=“fill_parent” />                                                                       

  15.  </Ticker>  

  16. <TextView android:id=“@+id/label2”           

  17.   android:layout_width=“fill_parent”           

  18.   android:layout_height=“wrap_content”           

  19.    android:layout_marginTop=“20dip”  />                                                             

  20. </LinearLayout>  

 LinearLayout with vertical orientation is like FlowLayout but in a vertical direction.






  1. package mypack.mydemos;   

  2.   

  3. import android.app.Activity;   

  4. import android.os.Handler;   

  5. import android.os.Bundle;   

  6. import android.view.View;   

  7. import android.view.View.OnClickListener;   

  8. import android.widget.*;   

  9. import java.util.Map;   

  10.   

  11. public class demo extends Activity   

  12.   

  13. implements Ticker.TickerListener {   

  14.     Ticker ticker1;   

  15.     TextView label1, label2;   

  16.     EditText text1;   

  17.   

  18.     @Override  

  19.     protected void onCreate(Bundle icicle) {   

  20.         super.onCreate(icicle);   

  21.         setContentView(R.layout.main);   

  22.         ticker1 = (Ticker) findViewById(R.id.ticker1);   

  23.         label1 = (TextView) findViewById(R.id.label1);   

  24.         label2 = (TextView) findViewById(R.id.label2);   

  25.         text1 = (EditText) findViewById(R.id.text1);   

  26.         ticker1.setTickerListener(this);   

  27.         text1.setOnClickListener(new clicker());   

  28.     }   

  29.   

  30.     class clicker implements EditText.OnClickListener {   

  31.         public void onClick(View v) {   

  32.   

  33.             label1.setText(text1.getText());   

  34.             ticker1.startTicker();   

  35.             label2.setText(“Ticking…”);   

  36.         }   

  37.     }   

  38.   

  39.     public void onTickerFinished(Ticker view) {   

  40.         label2.setText(“Done!”);   

  41.     }   

  42.   

  43. }  

 


Demo 4 – Checkbox 


Copy this to tools\demo\res\layout\


 





  1. <?xml version=“1.0” encoding=“utf-8”?>  

  2. <AbsoluteLayout android:id=“@+id/widget1”  

  3.     android:layout_width=“fill_parent” android:layout_height=“fill_parent”  

  4.     xmlns:android=“http://schemas.android.com/ apk/res/android”>  

  5.     <CheckBox android:id=“@+id/check1”  

  6.         android:layout_width=“wrap_content”  

  7.         android:layout_height=“wrap_content” android:text=“java”  

  8.         android:layout_x=“50px” android:layout_y=“22px”>  

  9.     </CheckBox>  

  10.   

  11.     <CheckBox android:id=“@+id/check2”  

  12.         android:layout_width=“wrap_content”  

  13.         android:layout_height=“wrap_content” android:text=“C#”  

  14.         android:layout_x=“50px” android:layout_y=“72px”>  

  15.     </CheckBox>  

  16.   

  17.     <Button android:id=“@+id/button1”  

  18.         android:layout_width=“wrap_content”  

  19.         android:layout_height=“wrap_content” android:text=“Confirm”  

  20.         android:layout_x=“60px” android:layout_y=“122px”>  

  21.     </Button>  

  22.   

  23.     <EditText android:id=“@+id/text1”  

  24.         android:layout_width=“wrap_content”  

  25.         android:layout_height=“wrap_content” android:text=“EditText”  

  26.         android:textSize=“18sp” android:layout_x=“60px”  

  27.         android:layout_y=“202px”>  

  28.     </EditText>  

  29. </AbsoluteLayout>  

 


To be copied to tools\demo\mypack\mydemos





  1. package mypack.mydemos;   

  2.   

  3. import android.app.Activity;   

  4. import android.os.Bundle;   

  5. import android.view.View;   

  6. import android.widget.*;   

  7.   

  8. public class demo extends Activity   

  9.   

  10. {   

  11.   

  12.     Button button1;   

  13.     CheckBox check1, check2;   

  14.     EditText text1;   

  15.   

  16.     @Override  

  17.     public void onCreate(Bundle icicle)   

  18.   

  19.     {   

  20.         super.onCreate(icicle);   

  21.         setContentView(R.layout.main);   

  22.         text1 = (EditText) this.findViewById(R.id.text1);   

  23.         check1 = (CheckBox) findViewById(R.id.check1);   

  24.         check2 = (CheckBox) findViewById(R.id.check2);   

  25.   

  26.         button1 = (Button) findViewById(R.id.button1);   

  27.         button1.setOnClickListener(new clicker());   

  28.   

  29.     }   

  30.   

  31.     class clicker implements Button.OnClickListener   

  32.   

  33.     {   

  34.         public void onClick(View v)   

  35.   

  36.         {   

  37.   

  38.             String r = “”;   

  39.             if (check1.isChecked())   

  40.             {   

  41.                 r = r + “java” + “\n”;   

  42.             }   

  43.             if (check2.isChecked())   

  44.             {   

  45.                 r = r + “c#”;   

  46.             }   

  47.             text1.setText(r);   

  48.         }   

  49.   

  50.     }   

  51.   

  52. }  

 


This is just the usual Java code and needs very little explanation. The only difference is the way , the controls are defined ( through res\layout\xml file).


 


Demo 5 – RadioButtons


The next standard control is the RadioButton, within a RadioGroup.


To be placed in tools\demo\res\layout





  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  

  2.     android:orientation=“vertical” android:layout_width=“fill_parent”  

  3.     android:layout_height=“wrap_content”>  

  4.     <RadioGroup android:id=“@+id/group1” android:layout_width=“fill_parent”  

  5.         android:layout_height=“wrap_content” android:orientation=“vertical”>  

  6.         <RadioButton android:id=“@+id/radio1” android:text=“madras”  

  7.             android:layout_width=“wrap_content” android:layout_height=“wrap_content” />  

  8.         <RadioButton android:id=“@+id/radio2” android:text=“bombay”  

  9.             android:layout_width=“wrap_content” android:layout_height=“wrap_content” />  

  10.     </RadioGroup>  

  11.     <Button android:id=“@+id/button1” android:layout_width=“wrap_content”  

  12.         android:layout_height=“wrap_content” android:text=“Button” />  

  13.     <TextView android:id=“@+id/label1” android:layout_width=“wrap_content”  

  14.         android:layout_height=“wrap_content” android:text=“where” />  

  15.     <EditText android:id=“@+id/text1” android:layout_width=“wrap_content”  

  16.         android:layout_height=“wrap_content” android:text=“” android:textSize=“18sp”  

  17.         android:layout_x=“70px” android:layout_y=“182px” />  

  18. </LinearLayout>  

The following Java code should be placed in tools\demo\src\mypack\mydemo





  1. package mypack.mydemos;   

  2.   

  3. import android.app.Activity;   

  4.   

  5. import android.os.Bundle;   

  6.   

  7. import android.view.View;   

  8.   

  9. import android.widget.*;   

  10.   

  11. public class demo extends Activity   

  12.   

  13. {   

  14.   

  15.     TextView label1;   

  16.   

  17.     RadioGroup group1;   

  18.   

  19.     RadioButton radio1, radio2;   

  20.   

  21.     Button button1;   

  22.   

  23.     EditText text1;   

  24.   

  25.     @Override  

  26.     protected void onCreate(Bundle icicle)   

  27.   

  28.     {   

  29.   

  30.         super.onCreate(icicle);   

  31.   

  32.         setContentView(R.layout.main);   

  33.   

  34.         group1 = (RadioGroup)   

  35.   

  36.         findViewById(R.id.group1);   

  37.   

  38.         radio1 = (RadioButton)   

  39.   

  40.         findViewById(R.id.radio1);   

  41.   

  42.         radio2 = (RadioButton)   

  43.   

  44.         findViewById(R.id.radio2);   

  45.   

  46.         button1 = (Button) findViewById(R.id.button1);   

  47.   

  48.         text1 = (EditText) findViewById(R.id.text1);   

  49.   

  50.         text1.setText(“radio”);   

  51.   

  52.         label1 = (TextView) findViewById(R.id.label1);   

  53.   

  54.         label1.setText(“where?”);   

  55.   

  56.         button1.setOnClickListener(new clicker());   

  57.   

  58.     }   

  59.   

  60.     // …inner class —follows ->   

  61.   

  62.     class clicker implements Button.OnClickListener   

  63.   

  64.     {   

  65.   

  66.         public void onClick(View v)   

  67.   

  68.         {   

  69.   

  70.             if (v == button1)   

  71.   

  72.             {   

  73.   

  74.                 if (radio1.isChecked())   

  75.   

  76.                 {   

  77.                     text1.setText(“madras”);   

  78.                 }   

  79.   

  80.                 if (radio2.isChecked())   

  81.   

  82.                 {   

  83.                     text1.setText(“bombay”);   

  84.                 }   

  85.   

  86.             }   

  87.   

  88.         }   

  89.   

  90.     }   

  91.   

  92.     // —-inner class ends here —   

  93.   

  94. }  

 


You would have observed that I am naming all my java files as ‘demo.java’. May be confusing at first but I am doing so with a purpose. First of all, the emulator screen gets cluttered with too many buttons if we go on adding my examples. What I have done is :


I have created a folder as d:\android\mydemos.


In mydemos folder, I have created subfolders such as (ex1,ex2 etc). But, within each folder, I have demo.java & main.xml.


This way, we can easily test each of our demos  by uniform procedure. In my system, I had the problem of insufficient memory. And, by the above step, I was able to test all my programs by the same name.


Here is an important tip however, if you choose to name the source files and xml files differently and want to reduce the clutter.


Demo 6 – Gallery 


Interestingly, there is a ready-made control in the toolbox, named ‘gallery’. Let us now learn to use this control, though the syntax is a bit difficult. This time, we will need demo.java, main.xml and also another folder in tools\demo\res\drawable. This special folder is to be created by us. You can read ‘img’ instead of ‘drawable’. So, we place all the image files to be displayed in the gallery, in this folder.


 


Let us as usual create the xml file by using DroidDraw as follows.


 





  1. <?xml version=“1.0” encoding=“utf-8”?>  

  2. <Gallery xmlns:android=“http://schemas.android.com/apk/res/android”  

  3.     android:id=“@+id/gallery” android:layout_width=“fill_parent”  

  4.     android:layout_height=“fill_parent” android:layout_alignParentBottom=“true”  

  5.     android:layout_alignParentLeft=“true” android:gravity=“center_vertical”  

  6.     android:spacing=“5” />  

 


The following class is to be placed in tools\demo\mypack\mydemos





  1. package mypack.mydemos;   

  2.   

  3. import android.app.Activity;   

  4. import android.content.Context;   

  5. import android.os.Bundle;   

  6. import android.view.View;   

  7. import android.view.ViewGroup;   

  8. import android.widget.*;   

  9. import android.widget.AdapterView.OnItemClickListener;   

  10.   

  11. public class example extends Activity   

  12.   

  13. {   

  14.   

  15.     @Override  

  16.     public void onCreate(Bundle icicle) {   

  17.   

  18.         super.onCreate(icicle);   

  19.         setContentView(R.layout.main);   

  20.         Gallery gallery = (Gallery)   

  21.         findViewById(R.id.gallery);   

  22.         gallery.setAdapter(new ImageAdapter(this));   

  23.         gallery.setOnItemClickListener(new OnItemClickListener()   

  24.         {   

  25.   

  26.             public void onItemClick(AdapterView parent,   

  27.             View v,   

  28.             int position,   

  29.             long id)   

  30.   

  31.             {   

  32.   

  33.                 Toast.makeText(example.this“” + position,   

  34.                 Toast.LENGTH_SHORT).show();   

  35.             }   

  36.   

  37.         });   

  38.   

  39.     }   

  40.   

  41.     public class ImageAdapter extends BaseAdapter   

  42.     {   

  43.   

  44.         public ImageAdapter(Context c)   

  45.         {   

  46.             mContext = c;   

  47.         }   

  48.   

  49.         public int getCount()   

  50.         {   

  51.             return mImageIds.length;   

  52.         }   

  53.   

  54.         public Object getItem(int position)   

  55.         {   

  56.             return position;   

  57.         }   

  58.   

  59.         public long getItemId(int position)   

  60.         {   

  61.             return position;   

  62.         }   

  63.   

  64.         public View getView(int position, View   

  65.         convertView, ViewGroup parent)   

  66.         {   

  67.             ImageView i = new ImageView(mContext);   

  68.             i.setImageResource(mImageIds[position]);   

  69.             i.setScaleType(ImageView.ScaleType.FIT_XY);   

  70.             i.setLayoutParams(new Gallery.LayoutParams(160200));   

  71.             return i;   

  72.         }   

  73.   

  74.         public float getAlpha(boolean focused, int offset)   

  75.         {   

  76.             return Math.max(01.0f – (0.2f * Math.abs(offset)));   

  77.         }   

  78.   

  79.         public float getScale(boolean focused, int offset)   

  80.         {   

  81.             return Math.max(01.0f – (0.2f *   

  82.             Math.abs(offset)));   

  83.         }   

  84.         private Context mContext;   

  85.         private Integer[] mImageIds = {   

  86.         R.drawable.cindy,   

  87.         R.drawable.clinton,   

  88.         R.drawable.ford,   

  89.         R.drawable.cybil,   

  90.         R.drawable.demi,   

  91.         R.drawable.colin,   

  92.         R.drawable.david,   

  93.         R.drawable.drew   

  94.         };   

  95.   

  96.     }   

  97.   

  98. }  

How to uninstall an application from the emulator?



  • Make sure your emulator is running
  • Open a dos box in the android/tools folder  d:\android\tools>adb shell 
  • You will get the shell prompt
     #cd /data/app
     #ls
     (It will list all the *.apk installed in your emulator)
    # rm  example.apk
    ( if you want to remove ‘example’)
    #exit
  • You will see the application getting removed from the emulator at the same moment

 


That completes the first part of my introductory tutorial on Android SDK.