Android 简单Activity和Service的交互代码

Activity如何互相交互, 更新Acivity控件?
请看例子:

Broadcast1Activity.java
package com.cn;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Broadcast1Activity extends Activity {
    /** Called when the activity is first created. */
	private static String SMS_ACTION = "com.webabcd.service.msg";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SMSReceiver r = new SMSReceiver();
				IntentFilter intent = new IntentFilter();
				intent.addAction(SMS_ACTION);
				Broadcast1Activity.this.registerReceiver(r, intent);
			}
		});
    }
    //button onclick事件
    public void startService(View v){
    	Intent i = new Intent(Broadcast1Activity.this,StartService.class);
    	System.out.println("111");
    	this.startService(i);
    	System.out.println("222");
    }
}


SMSReceiver.java (广播接收类)

package com.cn;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;

public class SMSReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("have recevie message");
		Broadcast1Activity b = (Broadcast1Activity)context;
		Button button1 = (Button)b.findViewById(R.id.button1);
		System.out.println(button1.getText());
	}

}




StartService.java

package com.cn;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class StartService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind");
		return null;
	}
	
	@Override
	public void onCreate() {
		System.out.println("onCreate");
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		System.out.println("onStart");
		sendMsg("send msg");
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	public void sendMsg(String msg){
		 // 指定广播目标的 action (注:指定了此 action 的 receiver 会接收此广播)   
        Intent intent = new Intent("com.webabcd.service.msg");   
        // 需要传递的参数   
        intent.putExtra("msg", msg);   
        // 发送广播   
        this.sendBroadcast(intent);   
	}

}



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Broadcast1Activity"
                  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 android:name=".StartService" />
    </application>
</manifest>


该例子为完整的从Activity启动一个广播接收类, 然后启动一个Service, 并且从Service中发送消息到Activity

你可能感兴趣的:(Activity)