我正在创建一个应用程序,它将获取文本消息的内容并在弹出窗口中显示它。收到短信时,我的广播接收器没有拍摄。
广播接收器类
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
/** sms sender phone */
String smsSender = "";
/** body of received sms */
String smsBody = "";
/** timerstamp */
long timestamp = 0L;
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
smsSender += msgs[i].getOriginatingAddress();
smsBody += msgs[i].getMessageBody().toString();
timestamp += msgs[i].getTimestampMillis();
}
intent.putExtra("sender", smsSender);
intent.putExtra("body", smsBody);
intent.putExtra("timestamp", timestamp);
}
}
对话活动 公共类PopSMSActivity扩展了Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog();
}
private void showDialog() {
final String sms_sender = getIntent().getStringExtra("sender");
final String sms_body = getIntent().getStringExtra("body");
final long timestamp = getIntent().getLongExtra("timestamp", 0L);
final String display = sms_sender + "\n" + sms_body + "\n" + timestamp;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(display)
.setCancelable(false)
.setPositiveButton("Reply",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// reply by calling SMS program
smsReply(sms_sender, sms_body);
}
})
.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// go back to the phone home screen
goHome();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void smsReply(String sender, String body) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("address", sender);
sendIntent.putExtra("sms_body", body);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
this.finish(); // close this Activity now
}
private void goHome() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
this.finish();
}
}
清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.intereceptsms.SMSReceiver" >
<intent-filter
android:exported="true"
android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.intereceptsms.PopSMSActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
我没有提出任何错误,所以必须有一些我缺少的东西,任何帮助都会很棒。谢谢。
答案 0 :(得分:1)
我的广播接收器在收到短信时未拍摄
这是因为除了创建Intent
之外,它没有做任何事情,然后忽略了它。据推测,您的计划是PopSMSActivity
,在这种情况下,您需要startActivity()
来电。
另请注意,虽然这对于实验来说很好,但如果因为文本消息到达而导致某些事情超出前景,则用户可能会非常恼火。