Android研究(3)
import android.content.Context;
import android.content.Intent;
public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){
System.out.println("constructed!");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("OK");
}
}
当系统收到短信信后就会输出OK,也就是执行上面的onReceive方法
方法二:通过代码注册广播
Java代码
package cn.edu.design;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import cn.edu.subclass.MyBroadCastReceiver;
public class AndroidBroadCastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyBroadCastReceiver my=new MyBroadCastReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(my, filter);
}
}
Java代码
package cn.edu.subclass;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){
System.out.println("constructed!");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("OK");
相关新闻>>
- 发表评论
-
- 最新评论 更多>>