我按照教程接收短信并在我的应用上显示。然而,当我尝试使用Android设备监视器(以及使用android终端的telnet)发送短信时,模拟器本身会收到短信,但应用程序什么都不做。
代码如下......
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.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;
public class Sms extends Activity {
TextView t1;
BroadcastReceiver receiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
t1 = (TextView) findViewById(R.id.textView);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arr0, Intent arr1) {
processReceive(arr0, arr1);
}
};
registerReceiver(receiver, filter);
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
public void processReceive(Context context, Intent intent) {
Toast.makeText(context, "Incoming...", Toast.LENGTH_LONG).show();
Bundle bundle = intent.getExtras();
Object[] objArr = (Object[]) bundle.get("pdus");
String sms = "";
for (int i = 0; i < objArr.length; i++) {
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[]) objArr[i]);
String smsBody = smsMsg.getMessageBody();
sms += smsBody;
}
t1.setText(sms);
}
}
Manifest.xml如下......
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Sms">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
有什么建议吗?模拟器接收短信但不接收应用程序。我也尝试在intent-filter标签中使用android:priority属性,但没有任何效果。吐司应该显示&#34; Incoming ...&#34;当调用onReceive方法时也没有调用(表明应用程序没有收到短信)。权限对我来说也很好看。请帮忙......
这是Youtube链接:https://www.youtube.com/watch?v=h-zYXVODiPo
答案 0 :(得分:-1)
karma-*