我是Android的新手,我尝试通过我的应用程序接收短信。正在发送短信,因此权限应该可以。首先,我想在Toast中显示接收到的SMS文本,但我阅读使用Log会更好。我正在使用它,并且没有BroadcastReciver的任何信号。
我尝试在清单中添加接收器,并记录日志以查看它是否有效,但是日志中没有任何内容
MainActivity:
package pl.edu.pwr.student.a235420_ks_a5;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button importSimData;
private Button sendMessage;
private EditText smsMessagePhoneNumber;
private EditText smsMessageText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
importSimData = findViewById(R.id.button1);
sendMessage = findViewById(R.id.button2);
smsMessagePhoneNumber = findViewById(R.id.editText1);
smsMessageText = findViewById(R.id.editText2);
SmsReceiver myReciver = new SmsReceiver();
Log.i("MAIN_ACTIV", "On create");
importSimData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
Toast.makeText(MainActivity.this, "NO permission!",
Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Add permissions in settings",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "you have permission!",
Toast.LENGTH_LONG).show();
TelephonyManager tm = (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
int phoneType = tm.getPhoneType();
int phoneCallState = tm.getCallState();
int phoneNetworkType = tm.getDataNetworkType();
String phoneSubscriberId = tm.getSubscriberId();
Toast.makeText(MainActivity.this, String.valueOf(phoneType),
Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, String.valueOf(phoneCallState ),
Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, String.valueOf( phoneNetworkType),
Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, phoneSubscriberId,
Toast.LENGTH_LONG).show();
}
}
});
sendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
//NO permission
Toast.makeText(MainActivity.this, "NO permission!",
Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Add permissions in settings",
Toast.LENGTH_LONG).show();}
else {
//you have
String phoneNumber = smsMessagePhoneNumber.getText().toString();
String smsText = smsMessageText.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, smsText, null, null);
}
}
});
}
}
SMSReceiver:
package pl.edu.pwr.student.a235420_ks_a5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_REC_ACTION =
"android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("SMS_RECIVER", "Recived Broadcast");
if (intent.getAction().
equals(SmsReceiver.SMS_REC_ACTION)) {
Log.i("SMS_RECIVER", "Recived SMS");
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])
bundle.get("pdus");
for (Object pdu : pdus) {
SmsMessage smsMessage =
SmsMessage.createFromPdu
((byte[]) pdu);
sb.append("body - " + smsMessage.
getDisplayMessageBody());
}
}
Toast.makeText(context, "SMS RECEIVED - "
+ sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.edu.pwr.student.a235420_ks_a5">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="PACKAGE_NAME.android.action.broadcast"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
我想从我的应用程序中收到的短信中获取文本,并在日志中显示或更好地在Toast中显示