我确实在寻找解决方案,但是没有一个可行,其中一个似乎可行,但是这次,错误发生在另一个地方。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ordulou.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_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" android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>
这是接收器类:
package com.example.ordulou.myapplication;
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 TAG = "Message recieved";
@Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Start Application's MainActivty activity
Intent smsIntent = new Intent(context, MainActivity.class);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
smsIntent.putExtra("MessageNumber", messages.getOriginatingAddress());
smsIntent.putExtra("Message", messages.getMessageBody());
context.startActivity(smsIntent);
Toast.makeText(context, "SMS Received From
:"+messages.getOriginatingAddress()+"\n"+
messages.getMessageBody(),
Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Wifi toggled", Toast.LENGTH_SHORT).show();
}
}
我真的很疯狂,我尝试了一个新项目中最基本的步骤,但都没有用, 到目前为止我尝试过的:
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
MY_PERMISSIONS_REQUEST_SMS_RECEIVE);*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_SMS_RECEIVE) {
// YES!!
Log.i("TAG", "MY_PERMISSIONS_REQUEST_SMS_RECEIVE --> YES");
}
}