mars老师[Android开发视频教学] 01_21 广播机制(1)

mar老师[Android开发视频教学] 01_21 广播机制(1)

创建项目:TestBC

==>testrecevier.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
	android:id="@+id/sendButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="发送消息给Receiver"
    />
</LinearLayout>


 

==>TestActivity.java

package mars.testbc;

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

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	private Button sendButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testreceiver);
        sendButton = (Button)findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new BroadcastListener());
    }
    class BroadcastListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_EDIT);
			TestActivity.this.sendBroadcast(intent);
		}
    	
    }
}


==>TestRecevier.java

package mars.testbc;

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

public class TestReceiver extends BroadcastReceiver{

	public TestReceiver(){
		System.out.println("TestReceiver");
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("onReceive");
	}
}


==>AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="mars.testbc" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".TestActivity" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

		<receiver android:name=".TestReceiver">
			<intent-filter>
				<action android:name="android.intent.action.PICK" />
			</intent-filter>
		</receiver>
	</application>
	<uses-sdk android:minSdkVersion="4" />
	<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest> 

 

 

 

posted on 2012-07-13 12:21  1.曲待续  阅读(140)  评论(0编辑  收藏  举报

导航