android:broadcast_01

minifest配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boardcast_activity"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".BoardCast_Reciver">
<intent-filter>
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</receiver>

</application>

</manifest>

 

接收端

package com.example.boardcast_activity;

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

public class BoardCast_Reciver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("jjjj");
}

}

事件触发:

package com.example.boardcast_activity;

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

public class MainActivity extends Activity {

private Button mybutton = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mybutton = (Button)findViewById(R.id.boardcast_click_button);
mybutton.setOnClickListener(new mybutton_listener());

}

class mybutton_listener implements OnClickListener{

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
MainActivity.this.sendBroadcast(intent);

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

posted @ 2012-09-01 16:04  黑-色-柳—丁  阅读(138)  评论(0编辑  收藏  举报