안녕하세요.. 아직 18살 여고생 개발자입니다...ㅠㅠ 간단한 예제를 작성하려 하는데 꼬여서 되지 않습니다 ㅠㅠ 도와주세요
버튼을 클릭하면 다이얼로그가 뜨고 그 안에 내용을 다른 클래스의 내용으로 만들고 싶습니다. 어떻게 해야하나요...
package com.example.sms;
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.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SMS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
Button sendBtn = (Button) findViewById(R.id.sendSmsBtn);
sendBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText addrTxt = (EditText) SMS.this.findViewById(R.id.addrEditText);
EditText msgTxt = (EditText) SMS.this.findViewById(R.id.msgEditText);
try {
sendSMS(addrTxt.getText().toString(), msgTxt.getText().toString());
Toast.makeText(SMS.this, "SMS 발송 완료", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(SMS.this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
protected void sendSmsMessage(String address, String message) throws Exception {
// TODO Auto-generated method stub
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(address, null, message, null, null);
}
private void sendSMS(String phoneNumber, String message) {
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);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", 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));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
------------------------------------------------------------------------------------
저 ButtonTest의 다이얼로그 안에 SMS.class의 내용을 넣고 그대로 구동시키려고 합니다. 어떻게 해야하나요 ㅠㅠㅠ
뷰와 액티비티의 개념을 좀 더 학습하시고, 명확히 구분하시는게 좋겠습니다.
다이얼로그는 뷰보다는 액티비티에 가깝다고 생각하셔야 하구요. 꼭 기본 다이얼로그를 사용하시려면 SMS 클래스를 activity가 아닌 view를 상속받으셔서 만드시고 SMS 클래스의 리스너를 ButtonTest activity에서 구현해줘야 하는데 난이도가 만만치 않을 겁니다.
좀 쉬운 방법으로는 ButtonTest activity에서 버튼 클릭시에 아래 SMS 클래스를 Intent로 호출하는 겁니다. manifest에서 SMS클래스의 theme설정을 dialog 방식으로 하고activity_sms.xml 을 수정하셔서 다이얼 로그처럼 보여질 수 있습니다. sdk 에 포함된 sample source 중 ApiDemos 에서 CustomDialogActivity 를 참고해 보세요.