首页 android
广播发送
发布时间:2016年07月25日 评论数:抢沙发 阅读数:170
1.MainActivity主页
package com.example.sendbroadcast; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /*** * 发送广播,按钮一普通广播 * 按钮二有序广播 * 按钮三粘性广播 * @author Administrator * */ public class MainActivity extends Activity { Button b1,b2,b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b1.setOnClickListener(new OnClickListener() {//发送普通广播() @Override public void onClick(View arg0) { Intent intent = new Intent("mystl"); intent.putExtra("name", "asd"); sendBroadcast(intent); } }); b2.setOnClickListener(new OnClickListener() {//发送有序广播(接收者通过priority) @Override public void onClick(View arg0) { Intent intent = new Intent("ordorBroad"); intent.putExtra("age", 15); sendOrderedBroadcast(intent, null); } }); b3.setOnClickListener(new OnClickListener() {//粘性广播发送 @Override public void onClick(View arg0) { Intent intent1 = new Intent("stickybroad"); intent1.putExtra("ss", "hahaha"); sendStickyBroadcast(intent1); } }); } }
2.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" android:text="发送普通广播" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="30dp" android:text="发送有序广播" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:text="发送粘性广播" /> </RelativeLayout>
相关文章