1、安卓实验报告 安卓实验报告班级:xxxxx学号:姓名:xxxx实验一:Activity生命周期的测试一:实验目的掌握使用Eclipse开发Android应用开发程序的方法;掌握Android虚拟设备的创建方法;了解Android的程序结构;了解使用命令行创建Android程序方法;了解Activity生命周期,理解Activity事件回调,onRestoreInstanceState()和 onSaveInstanceState两个函数的使用。二:实验设备 Eclipse 开发Android 应用 Android虚拟设备( AVD)三:实验内容 (1)正常启动ActivityLife,然后单击
2、用户界面的“结束程序”按钮关闭程序。(2)正常启动ACtivityLife,然后通过“拨号键”启动内置的拨号程序,再通过“回 退建” 退出拨号程序,使ActivityLife重新显示在屏幕中。四:实验核心代码package class MainActivity extends Activity private static String TAG=LIFECYCLE; Override protected void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView (TAG,(1) onCreate(
3、); Button button =(Button) findViewById (new OnClickListener() public void onClick(View view) oString().trim(); String str1 = ().toString().trim(); (姓名:+str); (爱好:+str1); if (arg1 = (性别:+男); else if (arg1 = (性别:+女); ); ); 五:运行结果截图:实验三:不同界面的切换一:实验目的掌握各种界面布局的特点和使用方法;掌握选项菜单,子菜单和快捷菜单的使用方法。二;实验设备 Eclipse
4、 开发Android 应用 Android虚拟设备( AVD)三;实验内容:设计一个Tab标签页,实现LinearLayout,RelativeLayout,AbsoluteLayout三种分页面之间的切换,每个页面的内容包括用户名和确定,取消两个按钮四:实验核心代码: : public class MainActivity extends TabActivity protected void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView TabHost tabHost=getTabHost()
5、; (this).inflate (),true); (this).inflate (),true); (this).inflate (),true); (TAB1). setIndicator(线性布局).setContent (TAB1). setIndicator(绝对布局).setContent (TAB1). setIndicator(相对布局).setContent Tab1,javapublic class Tab1 extends Activity public void onCreate(Bundle savedInstanceState) (savedInstanceSta
6、te); setContentView public class Tab2 extends Activity public void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView public class Tab3 extends Activity public void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView 五:运行结果截图:实验四:登陆界面和广播消息一:实验目的 了解使用Intent进
7、行组件通信的原理; 了解Intent过滤器的原理与匹配机制; 掌握发送和接受广播消息的方法。 二:实验设备 Eclipse 开发Android 应用 Android虚拟设备( AVD)三:实验内容先写一个登陆界面,包括用户名,密码,点击登陆时实现页面的跳转功能,在第二个页面使用Intent发送消息,并调用sendBroadcast()函数把Intent携带的消息传送出去,用BroadcastReceiver接受广播消息,在页面下方显示广播内容。四;实验核心代码:public class MainActivity extends Activity private Button button; O
8、verride protected void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView button=(Button) findViewById (new () public void onClick(View arg0) Intent intent=new Intent,; startActivity(intent); ); public class Second extends Activity private Button button2; private EditText editT
9、ext; protected void onCreate(Bundle savedInstanceState) (savedInstanceState); setContentView Intent intent=getIntent(); editText=(EditText) findViewById button2=(Button) findViewById (new () public void onClick(View arg0) Intent intent=new Intent(); (message, ().toString(); sendBroadcast(intent); );
10、 public class MyBroadcastReceiver extends BroadcastReceiver public void onReceive(Context context, Intent intent) String msg=(message); (context, msg, .show(); 五:运行结果截图实验五:Service绑定实现数据运算一:实验目的 了解Service的原理和用途; 掌握本地服务的管理和方法; 掌握服务的隐式启动和显示启动方法; 掌握远程服务的绑定和调用方法。二:实验设备 Eclipse 开发Android 应用 Android虚拟设备( A
11、VD)三:实验内容以绑定方式使用Service,获取Service实例,当点击绑定服务时可以实现两个数的求差和比较大小功能,并输出结果,当点击取消绑定服务时,则无法运算。四:实验核心代码public class MathService extends Service private final IBinder mBinder=new LocalBinder(); public class LocalBinder extends Binder MathService getService() return ; public IBinder onBind(Intent intent) (this,
12、 本地绑定:MathService,.show(); return mBinder; public boolean onUnbind(Intent intent) (this, 取消本地绑定:MathService, .show(); return false; public long sub(long a,long b) return a-b; public int compare(long a,long b) if(ab) return 1; else if(a=b) return 0; else return -1; public class MainActivity extends A
13、ctivity private MathService mathService; private Button button1; private Button button2; private Button button3; private Button button4; private EditText editText1; private EditText editText2; private EditText editText3; private boolean isBound=false; Override protected void onCreate(Bundle savedIns
14、tanceState) (savedInstanceState); setContentView editText1=(EditText) findViewById editText2=(EditText) findViewById editText3=(EditText) findViewById button1=(Button) findViewById button2=(Button) findViewById button3=(Button) findViewById button4=(Button) findViewById (new () public void onClick(V
15、iew v) if(!isBound) final Intent serviceIntent=new Intent ( ,; bindService(serviceIntent, mConnection, ; isBound=true; ); (new () public void onClick(View v) if(isBound) isBound=false; unbindService(mConnection); mathService=null; ); (new () public void onClick(View v) if(mathService=null) (未绑定服务);
16、return ; String a = ().toString(); long a1 = (a); String b = ().toString(); long b1 = (b); long result=(a1, b1); String msg=(a1)+-+(b)+ =+(result); (msg); ); (new () public void onClick(View v) if(mathService=null) (未绑定服务); return ; String a = ().toString(); long a1 = (a); String b = ().toString();
17、long b1 = (b); long result=(a1, b1); if(result=1) (a+b); else if(result=0) (a+=+b); else (a+b); ); private ServiceConnection mConnection=new ServiceConnection() public void onServiceDisconnected(ComponentName arg0) mathService=null; public void onServiceConnected(ComponentName name, IBinder service)
18、 mathService=(service).getService(); ; 实验六:SQLite实现用户的增删改查一:实验目的 了解SQLite数据库的特点和体系及结构; 掌握SQLite数据库的建立和操作方法; 理解ContentProvide的用途和原理; 掌握ContentProvider的创建和使用方法。二:实验设备 Eclipse 开发Android 应用 Android虚拟设备( AVD)三:实验内容使用SQlite数据库实现用户(包括:姓名,年龄,身高)的添加数据,全部显示,清除显示,全部删除,ID删除,ID查询,ID更新的功能。四:实验核心代码public class Mai
19、nActivity extends ActionBarActivity private EditText et_name; private EditText et_sex; private EditText et_department; private EditText et_id; private EditText et_salary; private MyOpenHelper oh; private SQLiteDatabase db; private TextView tv; private LinearLayout ll; Override protected void onCreat
20、e(Bundle savedInstanceState) (savedInstanceState); setContentView oh = new MyOpenHelper, , null, 1); db = (); et_name = (EditText) findViewById et_sex = (EditText) findViewById et_department = (EditText) findViewById et_salary = (EditText) findViewById et_id = (EditText) findViewById ll = (LinearLay
21、out) findViewById public void add(View v) db = (); String name=().toString(); String sex=().toString(); String department=().toString(); String salary=().toString(); ContentValues values=new ContentValues(); (name,name ); (sex, sex); (department,department); (salary, salary); (staff, null, values);
22、tv=new TextView; (添加成功); (); public void show(View v) db = (); Cursor cursor=(staff, null, null, null, null, null, null, null); while() String id=(_id); String name=(name); String sex=(sex); String department=(department); String salary=(salary); tv=new TextView; (ID:+id+ ;+Name:+name+ ;+Sex:+sex+ ;
23、+Department:+department+ ;+Salary+salary); (tv); (); public void clear(View v) (); public void deleteAll(View v) db = (); (staff, null, null); tv=new TextView; (删除成功); (); public void deleteById(View v) db = (); String id=().toString(); (staff, _id=, new Stringid); (); tv=new TextView; (ID删除成功); pub
24、lic void findById(View v) db = (); String id=().toString(); Cursor cursor=(staff, null, _id=, new Stringid, null, null, null, null); while() id=(_id); String name=(name); String sex=(sex); String department=(department); String salary=(salary); tv=new TextView; (id+;+name+;+sex+;+department+;+salary
25、); (tv); tv=new TextView; (ID查询成功); (); public void updateById(View v) db = (); String id=().toString(); String name=().toString(); String sex=().toString(); String department=().toString(); String salary=().toString(); ContentValues values=new ContentValues(); (name,name); (sex, sex); (department,d
26、epartment); (salary, salary); (staff, values, _id=, new Stringid); tv=new TextView; (更新ID成功); (); public class MyOpenHelper extends SQLiteOpenHelper public MyOpenHelper(Context context, String name, CursorFactory factory, int version) super(context, name, factory, version); Override public void onCr
27、eate(SQLiteDatabase db) (create table staff(_id integer primary key autoincrement,name text,sex text,department text,salary float); Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 五:运行结果截图实验七:天气预报一:实验目的 了解位置服务的概念; 了解地图密钥的申请方法; 掌握获取地理信息的方法;二:实验设备 Eclipse 开发Android 应用 Android虚拟设备( AVD)三:实验内容 写一个项目,实现显
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1