Android编程基础 实验报告三 下载本文

内容发布更新时间 : 2024/11/15 3:37:49星期一 下面是文章的全部内容请认真阅读。

username=(EditText)this.findViewById(R.id.username); password=(EditText)this.findViewById(R.id.password); submit=(Button)this.findViewById(R.id.submit); reset=(Button)this.findViewById(R.id.reset); 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) { 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=\用户名:\密码:\ Uri data = Uri.parse(uriString); 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 onStart() { super.onStart(); Log.i(TAG, \ } @Override //在onStart()后被调用,用于恢复onSaveInstanceState()保存的用户界面

第 错误!未指定书签。 页 共 13 页

信息 public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, \ } @Override //在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源 public void onResume() { super.onResume(); Log.i(TAG, \ } @Override // 在onResume()后被调用,保存界面信息 public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); Log.i(TAG, \ } @Override //在重新进入可视生命周期前被调用,载入界面所需要的更改信息 public void onRestart() { super.onRestart(); Log.i(TAG, \ } @Override //在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。 public void onPause() { super.onPause(); Log.i(TAG, \ } @Override //在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源 public void onStop() { super.onStop(); Log.i(TAG, \ } @Override //在完全生命周期结束时被调用,释放资源,包括线程、数据连接等 public void onDestroy() { super.onDestroy(); Log.i(TAG, \ } }

2. 详细工程结构及部分代码(显式和隐式都在其中): (显式启动)

第 错误!未指定书签。 页 共 13 页

结果:

部分代码:

MainActivity.java

public class MainActivity extends Activity {

/** Called when the activity is first created. */ public static int maxNum; public static Handler handler=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 static Runnable RefreshLable=new Runnable(){ public void run() { result.setText(String.valueOf(maxNum)); }};

public void onCreate(Bundle savedInstanceState) {

第 错误!未指定书签。 页 共 13 页

final Bundle mybundle=new Bundle(); super.onCreate(savedInstanceState); setContentView(R.layout.main);

final Intent intent=new Intent(MainActivity.this,CompareService.class); result=(EditText)findViewById(R.id.result); compare=(Button)findViewById(R.id.compare); reset=(Button)findViewById(R.id.reset); one=(EditText)findViewById(R.id.one); two=(EditText)findViewById(R.id.two);

compare.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mybundle.putString(\ mybundle.putString(\ intent.putExtras(mybundle); startService(intent); } });

reset.setOnClickListener(new OnClickListener(){ public void onClick(View v) { one.setText(null); two.setText(null); result.setText(null); } }); } }

CompareService.java

public 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,\ } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); bundle=intent.getExtras(); String c1=bundle.getString(\ String c2=bundle.getString(\ if(!c1.toString().equals(\ one=Integer.parseInt(c1);

第 错误!未指定书签。 页 共 13 页

two=Integer.parseInt(c2); } if (!workThread.isAlive()){ workThread.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; } }

(隐式启动) 结果:

部分代码:

MainActivity.java

public class MainActivity extends Activity {

/** Called when the activity is first created. */ private boolean isBound = false;

第 错误!未指定书签。 页 共 13 页