2013-10-28 5 views
1

전화가 SMS 메시지를받을 때 작은 응용 프로그램을 쓰고 있습니다. 발신자 전화 번호와 SMS 본문이 TextView에 표시됩니다. SMS BoardcastReceiverActivity이 있습니다.TextView에 SMS 메시지 표시

여기 내 SMS 수신기입니다.

다음
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class BroadcastNewSms extends Activity { 

    TextView SMSm; 
    String phoneNumber1; 
    String SMSBody1; 

    public void getSmsDetails(String phoneNumber, String SMSBody) { 
     phoneNumber1 = phoneNumber; 
     SMSBody1 = SMSBody; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     SMSm = (TextView) findViewById(R.id.etSmsBody); 

     SMSm.setText("Phone Number: " + phoneNumber1 + " " + "SMS: " + 
     SMSBody1); 

    } 

} 

내 매니페스트는 다음과 같습니다 :

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="kobi.avshalom.recivesms.BroadcastNewSms" 
      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="kobi.avshalom.recivesms.IncomingSms" > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

    <uses-permission android:name="android.permission.RECEIVE_SMS" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.READ_SMS" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.SEND_SMS" > 
    </uses-permission> 
+2

그러나 문제/오류가 무엇입니까? –

+0

@Prince 디버그하십시오. 질문이 없습니다. –

+0

@SherifelKhatib 무엇? OP는 문제를 지정하지 않으므로 이에 대해 묻습니다. –

답변

2

getSmsDetails 정적을 선언 IncomingSms에서
public static void getSmsDetails(String phoneNumber, String SMSBody)

BroadcastNewSms.getSmsDetails(senderNum, message);//ourSMS.getSmsDetails(senderNum, message);
을 대체 나는 또한 당신이 setSmsDetails에 getSmsDetails를 변경 제안

+0

감사합니다 근무 : D –

+0

안녕하세요. 이 [link] (http://stackoverflow.com/help/someone-answers)에 따라 stackoverflow에서 'say say'하는 법을 배우십시오. – ramaral

0

당신이 PopupWindow를 사용하여 시도 가지고 여기

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.Toast; 

public class IncomingSms extends BroadcastReceiver { 

    // Get the object of SmsManager 
    final SmsManager sms = SmsManager.getDefault(); 
    BroadcastNewSms ourSMS; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     final Bundle bundle = intent.getExtras(); 

     try { 
      if (bundle != null) { 
       final Object[] pdusObj = (Object[]) bundle.get("pdus"); 

       for (int i = 0; i < pdusObj.length; i++) { 
        SmsMessage currentMessage = SmsMessage 
          .createFromPdu((byte[]) pdusObj[i]); 
        String phoneNumber = currentMessage 
          .getDisplayOriginatingAddress(); 

        String senderNum = phoneNumber; 
        String message = currentMessage.getDisplayMessageBody(); 

        Log.i("SmsReciver", "senderNum: " + senderNum 
          + ", message: " + message); 
        //ourSMS.getSmsDetails(senderNum, message); 
        // Show SMS notification 
        int duration = Toast.LENGTH_LONG; 
        Toast toast = Toast.makeText(context, "senderNum: " 
          + senderNum + ", message: " + message, duration); 
        toast.show(); 

       } // end of for loop 
      } // bundle 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("SmsReciver", "Exception smsReciver" + e); 
     } 
    } 
} 

내 활동인가? (당신이

// Close the popup window 
popupWindow.dismiss(); 

당신은 타이머에 넣고 수 호출해야합니다

private PopupWindow popupWindow; 

....

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext() 
       .getSystemService(LAYOUT_INFLATER_SERVICE); 
     View popupView = layoutInflater.inflate(R.layout.custom_popup_layout, 
       (ViewGroup) findViewById(R.id.popup_root_element), true); 
popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      // Display the popup window 
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 

팝업 창을 닫으려면 : 이 같은 팝업을 표시 할 수 있습니다 자동 닫기를 원할 경우) 또는 팝업 창 레이아웃의 버튼에 이벤트를 첨부 할 수 있습니다.

당신이 사용할 수있는 팝업 윈도우의 레이아웃 내부 요소에 액세스하려면 :

popupWindow.getContentView().findViewById(R.id.the_id_of_the_widget); 

희망이 당신이 찾고 있던 것입니다. BroadcastNewSms 활동에

+0

안녕하세요, 감사합니다.하지만 TextView에 메시지를 넣어야합니다. –

+0

PopupWidnow의 레이아웃에는 TextView 위젯 만있을 수 있습니다. –