我的代码有什么问题? 我会尽量简化。
public static void sendMMS(final Context context, String phoneNumber, String messageText) throws Exception {
final String fileName = "send." + String.valueOf(Math.abs(new Random().nextLong())) + ".dat";
File mSendFile = new File(context.getCacheDir(), fileName);
final byte[] pdu = MmsMessagingDemo.buildPdu(context, phoneNumber, "hello", messageText);
FileOutputStream writer = null;
try {
writer = new FileOutputStream(mSendFile);
writer.write(pdu);
}
finally {
if (writer != null) {
writer.close();
}
}
Uri writerUri = (new Uri.Builder())
.authority("com.example.android.apis.os.MmsFileProvider")
.path(fileName)
.scheme(ContentResolver.SCHEME_CONTENT)
.build();
SmsManager sms = SmsManager.getDefault();
sms.sendMultimediaMessage(context, writerUri, null, null, null);
}
此方法在AsyncTask中调用 我使应用程序默认smsApp
public void makeDefautSmsApp() {
final String myPackageName = getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
startActivity(intent);
}
}
我已获得所有许可
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_SMS, Manifest.permission.READ_PHONE_NUMBERS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
并在清单中
<manifest package="com.example.sendsms"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- SMS -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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>
</manifest>
但是我没有错误,但是我的消息没有发送 MMSManager.sendMMS(MainActivity.this,“ + 33628xxxxxx”,“ helloWorld”);
也许有人有主意吗?
答案 0 :(得分:0)
我解决了我的问题,因为我在同一个打过它的电话上打了彩信。 它适用于短信,但似乎不适用于短信。
但是,如果我在其他手机上打听,那是可以的