Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063609
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-04-22 20:36:22

Java代码  收藏代码
  1. package com.amaker.broadcast;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.IntentFilter;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. /** 
  11.  * 1,点击按钮发送一个广播 
  12.  * 2,注册广播时有两种注册:一,在配置文件中静态注册 二、在代码中动态注册 
  13.  * 注意:注册方法写在:onResume里面,注销方法写在:onPause里面 
  14.  * 
  15.  */  
  16. public class MainActivity extends Activity {  
  17.     private Button btn_send;  
  18.     public static final String MY_ACTION = "com.amaker.broadcast.MY_ACTION";  
  19.     MyReceiver r = new MyReceiver();  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         btn_send = (Button) findViewById(R.id.button1);  
  26.           
  27.         btn_send.setOnClickListener(new OnClickListener() {  
  28.               
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 send();  
  32.             }  
  33.         });  
  34.     }  
  35.     //发送一个广播  
  36.     void send(){  
  37.         Intent intent = new Intent(MY_ACTION);  
  38.         String msg = "我给你发了一个广播,是否收到?";  
  39.         intent.putExtra("msg", msg);  
  40.         sendBroadcast(intent);  
  41.     }  
  42.     //动态注册广播  
  43.     @Override  
  44.     protected void onResume() {  
  45.         super.onResume();  
  46.         IntentFilter filter = new IntentFilter();  
  47.         filter.addAction(MY_ACTION);  
  48.         registerReceiver(r, filter);  
  49.     }  
  50.     //注销广播  
  51.     @Override  
  52.     protected void onPause() {  
  53.         super.onPause();  
  54.         unregisterReceiver(r);  
  55.     }  
  56. }  


Java代码  收藏代码
  1. package com.amaker.broadcast;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyReceiver extends BroadcastReceiver{  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         String msg = intent.getStringExtra("msg");  
  13.         //收到广播,简单Toast显示一下  
  14.         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();  
  15.     }  
  16.   
  17. }  


AndroidManifest.xml:
Java代码  收藏代码
  1. "1.0" encoding="utf-8"?>  
  2. ""  
  3.       package="com.amaker.broadcast"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     "8" />  
  7.   
  8.     "@drawable/icon" android:label="@string/app_name">  
  9.         ".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.               
  12.                 "android.intent.action.MAIN" />  
  13.                 "android.intent.category.LAUNCHER" />  
  14.               
  15.           
  16.       
  17.       
  18.   
  19.   
  20.      
  21.  
阅读(1555) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~