使用自定义意图广播来实现当前流量。将您的代码更改为:
第1步 :
SMS_RECEIVED
在 Manifest 中注册一个自定义 Intent为:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="xx.xxx.xxx.intent.action.SMS_STATUS_ACTION" />
</intent-filter>
</receiver>
第2步 :
public class SmsReceiver extends BroadcastReceiver {
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(SmsReceiver.STATUS_INTENT)) {
// get value here sended from Activity
}
else{
// Check for SMS_RECEIVED Action here
}
}
}
第 3 步:
从您的活动中发送价值,使用sendBroadcast
:
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
@Override
public void onClick(View v) {
int checked = 0;
if(this.param.isChecked()){
checked = 1;
}
// put value here
Intent intent = new Intent();
intent.putInt("param", checked);
intent.setAction(CUSTOM_INTENT);
sendBroadcast(intent);
}