此实例实现功能为在不打开新的Activity的前提下,从BroadcastReceiver传递数据给Activity
实现很简单,但使用到了自定义广播,以免遗忘在此记录:
代码
以通过广播读取短信再传递给前台Activity进行显示为例
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >
<EditText android:id="@+id/main_et" android:hint="短信内容!" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" />
</RelativeLayout>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SMSBroadcastReceiver extends BroadcastReceiver{
//指定动作的名称
public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION.equals(intent.getAction())) {
//根据接收到的意图对象获取短信
SmsMessage[] msgs = getMessageFromIntent(intent);
//提取短信内容
StringBuilder sBuilder = new StringBuilder();
if (msgs != null && msgs.length > 0 ) {
for (SmsMessage msg : msgs) {
sBuilder.append("接收到了短信:\n发件人是:");
sBuilder.append(msg.getDisplayOriginatingAddress());
sBuilder.append("\n------短信内容-------\n");
sBuilder.append(msg.getDisplayMessageBody());
}
}
Intent mIntent=new Intent("ReadSMS");
mIntent.putExtra("sms", sBuilder.toString());
context.sendBroadcast(mIntent);
}
}
//从意图获取短信对象的具体方法
public static SmsMessage[] getMessageFromIntent(Intent intent) {
SmsMessage retmeMessage[] = null;
Bundle bundle = intent.getExtras();
Object pdus[] = (Object[]) bundle.get("pdus");
retmeMessage = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
byte[] bytedata = (byte[]) pdus[i];
retmeMessage[i] = SmsMessage.createFromPdu(bytedata);
}
return retmeMessage;
}
}
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.text.TextUtils;
import android.widget.EditText;
import com.example.locationdemo2.R;
public class MainActivity extends Activity {
private EditText editText;
private MyBroadcastReceiver myBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.main_et);
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("ReadSMS");
registerReceiver(myBroadcastReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("ReadSMS")) {
System.out.println("收到广播!");
String stringExtra = intent.getStringExtra("sms");
if (!TextUtils.isEmpty(stringExtra)) {
editText.setText(stringExtra);
System.out.println(stringExtra);
}
}
}
}
}