BroadcastReceiver的onReceive不执行

a869666620 2016-02-02 10:25:18
 package net.zx;

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

public class ReceiverActivity extends Activity {

private Button bind_receiver;
private Button unbind_receiver;
private SMSReceiver smsReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver_main);
bind_receiver = (Button) findViewById(R.id.bind_receiver);
bind_receiver.setOnClickListener(new BindButtonListener());
unbind_receiver = (Button) findViewById(R.id.unbind_receiver);
unbind_receiver.setOnClickListener(new UnbindButtonListener());
}

class UnbindButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
ReceiverActivity.this.unregisterReceiver(smsReceiver);
}
}
class BindButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
smsReceiver = new SMSReceiver();
Intent intent = new Intent();
intent.setAction("android.provider.Telephony.SMS_RECEIVED");
//监听短信
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
ReceiverActivity.this.registerReceiver(smsReceiver, filter);
// ReceiverActivity.this.sendBroadcast(intent);
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.receiver, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


package net.zx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver{

public SMSReceiver(){
System.out.println("已执行。。。。");
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receiver message");
Bundle bundle = intent.getExtras();
Object[] object = (Object[])bundle.get("pdus");
SmsMessage sms[]=new SmsMessage[object.length];
for(int i=0;i<object.length;i++)
{
sms[0] = SmsMessage.createFromPdu((byte[])object[i]);
Toast.makeText(context, "来自"+sms[i].getDisplayOriginatingAddress()+" 的消息是:"+sms[i].getDisplayMessageBody(), Toast.LENGTH_SHORT).show();
}
//终止广播,在这里我们可以稍微处理,根据用户输入的号码可以实现短信防火墙。
// abortBroadcast();
}

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.zx"
android:versionCode="1"
android:versionName="1.0" >

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

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

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
-->

</application>
<!-- 权限申请 -->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
...全文
485 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
草稿本子 2017-09-21
  • 打赏
  • 举报
回复
楼主,请问你是怎么解决这个问题的
开开心心? 2017-02-15
  • 打赏
  • 举报
回复
咋回事呀,我也触发不了。
a869666620 2016-02-02
  • 打赏
  • 举报
回复
我懂了,结贴
a869666620 2016-02-02
  • 打赏
  • 举报
回复
ReceiverActivity.this.registerReceiver(smsReceiver, filter);我这里已经注册了还需要发送吗
jklwan 2016-02-02
  • 打赏
  • 举报
回复
你把发送广播的代码注释掉了,就不会发广播了,当然接收不到。
a869666620 2016-02-02
  • 打赏
  • 举报
回复
构造执行了,onReceive没有执行

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧