2013-10-28 80 views
1

我在写一个小应用程序,当手机收到短信时。它会在TextView中显示发件人电话号码和短信正文。我有一个短信BoardcastReceiver和一个Activity在TextView中显示短信

这是我的SMS侦听器。

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); 
     } 
    } 
} 

这里是我的活动:

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

在BroadcastNewSms活动声明getSmsDetails静:
public static void getSmsDetails(String phoneNumber, String SMSBody)

在IncomingSms更换//ourSMS.getSmsDetails(senderNum, message);
BroadcastNewSms.getSmsDetails(senderNum, message);

我也建议你改变getSmsDetails到setSmsDetails

+0

谢谢工作:D –

+0

欢迎来到SO。按照这个[链接](http://stackoverflow.com/help/someone-answers)来学习如何在stackoverflow中说'谢谢'。 – ramaral

0

您是否尝试过使用PopupWindow? 您可以显示像这样的弹出:

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); 

要关闭弹出的窗口中,你必须调用

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

你可以把这个计时器(如果你想自动解雇),或者你可以将事件附加到弹出窗口布局中的按钮上。

要访问的元素弹出窗口的布局里,你可以使用:

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

希望这是你所期待的。

+0

嗨,谢谢,但我需要把消息放在一个TextView中,没有弹出。 –

+0

PopupWidnow的布局只能有一个TextView小部件。 –