Bundle value is always null in Broadcast receiver?

2019-09-05 03:02发布

I am using a BroadcastReceiver for hiding sms in my project I have two button one for register and one is for unregister the broadcast receiver.

Here is the problem - when I press the button to register its turns the broadcast on but when a sms is receiver the functionality of code does not working it will not hide the SMS.

In the receiver code I am checking whether the bundle value is not equal to null then only the code will executed the hiding part now my problem is that how to change the bundle value when a sms is begin received or is there any way to listen incommig sms

I am giving the complete code please any one find the solution

//my Activity.java

package sam.ll;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
 import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class samm extends Activity {
Button b1,b2;
BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    mReceiver  = new BroadcastReceiver() {


        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("calling me "," !!!");
            System.out.println("podaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        
            System.out.println("bundle value issss"+bundle);
            SmsMessage[] msgs = null;
            String str = "";            
            if (bundle != null)
            {
                abortBroadcast();
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += " :";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";        
                }
                //---display the new SMS message---
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            }                 
        }
        };
    b1.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
            samm.this.registerReceiver(mReceiver, intentFilter);

            Intent i = new Intent("android.intent.action.MAIN");
            sendBroadcast(i);

        }
    });
    b2.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            unregisterReceiver(mReceiver);
        }
    });

  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<Button android:text="Button" 
android:id="@+id/button1" 
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="Button" 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
</LinearLayout>

//manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="sam.ll"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".samm"
              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=".mReceiver" />
    <uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>

</application>
</manifest>

2条回答
小情绪 Triste *
2楼-- · 2019-09-05 03:29

When you click your first button, the receiver is registered to listen for the intent "android.intent.action.MAIN" which is created by your own code and does not have any bundle attached (you do not attach anything). Do not subscribe for this fake intent and do not send it to your own broadcast receiver.

To subscribe for incoming SMS you need to listen for other intents:

    IntentFilter intentSMSReceiver = new IntentFilter();
    intentSMSReceiver.addAction("android.intent.action.DATA_SMS_RECEIVED");
    intentSMSReceiver.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(mReceiver, intentSMSReceiver);

This intent is issued when your device gets an SMS.

查看更多
Explosion°爆炸
3楼-- · 2019-09-05 03:36

Replace the code on click of b1 to

b1.setOnClickListener(new OnClickListener() {


    public void onClick(View v) {
        // TODO Auto-generated method stub
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        samm.this.registerReceiver(mReceiver, intentFilter);
    }
});
查看更多
登录 后发表回答