1、Android编程基础 实验报告三南昌航空大学实验报告 二0一三年一月六日课程名称: 移动终端编程技术 实验名称: Android组件通信和后台服务 班级: 姓名: 同组人: 指导教师评定: 签名: 一、实验目的了解使用Intent进行组件通信的原理,掌握使用Intent启动Activity的方法;掌握获取Activity返回值的方法;了解Intent过滤器的原理与匹配机制;了解Service的原理和用途;掌握进程内服务的管理方法;掌握服务的隐式启动和显式启动方法;二、实验工具Eclipse(MyEclipse)+ ADT + Android2.2 SDK;三、实验题目1.编程实现功能描述:主
2、界面上有一个“登录”按钮,点击“登录”按钮后打开一个新的Activity;新的Activity上面有输入用户名和密码的控件,在用户关闭这个Activity后,将用户输入的用户名和密码传递到主界面中。编程要点:主界面的Activity命名为MainActivity;启动新的Activity命名为UserLoginActivity;分别使用显示启动和隐式启动的方式,启动新的Activity;用户名中不能出现“”符号,同时长度不超过12个字符;密码使用密码文本显示方式,即显示为“*”,同时只能为数字;返回的用户名和密码要以Toast的方式显示出来;MainActivity和UserLoginActi
3、vity中各个生命周期的回调函数中要以Log.i方式显示日志信息。2. 编程实现功能描述:编程建立一个简单的进程内服务,实现比较两个整数大小的功能。服务提供IntCompare(Int, Int)函数,输入两个整数,输出较大的整数。 编程要点:主界面的Activity命名为MainActivity;提供两个EditText,分别输入两个整数;提供一个Botton,启动比较过程;提供一个TextView,显示较大的整数;分别使用启动方式和绑定方式使用Service;分别使用Handle和AsyncTask更新TextView中的内容。四、实验步骤1.详细工程结构及部分代码(显式和隐式都在其中):
4、结果:代码:MainActivity.javapublic class MainActivity extends Activity protected int SUBACTIVITY1 = 1; private static String TAG = MainActivity; Button button1,button2; public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); Log.i(TAG, (1) onCreate(); button1.setOnClickListene
5、r(new View.OnClickListener() public void onClick(View v) Intent intent = new Intent(MainActivity.this,UserLoginActivity.class); Toast.makeText(getApplicationContext(), 显示启动, Toast.LENGTH_SHORT).show(); startActivityForResult(intent, SUBACTIVITY1); ); button2.setOnClickListener(new View.OnClickListen
6、er() public void onClick(View v) Toast.makeText(getApplicationContext(), 隐式启动, Toast.LENGTH_SHORT).show(); startActivityForResult(intent, SUBACTIVITY1); ); protected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); if (resultC
7、ode = RESULT_OK) Uri uriData = data.getData(); Toast.makeText(getApplicationContext(), uriData.toString(), Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), 用户名和密码为空, Toast.LENGTH_SHORT) .show(); Override /可视生命周期开始时被调用,对用户界面进行必要的更改 public void onStart() super.onStart(); Log.i(
8、TAG, (2) onStart(); Override /在onStart()后被调用,用于恢复onSaveInstanceState()保存的用户界面信息 public void onRestoreInstanceState(Bundle savedInstanceState) super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, (3) onRestoreInstanceState(); Override /在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源 public void onResu
9、me() super.onResume(); Log.i(TAG, (4) onResume(); Override / 在onResume()后被调用,保存界面信息 public void onSaveInstanceState(Bundle savedInstanceState) super.onSaveInstanceState(savedInstanceState); Log.i(TAG, (5) onSaveInstanceState(); Override /在重新进入可视生命周期前被调用,载入界面所需要的更改信息 public void onRestart() super.onR
10、estart(); Log.i(TAG, (6) onRestart(); Override /在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。 public void onPause() super.onPause(); Log.i(TAG, (7) onPause(); Override /在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源 public void onStop() super.onStop(); Log.i(TAG, (8) onStop(); Override /在完全生命周期结束时被调用,释放资源,包括线程、数据连接等 public v
11、oid onDestroy() super.onDestroy(); Log.i(TAG, (9) onDestroy(); UserLoginActivity,javapublic class UserLoginActivity extends Activity EditText username,password; Button submit,reset; private static String TAG = MainActivity; protected void onCreate(Bundle savedInstanceState) / TODO Auto-generated met
12、hod stub super.onCreate(savedInstanceState); Log.i(TAG, (1) onCreate(); username.setFocusable(true); username.setOnKeyListener(new OnKeyListener() public boolean onKey(View v, int keyCode, KeyEvent event) / TODO Auto-generated method stub int unicodeChar = event.getUnicodeChar(); if(unicodeChar= 64)
13、 return true; else return false; ); submit.setOnClickListener(new View.OnClickListener() public void onClick(View v) / TODO Auto-generated method stub String name =username.getText().toString(); String pass=password.getText().toString(); String uriString=用户名:+name+密码:+pass; Uri data = Uri.parse(uriS
14、tring); Intent result = new Intent(null, data); setResult(RESULT_OK, result); finish(); ); reset.setOnClickListener(new View.OnClickListener() public void onClick(View v) / TODO Auto-generated method stub setResult(RESULT_CANCELED, null); finish(); ); Override /可视生命周期开始时被调用,对用户界面进行必要的更改 public void
15、onStart() super.onStart(); Log.i(TAG, (2) onStart(); Override /在onStart()后被调用,用于恢复onSaveInstanceState()保存的用户界面信息 public void onRestoreInstanceState(Bundle savedInstanceState) super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, (3) onRestoreInstanceState(); Override /在活动生命周期开始时被调用,恢复被onPause
16、()停止的用于界面更新的资源 public void onResume() super.onResume(); Log.i(TAG, (4) onResume(); Override / 在onResume()后被调用,保存界面信息 public void onSaveInstanceState(Bundle savedInstanceState) super.onSaveInstanceState(savedInstanceState); Log.i(TAG, (5) onSaveInstanceState(); Override /在重新进入可视生命周期前被调用,载入界面所需要的更改信息
17、public void onRestart() super.onRestart(); Log.i(TAG, (6) onRestart(); Override /在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。 public void onPause() super.onPause(); Log.i(TAG, (7) onPause(); Override /在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源 public void onStop() super.onStop(); Log.i(TAG, (8) onStop(); Override /在完全生命
18、周期结束时被调用,释放资源,包括线程、数据连接等 public void onDestroy() super.onDestroy(); Log.i(TAG, (9) onDestroy(); 2.详细工程结构及部分代码(显式和隐式都在其中):(显式启动)结果:部分代码:MainActivity.javapublic class MainActivity extends Activity /* Called when the activity is first created. */ public static int maxNum; public static Handler handler=
19、new Handler(); private static TextView result=null; private static Button compare=null; private static Button reset=null; private static EditText one=null; private static EditText two=null; public static void UpdateGUI(int refreshDouble) maxNum=refreshDouble; handler.post(RefreshLable); private stat
20、ic Runnable RefreshLable=new Runnable() public void run() result.setText(String.valueOf(maxNum); ; public void onCreate(Bundle savedInstanceState) final Bundle mybundle=new Bundle(); super.onCreate(savedInstanceState); final Intent intent=new Intent(MainActivity.this,CompareService.class); compare.s
21、etOnClickListener(new OnClickListener() public void onClick(View v) mybundle.putString(one, one.getText().toString(); mybundle.putString(two, two.getText().toString(); intent.putExtras(mybundle); startService(intent); ); reset.setOnClickListener(new OnClickListener() public void onClick(View v) one.
22、setText(null); two.setText(null); result.setText(null); ); CompareService.javapublic class CompareService extends Service private Thread workThread; Bundle bundle=null; int one=0,two=0; public void onCreate() super.onCreate(); workThread = new Thread(null,backgroudWork,WorkThread); Override public v
23、oid onStart(Intent intent, int startId) super.onStart(intent, startId); bundle=intent.getExtras(); String c1=bundle.getString(one); String c2=bundle.getString(two); if(!c1.toString().equals()&!c2.toString().equals() one=Integer.parseInt(c1); two=Integer.parseInt(c2); if (!workThread.isAlive() workTh
24、read.start(); public IBinder onBind(Intent intent) return null; private Runnable backgroudWork = new Runnable() Override public void run() int randomDouble =IntCompare(one,two); MainActivity.UpdateGUI(randomDouble); stopSelf(); ; int IntCompare(int a,int b) if(a=b) return a; else return b; (隐式启动) 结果
25、:部分代码:MainActivity.javapublic class MainActivity extends Activity /* Called when the activity is first created. */ private boolean isBound = false; private CompareService compareService; int a=0,b=0; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); if(!isB
26、ound) Intent serviceIntent=new Intent(MainActivity.this,CompareService.class); bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE); isBound=true; compare.setOnClickListener(new OnClickListener() public void onClick(View v) String c1=one.getText().toString(); String c2=two.getText().toStr
27、ing(); if(!c1.equals()&!c2.equals() a=Integer.parseInt(c1); b=Integer.parseInt(c2); result.setText(String.valueOf(compareService.IntCompare(a, b); ); reset.setOnClickListener(new OnClickListener() public void onClick(View v) one.setText(null); two.setText(null); result.setText(null); ); private Serv
28、iceConnection mConnection=new ServiceConnection() Override public void onServiceConnected(ComponentName name, IBinder service) / TODO Auto-generated method stub compareService=(CompareService.LocalBinder)service).getService(); Override public void onServiceDisconnected(ComponentName name) / TODO Auto-generated method stub compareService=null; ;CompareService.java public class CompareService extends Service private final IBinder mBinder=new LocalBinder(); class LocalBinder extends Binder CompareService getService() return CompareService.this; Override public IBinder onBind(Intent
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1