原创:kylin_zeng http://blog.chinaunix.net/uid/23795897.html
再此感谢mars 老师的帮助。
1、系统会产生广播,然后让注册到系统中的接收器接收到。如把一个usb处理程序注册到系统中,当插入usb系统就会发送广播消息事件通知usb处理程序的activity.
2、一个receive activity怎么注册到系统中,在AndroidMamifest.xml ->
<receiver android:name=".TestReceiver">
<intent-filter> //这个过滤器是决定TestReciver 接收哪些类型的事件,
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
3、recver:
public class TestReceiver extends BroadcastReceiver{ //这里面只有生成一次,一旦代码返回后生命就结束了。再此发送广播这个重新接收就会再次生成一个这个对象。
public TestReceiver(){
System.out.println("TestReceiver");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
}
}
4、事件产生:
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{
//import android.view.View.OnClickListener; 这里记得要用这个,否则会有一些乱七八糟的问题。
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
//在xml只有 <action android:name="android.intent.action.EDIT"/> 对应的EDIT 才能反应。recve里面才能进入构造函数,onReceive里面也才有进入。
TestActivity.this.sendBroadcast(intent);
}
}
}
5、如果想接收一个广播:
1)就要创建一个类来接收,这个类要继承 extends BroadcastReiver.,
2)这这个类中复写onReive 方法 //这里面就是说如果接收到了这个广播,你要干啥来着的。
3)在AndroidManifest注册到系统中。
<receiver android:name=".TestReceiver"> //注册
<intent-filter> //过滤器,看是否是你要的东西
<action android:name="android.intent.action.EDIT" /> //你要什么东西。
....
4)发送广播,在另一个activity中,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT); //设置哪一个action
TestActivity.this.sendBroadcast(intent); //发送广播。
TestBC.rar
阅读(32) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
再此感谢mars 老师的帮助。
1、系统会产生广播,然后让注册到系统中的接收器接收到。如把一个usb处理程序注册到系统中,当插入usb系统就会发送广播消息事件通知usb处理程序的activity.
2、一个receive activity怎么注册到系统中,在AndroidMamifest.xml ->
<receiver android:name=".TestReceiver">
<intent-filter> //这个过滤器是决定TestReciver 接收哪些类型的事件,
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
3、recver:
public class TestReceiver extends BroadcastReceiver{ //这里面只有生成一次,一旦代码返回后生命就结束了。再此发送广播这个重新接收就会再次生成一个这个对象。
public TestReceiver(){
System.out.println("TestReceiver");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
}
}
4、事件产生:
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{
//import android.view.View.OnClickListener; 这里记得要用这个,否则会有一些乱七八糟的问题。
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
//在xml只有 <action android:name="android.intent.action.EDIT"/> 对应的EDIT 才能反应。recve里面才能进入构造函数,onReceive里面也才有进入。
TestActivity.this.sendBroadcast(intent);
}
}
}
5、如果想接收一个广播:
1)就要创建一个类来接收,这个类要继承 extends BroadcastReiver.,
2)这这个类中复写onReive 方法 //这里面就是说如果接收到了这个广播,你要干啥来着的。
3)在AndroidManifest注册到系统中。
<receiver android:name=".TestReceiver"> //注册
<intent-filter> //过滤器,看是否是你要的东西
<action android:name="android.intent.action.EDIT" /> //你要什么东西。
....
4)发送广播,在另一个activity中,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT); //设置哪一个action
TestActivity.this.sendBroadcast(intent); //发送广播。
![](/image/default/fj.png)
相关热门文章
给主人留下些什么吧!~~
评论热议