AndroidでSMSを送信する際に問題があります。私は2つのクラスを持っています。それらの1つはSendMessage.javaで、もう1つはSendingSms.javaで、SendSms.javaファイルでメッセージを送信したいので、sendSmsクラスがあり、SendMessage.javaファイルでこのsendSms関数を使用したいのですが、できませんでしたいけません。さらに、SendingSms.java にある関数を使用できますが、sms コードを送信すると機能しません。どうしたの?
ここに私のSendMessage.javaにある関数があります
public void sendMessageButton (View view)
{
//SendingSms send = new SendingSms();
if(who_detail.getText().toString().length()>0 && message_text.getText().toString().length()>0 )
{
sendSMS(who_detail.getText().toString(), message_text.getText().toString());
}
else
{
Toast.makeText(getApplicationContext(), R.string.sendingsms_error, Toast.LENGTH_LONG).show();
}
}
ここに私の sendinSms.java があります
package com.example.stildeneme;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;
public class SendingSms extends Activity {
public void sendSMS(String telNo, String mesaj)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
switch(getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Gönderildi",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS iletildi",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS iletilemedi",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(telNo, null, mesaj, sentPI, deliveredPI);
Toast.makeText(getApplicationContext(), telNo.toString() + " - " + mesaj.toString(), Toast.LENGTH_LONG).show();
}
}
ところで、SendMessage クラスを SendingSms クラスから拡張しました。
ここに私のlogcatがあります
11-30 09:48:09.613: E/AndroidRuntime(1007): FATAL EXCEPTION: main
11-30 09:48:09.613: E/AndroidRuntime(1007): java.lang.IllegalStateException: Could not execute method of the activity
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.view.View$1.onClick(View.java:2144)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.view.View.performClick(View.java:2485)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.view.View$PerformClick.run(View.java:9080)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.os.Handler.handleCallback(Handler.java:587)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.os.Looper.loop(Looper.java:123)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-30 09:48:09.613: E/AndroidRuntime(1007): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007): at java.lang.reflect.Method.invoke(Method.java:507)
11-30 09:48:09.613: E/AndroidRuntime(1007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-30 09:48:09.613: E/AndroidRuntime(1007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-30 09:48:09.613: E/AndroidRuntime(1007): at dalvik.system.NativeStart.main(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007): Caused by: java.lang.reflect.InvocationTargetException
11-30 09:48:09.613: E/AndroidRuntime(1007): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007): at java.lang.reflect.Method.invoke(Method.java:507)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.view.View$1.onClick(View.java:2139)
11-30 09:48:09.613: E/AndroidRuntime(1007): ... 11 more
11-30 09:48:09.613: E/AndroidRuntime(1007): Caused by: java.lang.SecurityException: Sending SMS message: User 10048 does not have android.permission.SEND_SMS.
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.os.Parcel.readException(Parcel.java:1322)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.os.Parcel.readException(Parcel.java:1276)
11-30 09:48:09.613: E/AndroidRuntime(1007): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
11-30 09:48:09.613: E/AndroidRuntime(1007): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
11-30 09:48:09.613: E/AndroidRuntime(1007): at com.example.stildeneme.SendingSms.sendSMS(SendingSms.java:69)
11-30 09:48:09.613: E/AndroidRuntime(1007): at com.example.stildeneme.SendMessage.sendMessageButton(SendMessage.java:76)
11-30 09:48:09.613: E/AndroidRuntime(1007): ... 14 more