我有一个主要的Activity类,我在其中定义了挂起的意图、请求代码和SmsManager。但当我在一个模拟器中运行应用程序时,它显示应用程序没有发送短信的权限,而在另一个仿真器中,它只显示发送短信时发送的短信,而不显示发送短信时发送的短信。
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
The main activity class
public class MainActivity extends AppCompatActivity {
public static final String ACTION_SMS_SENT = "com.app.sms.SENT";
public static final String ACTION_SMS_DELIVERED = "com.app.sms.DELIVERED";
public static final int REQ_CODE_SENT = 10;
public static final int REQ_CODE_DELIVERED = 20;
Intent smsIntent;
Intent deliveredIntent;
PendingIntent piSent;
PendingIntent piDelivered;
SmsManager sms = SmsManager.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Defined all the intents and pending Intents
smsIntent = new Intent(ACTION_SMS_SENT);
deliveredIntent = new Intent(ACTION_SMS_DELIVERED);
piSent = PendingIntent.getBroadcast(this,REQ_CODE_SENT,smsIntent,PendingIntent.FLAG_ONE_SHOT);
piDelivered = PendingIntent.getBroadcast(this,REQ_CODE_DELIVERED,deliveredIntent,PendingIntent.FLAG_ONE_SHOT);
final EditText number = (EditText) findViewById(R.id.numberInput);
final EditText text = (EditText) findViewById(R.id.textInput);
Button send = (Button) findViewById(R.id.sendButton);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String numberText = number.getText().toString();
String message = text.getText().toString();
}
});
}
private void sendMsg(String numberText, String message) {
sms.sendTextMessage(numberText,null,message,piSent,piDelivered);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// And this is the SmsReceiver class that is to be called when the sms is sent by the application
package com.example.angshuman.smsimplementation;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MainActivity.ACTION_SMS_SENT)) {
//code will run when the sms is sent
if (intent.getAction().equals(MainActivity.REQ_CODE_SENT))
Toast.makeText(context, "Sms sent", Toast.LENGTH_LONG).show();
}
if (intent.getAction().equals(MainActivity.ACTION_SMS_DELIVERED)) {
//code will run when the sms is delivered
if (intent.getAction().equals(MainActivity.REQ_CODE_DELIVERED))
Toast.makeText(context, "Sms delivered", Toast.LENGTH_LONG).show();
}
}
}//这是android清单文件,声明了必要的权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.angshuman.smsimplementation">
<uses-permission android:name="android.permission.SEND_SMS"/>
<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=".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>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="com.app.sms.SENT"/>
<action android:name="com.app.sms.DELIVERED"/>
</intent-filter>
</receiver>
</application>
</manifest>//和logcat错误
02-16 22:22:05.004 17534-17534/com.example.angshuman.smsimplementation E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.angshuman.smsimplementation, PID: 17534
java.lang.SecurityException: Sending SMS message: uid 10083 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
at com.example.angshuman.smsimplementation.MainActivity.sendMsg(MainActivity.java:73)
at com.example.angshuman.smsimplementation.MainActivity.access$000(MainActivity.java:17)
at com.example.angshuman.smsimplementation.MainActivity$2.onClick(MainActivity.java:66)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)//我不明白代码中有什么问题。我试图使用它的pid从一个仿真器向另一个仿真器发送sms,但是从一个仿真器可以发送sms,但只显示已发送的短消息的吐司,而不是发送,并且在另一个仿真器中应用程序崩溃
发布于 2016-02-17 01:23:20
您只需在清单文件中添加android premission : android.permission.SEND_SMS即可。
发布于 2016-11-28 20:35:22
我不能百分之百确定出了什么问题,但我确实注意到您在发送消息时没有尝试/捕获。不过,这段代码对我来说一直都很有效。
protected void sendSMSMessage(String phoneNo, String msg) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/35438870
复制相似问题