gpt4 book ai didi

java - 短信未发送

转载 作者:行者123 更新时间:2023-12-01 10:21:12 24 4
gpt4 key购买 nike

我在发送短信时遇到问题。当我单击按钮时,什么也没有发生。没有消息或任何东西。没有意义的是,它作为短信发送的独立应用程序工作,但如果我将代码集成到更大的项目中,它不会发送短信。什么都没发生。某些东西必须与 Smsmanager 或其他东西发生冲突。谁能给我建议为什么它不再起作用?

Android list

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_auto">

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".engine.MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.MapsActivity1">

</activity>

<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" />
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyCMFYfJ6aOuxJk3W0vmhF6Nou3TP_qIU6c" />

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--
Main music service, provides media browsing and media playback services to
consumers through MediaBrowserService and MediaSession. Consumers connect to it through
MediaBrowser (for browsing) and MediaController (for playback control)
-->
<service android:name=".MyMusicService" android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<service android:name=".MyMessagingService" />

<receiver android:name=".MessageReadReceiver">
<intent-filter>
<action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_READ" />
</intent-filter>
</receiver>
<receiver android:name=".MessageReplyReceiver">
<intent-filter>
<action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_REPLY" />
</intent-filter>
</receiver>

</application>




</manifest>

SendSmsActivity.java

package android_auto.engine;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android_auto.R;


public class SendSmsActivity extends Activity {

Button buttonSend;
EditText textPhoneNo;
EditText textSMS;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);

buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textSMS = (EditText) findViewById(R.id.editTextSMS);

buttonSend.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSmsActivity.this, 0, new Intent("SMS_SENT"), 0);


smsManager.sendTextMessage(phoneNo, null, sms, pendingIntent, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}

}
});
}
}

activity_sms.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Phone Number : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:phoneNumber="true" >
</EditText>

<TextView
android:id="@+id/textViewSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter SMS Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:gravity="top" />

<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />

</LinearLayout>

enter image description here

最佳答案

尝试在点击监听器中编辑代码

PendingIntent pendingIntent = PendingIntent.getBroadcast(SendSmsActivity.this, 0,new Intent("SMS_SENT"), 0);
smsManager.sendTextMessage(phoneNo, null, sms, pendingIntent, null);

希望这可以帮助您使其正常工作。

关于java - 短信未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com