束值始终是空的广播接收器? [英] Bundle value is always null in Broadcast receiver?

查看:112
本文介绍了束值始终是空的广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是广播接收器在我的项目隐藏的短信,我有两个按钮,一个是寄存器和一个用于注销广播接收机。

下面的问题是 - 当我preSS按钮注册其打开广播,但是当一个短信接收器code的功能不工作也不会隐藏短信。

在接收器code我检查包值是否不等于空则仅code将执行的隐藏部分,现在我的问题是如何改变束值时,短信开始收到的或有没有办法听incommig 短信

我给完整的code请任何一个找到解决办法

//我Activity.java

 包sam.ll;进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.telephony.SmsMessage;
 进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.Toast;公共类SAMM延伸活动{
按钮B1,B2;
广播接收器mReceiver;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    B1 =(按钮)findViewById(R.id.button1);
    B2 =(按钮)findViewById(R.id.button2);
    mReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            // TODO自动生成方法存根
            Log.i(叫我,!);
            的System.out.println(podaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
            // ---获得通过SMS消息---
            捆绑包= intent.getExtras();
            的System.out.println(束值issss+包);
            SmsMessage []封邮件= NULL;
            字符串str =;
            如果(捆绑!= NULL)
            {
                abortBroadcast();
                // ---检索收到的短信---
                [对象]的PDU =(对象[])bundle.get(的PDU);
                封邮件=新SmsMessage [pdus.length]
                的for(int i = 0; I< msgs.length;我++){
                    封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                    STR + =短信来自+封邮件[I] .getOriginatingAddress();
                    STR + =;
                    STR + =封邮件[I] .getMessageBody()的toString()。
                    STR + =\\ n;
                }
                // ---显示新短信---
                Toast.makeText(背景下,STR,Toast.LENGTH_SHORT).show();
            }
        }
        };
    b1.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            IntentFilter的IntentFilter的=新的IntentFilter(android.intent.action.MAIN);
            samm.this.registerReceiver(mReceiver,IntentFilter的);            意图I =新意图(android.intent.action.MAIN);
            sendBroadcast(ⅰ);        }
    });
    b2.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            unregisterReceiver(mReceiver);
        }
    });  }
}

// main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好
/>
<按钮的android:文本=按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< /按钮>
<按钮的android:文本=按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< /按钮>
< / LinearLayout中>

//的manifest.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  包=sam.ll
  安卓版code =1
  机器人:=的versionName1.0>
<采用-SDK安卓的minSdkVersion =8/><应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
    <活动机器人:SAMMNAME =
              机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <接收机器人:名字=。mReceiver/>
    <使用许可权的android:NAME =android.permission.SEND_SMS>
< /使用许可权>
<使用许可权的android:NAME =android.permission.READ_SMS/>
<使用许可权的android:NAME =android.permission.RECEIVE_SMS>
< /使用许可权>< /用途>
< /清单>


解决方案

替换上的B1点击code到

  b1.setOnClickListener(新OnClickListener(){
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        IntentFilter的IntentFilter的=新的IntentFilter(android.provider.Telephony.SMS_RECEIVED);
        samm.this.registerReceiver(mReceiver,IntentFilter的);
    }
});

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>

解决方案

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);
    }
});

这篇关于束值始终是空的广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆