我在将变量从Activity传递给BroadcastReceiver时遇到了一些问题... 这是我的代码:
这是我的广播接收器代码...我尝试从我的活动中获得的一个电话号码中捕捉短信...
public class SMSMonitor extends BroadcastReceiver
{
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public static String phone_number = "";
public static String msg_body = "";
public static final String SMS_EXTRA_NAME = "pdus";
@Override
public void onReceive(Context context, Intent intent)
{
String phone = intent.getExtras().getString("trusted_num");
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
System.out.println("Phone entered: "+phone);
}
}
}
这是我的活动代码:
public class Settings extends Activity implements OnClickListener{
private Button btn_save;
private EditText txt_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//set Save button
btn_save = (Button)findViewById(R.id.btn_save);
txt_phone = (EditText)findViewById(R.id.et_phone);
btn_save.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_settings, menu);
return true;
}
@Override
public void onClick(View v)
{
if (v == btn_save)
{
try
{
String phone_num = txt_phone.getText().toString();
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
}
catch(Exception e)
{
System.out.println("Error: "+e.getLocalizedMessage());
}
}
}
}
在此代码中,我有输入电话号码的文本字段,我需要使用intent.putExtra()
方法传递给BroadcastReceiver,但在LogCat中我看到,该变量没有通过:
07-25 18:43:57.382: I/System.out(14245): Phone number: +37129690449
07-25 18:43:57.382: I/System.out(14245): Phone entered: null
那我在这里做错了什么?
UPD 也许代码不正确,但它对我有用......
public void onReceive(Context context, Intent intent)
{
phone = intent.getExtras().getString("trusted_num");//get trusted phone number from Settings screen
//receive SMS
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
}
//check if number is not null
if (phone != null && phone != "")
{
System.out.println("Phone entered: "+phone);
}
}
}
答案 0 :(得分:1)
您无法将意图传递给广播接收器。 “BroadcastReceiver无法查看或捕获与startActivity()一起使用的Intent” https://developer.android.com/reference/android/content/BroadcastReceiver.html
我有一个类似的问题,并通过使用IntentServices和Activities的组合来解决它。你必须重组你的程序以适应这些指导
答案 1 :(得分:0)
嗯,有些事情不匹配:
你首先发送一个没有动作的意图,但是你指的是Broadcastreceiver的类;不喜欢那样:
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
但请尝试:
Intent i = new Intent("my_package_name.Some_general_constant");
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
然后,您的BroadcastReceiver
应该知道它也可以处理"Some_general_constant"
动作。因此,请在您的清单文件中为SMSMonitor
注册一项额外操作:
<receiver android:name=".package_to_bla_bla.SMSMonitor">
<intent-filter>
<action android:name="my_package_name.Some_general_constant"/>
</intent-filter>
</receiver>
然后在您的SMSMonitor中,您需要添加else if
语句来处理此广播:
else if("my_package_name.Some_general_constant".equals(intent.getAction())
{
// get the data from intent and use it
}