2012-12-15 35 views
4

我嘗試在我的應用程序,接收短信,我相信這個問題是在我的AndroidManifest.xml文件接收短信的Android的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.amin_amini.smstestmobi" 
     android:versionCode="1" 
     android:versionName="1.0.0"> 
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
    <uses-permission android:name="android.permission.SEND_SMS"/> 
    <uses-permission android:name="android.permission.READ_SMS"/> 
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 
     <activity android:name=".SMS" 
        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=".SmsReceiver" 
        android:enabled="true" 
        android:exported="true" 
        android:permission="android.permission.BROADCAST_SMS"> 
      <intent-filter android:priority="1000"> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

我SmsReceive.java是這樣

package com.amin_amini.smstestmobi; 

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

public class SmsReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = "";    
    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     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]);     
      str += "SMS from " + msgs[i].getOriginatingAddress();      
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n"; 
     } 
     //---display the new SMS message--- 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
    }     
    Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show(); 
    } 
} 

我敢肯定,我的問題是在manifest.xml中,因爲

Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show(); 

不顯示任何東西。
我該怎麼辦?

編輯: 作爲我們的朋友波紋管說,我用

Log.i("Hey","Hey"); 

在我的功能,但沒有將logcat的 顯示所以我defenetly肯定,我的問題是在我的AndroidManifest.xml 注意:我採用了android 4.0.3對HTC感覺

+0

不要使用Toast調試廣播接收器。使用日誌消息或斷點。我甚至不確定你是否可以從其中一個烤麪包。 –

+0

是的,我敢肯定,'上下文'是應用程序上下文,而不是活動上下文。也將無法顯示對話框。使用Log.i(*)。 – edthethird

+0

我正在做類似的事情! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42

回答

3

改變你的接收器的代碼,它會幫助你,如果還是它不是那麼工作可能是你已經安裝了另一個SMS的應用程序,這將具有更高的優先級,並會abord廣播服務。

 <receiver android:name=".SmsReceiver"> 
     <intent-filter android:priority="1000"> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
      <action android:name="PACKAGE_NAME.android.action.broadcast"/> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
     </intent-filter> 
    </receiver> 
+0

感謝您的幫助工作,但我試過了,再不會在真實設備上工作,我沒有anothe短信應用程序也測試它在5 Android設備至極它沒有工作我已經把Log.i(「收到」,「收到」);在onReceive的第一行,但沒有記錄LogCat :(( – Amin

+0

@ user1905965 ,好吧,我有這個代碼,你可以從我的 –

+0

拿什麼代碼?我應該如何把它從U? – Amin

0

是你的代碼工作在這裏很好。

這是我androidmanifest

<receiver android:name=".SmsReceiver" > 
      <intent-filter > 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 

,我已經添加了這些權限

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

或下載本code ..我取得了這一點。它會在對話框中收到短信和節目。

+0

感謝幫助,但我previosly添加這些權限,還我下載了你的代碼的Dropbox,但它不的顯示任何東西,我只看到我在我的消息應用程序:( – Amin

+0

短信嘗試使用日誌,而不是烤麪包。並提出在不同的地方,一些日誌在廣播接收器。它會幫助你進行調試。嘗試。 – Zohaib

+0

嘗試在Android模擬器中運行它。檢查其工作或沒有 – Zohaib