안드로이드 개발 질문/답변
(글 수 45,052)
package my.AndroidSms;
import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver { Location location; LocationManager locationManager; Context mContext;
@Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub Bundle bundle = arg1.getExtras(); SmsMessage[] msgs = null; String number = ""; String message = ""; mContext = arg0;
if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i = 0; i < msgs.length; i++) { msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); number = msgs[i].getOriginatingAddress(); // 전화번호 message = msgs[i].getMessageBody().toString(); Log.d("SmsReceiver", "number:" + number + " Message :" + message);
Toast.makeText(arg0, "number:" + number + " Message :" + message, Toast.LENGTH_LONG).show();
if (message.equals("1234")) { Log.d("SmsReceiver", "call sendMyLocation(context)"); sendSMS(number, message); } } }
}
location = locationManager.getLastKnownLocation(provider);
if (location == null) { try { Toast.makeText(context, "에뮬레이터 GPS를 작동해 주세요", 10000).show(); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } location = locationManager.getLastKnownLocation(provider); } }
private void sendSMS(String phoneNumber, String message) { /* * PendingIntent pi = PendingIntent.getActivity(this, 0, new * Intent(this, AndroidSms.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(mContext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0);
// ---when the SMS has been sent--- mContext.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(mContext, "Generic failure", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(mContext, "No service", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(mContext, "Null PDU", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(mContext, "Radio off", Toast.LENGTH_SHORT) .show(); break; } } }, new IntentFilter(SENT));
// ---when the SMS has been delivered--- mContext.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(mContext, "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(mContext, "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); }
} 위와 같은 소스로 되어 있습니다 헌데..
문자 수신까지는 잘 되는데. 다시 발신하려고 하면 죽어 나가네요.
죽는 시점은 mContext.registerReceiver(new BroadcastReceiver())
이 부분 입니다.
그리고 LOG를 확인하면
01-19 02:32:33.269: ERROR/AndroidRuntime(31914): Uncaught handler: thread main exiting due to uncaught exception
01-19 02:32:33.409: ERROR/AndroidRuntime(31914): java.lang.RuntimeException: Unable to start receiver my.AndroidSms.SmsReceiver: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ActivityThread.access$3100(ActivityThread.java:119) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.os.Handler.dispatchMessage(Handler.java:99) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.os.Looper.loop(Looper.java:123) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ActivityThread.main(ActivityThread.java:4363) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at java.lang.reflect.Method.invokeNative(Native Method) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at java.lang.reflect.Method.invoke(Method.java:521) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at dalvik.system.NativeStart.main(Native Method) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): Caused by: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ReceiverRestrictedContext.registerReceiver(ApplicationContext.java:126) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ReceiverRestrictedContext.registerReceiver(ApplicationContext.java:120) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at my.AndroidSms.SmsReceiver.sendSMS(SmsReceiver.java:146) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at my.AndroidSms.SmsReceiver.onReceive(SmsReceiver.java:59) 01-19 02:32:33.409: ERROR/AndroidRuntime(31914): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637) 인데 뭐가 문제 일까요??