2017-05-18 3 views
-1

SMSメッセージを聞く簡単なアンドロイドプログラムを作成しようとしています。メッセージを受信すると、それをトーストで表示します。Androidで一度受信したSMSメッセージを表示する

public class SmsListener extends BroadcastReceiver { 

    private String msgBody; 
    private SharedPreferences preferences; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 


     if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ 

      Toast.makeText(context,"message received:",Toast.LENGTH_SHORT).show(); 

      Bundle bundle = intent.getExtras();   //---get the SMS message passed in--- 
      SmsMessage[] msgs = null; 
      String msg_from; 
      if (bundle != null){ 
       //---retrieve the SMS message received--- 
       try{ 
        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]); 
         msg_from = msgs[i].getOriginatingAddress(); 
         msgBody = msgs[i].getMessageBody(); 
        } 

        Toast.makeText(context,"message is:"+msgBody,Toast.LENGTH_SHORT).show(); 
       }catch(Exception e){ 
        Log.d("Exception caught",e.getMessage()); 
       } 
      } 
     } 
    } 
} 

と以下の私の活動です:

public class MainActivity extends AppCompatActivity{ 

    SmsListener smsListener; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     smsListener= new SmsListener(); 
     IntentFilter intentFilter=new IntentFilter(); 
     intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
     registerReceiver(smsListener, intentFilter); 


    } 


    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 

     // Unregister the SMS receiver 
     unregisterReceiver(smsListener); 
    } 


} 

は、驚くべきことに、私は何も出力時を得ていないのです以下

は以下
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.zaidalmahmoud.expenseless"> 

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

    <application 

     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


    </application> 

</manifest> 

は私のSMSリスナーである私のマニフェストファイルでありますSMSを受信するとすべて(トーストメッセージなし)。助けてもらえますか?ありがとうございました。

+0

は、あなたはあなたがアンドロイド6+上にある場合、実行時のアクセス許可を要求する必要が知っていますか? – Saurabh

+0

いいえ、既に含まれている3つのアクセス許可以外のものですか? –

+0

このガイドに従ってください:https://developer.android.com/training/permissions/requesting.html – Saurabh

答えて

-1

受信者は、何も聞いていないので、マニフェストファイルでは宣言していません。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.zaidalmahmoud.expenseless"> 

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

<application 

    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

<receiver android:name=".SmsListener"/> 

</application> 

+0

私の予想通り、あなたのソリューションは動作しません。実際には、私はすでにアクティビティで受信者を宣言しているので、マニフェストファイルに再度宣言する必要はありません。 –

関連する問題