字符串无法进入广播接收器

时间:2017-02-11 04:55:31

标签: android broadcastreceiver

我是android的新手,试图开发一个应用程序。我有一个BroadcastReceiver类和一个活动类。它是为管理音频控制(静音,振铃,振动)而创建的。就在接收短信收发器时检查消息体并将其与用户给定的关键字进行比较(在活动页面中使用编辑文本)在这里我遇到了一个麻烦,我传递了这个字符串(editText值为broadcastReceiver)。它到达那里但在try块内部这个字符串变量为null。任何一个人可以帮助我...

活动类

@Override
protected void onCreate(Bundle savedInstanceState) {.....

.......

acivatebtn.setOnClickListener(new View.OnClickListener(){....
....public void onClick(View v) {

String kslnt=txtkeysilent.getText().toString();....

Intent intent = new Intent("package.action.string");
            if (checksilnt.isChecked()){intent.putExtra("extra", kslnt);}
sendBroadcast(intent);

BroadcastReceiver类

public class IncomingSms extends BroadcastReceiver {

AudioControl audio;

final SmsManager sms = SmsManager.getDefault();

private AudioManager myAudioManager;

public String silent;

@Override
public void onReceive(Context context, Intent intent) {


    String action = intent.getAction();
    Log.i("Receiver", "Broadcast received: " + action);

    if(action.equals("package.action.string")){
        silent = intent.getExtras().getString("extra");

       /* int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, "senderNum: "+  state+rng+vbrt, duration);
        toast.show();*/
    }

    myAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE );

    final Bundle bundle = intent.getExtras();
    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);


                String message = currentMessage.getDisplayMessageBody().toString();

                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+  state+rng+vbrt, duration);
                toast.show();
                Log.i("SmsReceiver","message:" + message);
                if (message.equals(silent )){myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);}


                this.abortBroadcast();

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
}

}

这里值'silent'应该包含关键字(来自活动editText)

if (message.equals(silent )){myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);}

但是silent显示空值......

在此块中正确显示(此处显示editText字符串)

 String action = intent.getAction();
    Log.i("Receiver", "Broadcast received: " + action);
    if(action.equals("package.action.string")){
        silent= intent.getExtras().getString("extra");

       /* int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, "senderNum: "+  state+rng+vbrt, duration);
        toast.show();*/
    }

2 个答案:

答案 0 :(得分:0)

  

但是silent显示空值......

问题是在广播数据/意图时,您已将字符串值放入Intent&amp;在重新审核时,您正试图从Intent的附加内容中获取该字符串Bundle。因此,要解决此问题,您需要从Intent而非Intent附加内容(Bundle)获取。

你输入了像

这样的值
intent.putExtra("extra", kslnt);

不应该(特别是在您的情况下,因为您没有将值放在Bundle中)

silent = intent.getExtras().getString("extra");

所以应该获得值,如下所示

intent.getStringExtra("extra");

特别是您的修正方法

String action = intent.getAction();
    Log.i("Receiver", "Broadcast received: " + action);
    if(action.equals("package.action.string")){
        //silent= intent.getExtras().getString("extra"); // this is wrong
        intent.getStringExtra("extra"); //get String from intent getStringExtra method (Because you have put string values in Intent not in Intent's putExtras's (Bundle)
       /* int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, "senderNum: "+  state+rng+vbrt, duration);
        toast.show();*/
    }

如果有的话,请告诉我

答案 1 :(得分:0)

由于AudioManager未初始化而出现此问题,此处仅声明,未初始化

myAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

我通过添加此行来解决此问题