2013-10-28 119 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小部件。 –