收藏 分销(赏)

安卓小应用-时间定时器.doc

上传人:仙人****88 文档编号:9200003 上传时间:2025-03-16 格式:DOC 页数:27 大小:1.58MB
下载 相关 举报
安卓小应用-时间定时器.doc_第1页
第1页 / 共27页
安卓小应用-时间定时器.doc_第2页
第2页 / 共27页
点击查看更多>>
资源描述
第一章 安卓开发软件的安装 在做安卓开发之前,首先要安装安卓开发软件。 (1) 由于安卓app的开发都是基于Java语言,所以要安装JDk,这里安装的是由Oracle公司(甲骨文公司)提供的JDK-u25版本,安装过程如下: 1.我的电脑-->属性-->高级系统设置-->环境变量. 2.配置系统变量: a.新建 JAVA_HOME 然后安装: 按照提示一步步往下走就行。 (2) 安装完JDK以后,就要安装eclipse: 安装好以后的界面: (3) 安装SDK,这部这重要最耗时间; 去安卓开发者网站 下载sdk安装包: 然后配置变量环境: 然后双击打开sdk manager 勾选全部内容,进行安装更新,这步很关键,不更新会导致最后编译不兼容,安装时间大概在6小时。 (4) 安装ADT: 还是去安卓开发者网站下载ADT安装包 安装步骤: 至此,安卓开发软件就全部安装好了! 第二章 建立“hello world!” (1) 打开eclipse,选择File>New>Android application project,新建一个Android工程: (2)选择window>android virtual device manager,进行Android虚拟界面的设置: (3)右击first1工程,选择run as>android application,进行虚拟界面 此时,看到软件first1,点开 这样,一个最简单的app就完成了。打开res>layout>main.xml,看到文件 <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 这些都是由工程自己创建完成的,不需要做任何改动。 第三章 计时器“TimeTracker”设计 首先了解Android应用程序的基本架构 src:该文件夹包含了应用程序的Java源代码。它遵循标准的Java包约定。 res:此文件夹包含应用程序的所有资源,同时也是使用xml声明布局的地方,即包含所有的布局文件、图片、主题和字符串。 gen:该文件夹下的R.Java文件是自动生成的,不用管该文件夹下的任何东西。 AndroidMainfest.xml:包含了Android系统需要的应用程序的基本信息,包括应用程序使用的活动、服务、权限等。 Default.properties:列出了Android API的构建目标。 设计过程 (1) 建立一个Android 工程“timetracker” (2) 然后打开res\values\strings.xml,编辑代码设计软件中可见字符 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="Hello">Hello World, TimeTrackerActivity!</string> <string name="app_name">TimeTracker</string> <string name="elapsed_time_format">%1$d:%2$02d:%3$02d:%4$03d</string> <string name="finish">Finish</string> <string name="start">Start</string> <string name="stop">Stop</string> <string name="task_name">Task %1$d</string> </resources> (3) 进行页面布局,使用线性布局来摆放三个子视图:一个文本视图表示时间,一个两个按钮的线性布局,以及展示之前所有时刻列表视图,打开res\layout\main.xml,输入如下代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/counter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="50dp" > </TextView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/start_stop" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/start" > </Button> <Button android:id="@+id/reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/finish" > </Button> </LinearLayout> <ListView android:id="@+id/time_list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <!-- Preview: listitem=@layout/time_row --> </ListView> </LinearLayout> (4) timetracker应用程序将需要为他的列表时刻视图自定义布局,显示列表。在res\layout\time_row.xml输入如下代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:id="@+id/time_row" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingBottom="20dp" android:paddingTop="20dp"> <TextView android:id="@+id/lap_name" android:layout_height="wrap_content" android:text="Lap 1" android:layout_weight="1" android:layout_width="0dp" /> <TextView android:id="@+id/lap_time" android:layout_height="wrap_content" android:text="00:00:00" android:layout_weight="1" android:layout_width="0dp" android:gravity="right" /> </LinearLayout> (5)把数据捆绑到列表,打开src\timelistadapter.Java,往listview填充数据: package com.example.timetracter; import android.content.Context; import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class TimeListAdapter extends ArrayAdapter<Long> { public TimeListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(getContext()).inflate(R.layout.time_row, null); } long time = getItem(position); TextView name = (TextView) view.findViewById(R.id.lap_name); String taskString = getContext().getResources().getString(R.string.task_name); name.setText(String.format(taskString, position+1)); TextView lapTime = (TextView) view.findViewById(R.id.lap_time); lapTime.setText(DateUtils.formatElapsedTime(time)); return view; } } (6)声明活动,所有的活动都必须在应用程序的清单中声明,否则在应用第一次运行时会出现异常现象。Timetracker应用程序将会重写oncreate,程序如下: package com.example.timetracter; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.StrictMode; import android.text.format.DateUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class TimeTrackerActivity extends Activity implements OnClickListener { private TimeListAdapter mTimeListAdapter = null; private long mStart = 0; private long mTime = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Initialize the Timer TextView counter = (TextView) findViewById(R.id.counter); counter.setText(DateUtils.formatElapsedTime(0)); Button startButton = (Button) findViewById(R.id.start_stop); startButton.setOnClickListener(this); Button stopButton = (Button) findViewById(R.id.reset); stopButton.setOnClickListener(this); if (mTimeListAdapter == null) mTimeListAdapter = new TimeListAdapter(this, 0); ListView list = (ListView) findViewById(R.id.time_list); list.setAdapter(mTimeListAdapter); if (Util.useStrictMode(this)) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDialog() .build()); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDialog() .build()); } } @Override protected void onDestroy() { mHandler.removeMessages(0); super.onDestroy(); } @Override public void onClick(View v) { TextView ssButton = (TextView) findViewById(R.id.start_stop); if (v.getId() == R.id.start_stop) { if (!isTimerRunning()) { startTimer(); ssButton.setText(R.string.stop); } else { stopTimer(); ssButton.setText(R.string.start); } } else if (v.getId() == R.id.reset) { resetTimer(); TextView counter = (TextView) findViewById(R.id.counter); counter.setText(DateUtils.formatElapsedTime(0)); ssButton.setText(R.string.start); } } private void startTimer() { mStart = System.currentTimeMillis(); mHandler.removeMessages(0); mHandler.sendEmptyMessage(0); } private void stopTimer() { mHandler.removeMessages(0); } private boolean isTimerRunning() { return mHandler.hasMessages(0); } private void resetTimer() { stopTimer(); if (mTimeListAdapter != null) mTimeListAdapter.add(mTime/1000); mTime = 0; } private Handler mHandler = new Handler() { public void handleMessage(Message msg) { long current = System.currentTimeMillis(); mTime += current - mStart; mStart = current; TextView counter = (TextView) TimeTrackerActivity.this.findViewById(R.id.counter); counter.setText(DateUtils.formatElapsedTime(mTime/1000)); mHandler.sendEmptyMessageDelayed(0, 250); }; }; } (7)为了防止出现应用程序可能会遇到违反政策的对话框或者甚至会经历应用崩溃,这时可以只在调试模式时启用strictmode。要检测一个应用程序是否运行在调试模式,检测ApplicationInfo标志就行。在src\Util.Java下添加代码,代码如下: package com.example.timetracter; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; public class Util { public static boolean isDebugMode(Context context) { PackageManager pm = context.getPackageManager(); try { ApplicationInfo info = pm.getApplicationInfo(context.getPackageName(), 0); return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } catch (NameNotFoundException e) { } return true; } public static boolean useStrictMode(Context context) { return isDebugMode(context) && Build.VERSION.SDK_INT >= 9; } } (8)运行timetracker工程: 在所有应用程序中找到timetracker应用,点开运行: 运行正常,期间没有出现软件崩溃的情况。 总结 通过大概一个星期的学习与代码编写,终于将timetracker应用程序编出来了,也将apk下载到自己的手机上进行了实际的应用,这么多天的努力总算没有白费。 在做Android开发的过程中也遇到了很多的问题,主要有: (1) 首先是软件的安装,由于Android是基于Java语言环境的,首先是要装JDK,该开始以为安装这个软件只要一步步走就好了,没有设置环境变量,导致java语言不能用;sdk的安装也是很费劲,因为Google服务器在国外的原因,安装更新很麻烦,大概安装了6个小时才好。 (2) 在编写好res\layout\main.xml时,一直有警告出现,但是检查了几遍以后还是没有发现错误。后来根据错误原因找到了原来在menu文件夹下也有一个main.xml,由于这个应用程序没有用到menu,所以将该文件删除后就解决了。 (3) 在编写好timeadapter和timetrackeractivity以后,一直有错误,原因是缺少自动生成文件R.java文件,根据网上的解决方案,包括clean程序,刷新程序,删除自动生成文件gen,更新ADT等问题都试了一遍,还是没有解决问题。最后在网上问了,得到的解决。原来是eclipse版本原因,我这个版本Android程序图片文件drawable默认是ic_launcher.png,而现在一般默认是icon.png。 这些就是在做timetracker过程中遇到的主要问题。 总而言之,在这一个多礼拜的学习和实践中,我学到了很多关于Android开发的知识和经验,同时也学习了Java语言。但是我不会拘泥于这一个简单的软件,以后我还要更加努力学习前沿的知识,将其运用于实践。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服