Main.java代码package com.webabcd.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Main extends Activity {TextView txt;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.main);txt = (TextView) this.findViewById(R.id.txt);txt.setText("Activity 1");Button btn = (Button) this.findViewById(R.id.btn);btn.setText("启动另一个Activity");btn.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// 实例化 Intent,指定需要启动的 ActivityIntent intent = new Intent();intent.setClass(Main.this, MyActivity.class);// 实例化 Bundle,设置需要传递的参数Bundle bundle = new Bundle();bundle.putString("name", "webabcd");bundle.putDouble("salary", 100.13);// 将需要传递的参数赋值给 Intent 对象intent.putExtras(bundle);// startActivity(intent); // 启动指定的 Intent(不等待返回结果)// Main.this.finish();// 启动指定的 Intent,并等待返回结果// 其中第二个参数如果大于等于零,则返回结果时会回调 onActivityResult() 方法startActivityForResult(intent, 0);}});Log.d("MyDebug", "onCreate");}// 被启动的 Activity 返回结果时的回调函数@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == Activity.RESULT_OK){Bundle bundle = data.getExtras();String name = bundle.getString("name");double salary = bundle.getDouble("salary");txt.setText("Activity 1" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));}}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();Log.d("MyDebug", "onStart");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.d("MyDebug", "onStop");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();Log.d("MyDebug", "onRestart");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();Log.d("MyDebug", "onPause");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();Log.d("MyDebug", "onResume");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.d("MyDebug", "onDestroy");}}MyActivity.java代码package com.webabcd.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;// 被另一个 Activity 所启动的 Activitypublic class MyActivity extends Activity {Intent intent;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.main2);// 获取启动者传递过来的参数intent = this.getIntent();Bundle bundle = intent.getExtras();String name = bundle.getString("name");double salary = bundle.getDouble("salary");TextView txt = (TextView) this.findViewById(R.id.txt);txt.setText("Activity 2" + "\n名字:" + name + "\n薪水:" + String.valueOf(salary));Button btn = (Button) this.findViewById(R.id.btn);btn.setText("返回前一个Activity");btn.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {// 返回参数给启动者MyActivity.this.setResult(Activity.RESULT_OK, intent);MyActivity.this.finish();}});}}AndroidManifest.xml代码<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.webabcd.activity" android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".Main" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--如果有需要用到的 Activity ,则都要在这里做相应的配置--><activity android:name=".MyActivity" android:label="Activity 2" /></application><uses-sdk android:minSdkVersion="3" /></manifest>2、Service, Broadcast, BroadcastReceiver 的演示Main.java代码package com.webabcd.service;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;/** startService() 和 bindService() 的区别* startService() - 正常理解就好* bindService() - 使当前上下文对象(本例中就是 Activity)通过一个 ServiceConnection 对象邦定到指定的 Service 。这样,如果上下文对象销毁了的话,那么其对应的 Service 也会被销毁*/public class Main extends Activity implements OnClickListener {private TextView txtMsg;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);setTitle("android 之 service");this.findViewById(R.id.btnStart).setOnClickListener(this);this.findViewById(R.id.btnStop).setOnClickListener(this);this.findViewById(R.id.btnBind).setOnClickListener(this);this.findViewById(R.id.btnUnbind).setOnClickListener(this);txtMsg = (TextView)this.findViewById(R.id.txtMsg);// 实例化自定义的 BroadcastReceiverreceiver = new UpdateReceiver();IntentFilter filter = new IntentFilter();// 为 BroadcastReceiver 指定 action ,使之用于接收同 action 的广播filter.addAction("com.webabcd.service.msg");// 以编程方式注册 BroadcastReceiver 。配置方式注册 BroadcastReceiver 的例子见 AndroidManifest.xml 文件// 一般在 OnStart 时注册,在 OnStop 时取消注册this.registerReceiver(receiver, filter);// this.unregisterReceiver(receiver);}@Overridepublic void onClick(View v) {Intent intent = new Intent(Main.this, MyService.class);switch (v.getId()) {case R.id.btnStart:this.startService(intent);break;case R.id.btnStop:this.stopService(intent);break;case R.id.btnBind:this.bindService(intent, conn, Context.BIND_AUTO_CREATE);break;case R.id.btnUnbind:this.unbindService(conn);break;}}// bindService() 所需的 ServiceConnection 对象private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName className, IBinder service) {}@Overridepublic void onServiceDisconnected(ComponentName className) {}};private String msg="";private UpdateReceiver receiver;// 实现一个 BroadcastReceiver,用于接收指定的 Broadcastpublic class UpdateReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {msg = intent.getStringExtra("msg");txtMsg.append(msg + "\n");}}}MyService.java代码package com.webabcd.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;// 演示 Service 的生命周期。具体信息运行程序后在 LogCat 中查看public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {Log.d("MyDebug", "onBind");sendMsg("onBind");// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.d("MyDebug", "onCreate");sendMsg("onCreate");}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.d("MyDebug", "onDestroy");sendMsg("onDestroy");}@Overridepublic void onRebind(Intent intent) {// TODO Auto-generated method stubsuper.onRebind(intent);Log.d("MyDebug", "onRebind");sendMsg("onRebind");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Log.d("MyDebug", "onStart");sendMsg("onStart");}@Overridepublic boolean onUnbind(Intent intent) {Log.d("MyDebug", "onUnbind");sendMsg("onUnbind");// TODO Auto-generated method stubreturn super.onUnbind(intent);}// 发送广播信息private void sendMsg(String msg){// 指定广播目标的 action (注:指定了此 action 的 receiver 会接收此广播)Intent intent = new Intent("com.webabcd.service.msg");// 需要传递的参数intent.putExtra("msg", msg);// 发送广播this.sendBroadcast(intent);}}MyBootReceiver.java代码package com.webabcd.service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyBootReceiver extends BroadcastReceiver {// 用于接收满足条件的 Broadcast(相应的 Broadcast 的注册信息详见 AndroidManifest.xml ,当系统启动完毕后会调用这个广播接收器)@Overridepublic void onReceive(Context arg0, Intent arg1) {Log.d("MyDebug", "onReceive");// 启动服务Intent service = new Intent(arg0, MyService.class);arg0.startService(service);}}AndroidManifest.xml代码<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.webabcd.service" android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".Main" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--如果有需要用到的 service ,则都要在这里做相应的配置--><service android:name=".MyService"></service><!--注册一个 BroadcastReceiver其 intent-filter 为 android.intent.action.BOOT_COMPLETED(用于接收系统启动完毕的 Broadcast)--><receiver android:name=".MyBootReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver></application><!--接受系统启动完毕的 Broadcast 的权限--><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-sdk android:minSdkVersion="3" /></manifest>OK
联系客服