私はAndroidでSMSアプリに取り組んでおり、送信者が入力して[メッセージの送信]をクリックすると、メッセージはクラスEccで操作され、受信者に送信されます。受信者はこのメッセージを読み取ってEccクラスに送信する必要があり、そこで操作が行われ、送信者に返送されます。受信者にメッセージを送信することはできますが、受信者側では何も起こりません。
これがSmsReceiverクラスです
package com.example.smsTest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
Ecc ecc;
ecc = new Ecc(context);
String msg_receive = intent.getStringExtra("message1");
if (msg_receive != null) {
for(int n=0;n<messages.length;n++)
{
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms)
{
String ph=msg.getDisplayOriginatingAddress();
String body=msg.getMessageBody();
ecc.recv(ph,body);
Toast.makeText(context,"ecc entered at receiver", Toast.LENGTH_SHORT).show();
SMSTest.update("\n From:" +ph+"\n"+"message:"+body+"\n");
Toast.makeText(context,"updated",
Toast.LENGTH_SHORT).show();
}
}
}
}
これがeccクラスです
package com.example.smsTest;
import java.util.*;
public class Ecc{
//code
private SMSTest smsTest;
// Constructor
public Ecc(SMSTest smsTest) {
this.smsTest = smsTest;
}
public void recv(String phn, String smsg){
String[] spl1=str.split("\\.");
if("abc".equals(spl1[0])){
//code
sendSMS(ph,smsg)
}
if("pmg".equals(spl1[0])){
//code
sendSMS(ph,smsg)
}
}
//code
//---sends a SMS message to another device---
@TargetApi(Build.VERSION_CODES.DONUT)
@SuppressLint("NewApi")
public void sendSMS(String phoneNumber, String message)
{
/*
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, test.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
*/
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(smsTest, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(smsTest, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(smsTest, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(smsTest, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(smsTest, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(smsTest, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(smsTest, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@TargetApi(Build.VERSION_CODES.DONUT)
@SuppressLint("NewApi")
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(smsTest, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(smsTest, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
private void registerReceiver(BroadcastReceiver broadcastReceiver,
IntentFilter intentFilter) {
// TODO Auto-generated method stub
}
}
こちらがSMSTestクラスです
package com.example.smsTest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@TargetApi(Build.VERSION_CODES.DONUT)
@SuppressLint("NewApi")
public class SMSTest extends Activity
{
public final static String SMS_Message = "com.example.SMSTest.MESSAGE";
public final static String SMS_Phone = "com.example.SMSTest.MESSAGE";
static TextView txtmsg;
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
Ecc ecc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtmsg=(TextView)findViewById(R.id.txtmsg);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
ecc=new Ecc(this);
/*
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
*/
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
message="pmg."+message;
if (phoneNo.length()>0 && message.length()>0){
System.out.println("details verified");
Toast.makeText(getBaseContext(),"Ecc call clicked",
Toast.LENGTH_SHORT).show();
ecc.recv(phoneNo, message);
}
else
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}
public static void update(String msg) {
// TODO Auto-generated method stub
txtmsg.append(msg);
}
}
これがマニフェストファイルです
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsTest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMSTest"
android:label="@string/app_name">
<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="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
エラーは発生しません。送信者側は、受信者がメッセージを受信するように機能しますが、何も起こりません。