2014-01-21 25 views
-1

這是一個短信接收器,但不工作!這裏是我的代碼。我不知道最新的問題! 我也安裝了gosms !!是否有任何問題?我有短信接收器,但沒有工作

我的manifest.xml

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.SEND_SMS"> 
    </uses-permission> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"> 
    </uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.sms_rec.MainActivity" 
      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=".Smsreciv" 
      > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" > 
       </action> 
      </intent-filter> 


      </receiver> 

    </application> 


    </manifest> 

我的主要活動:

package com.example.sms_rec; 

    import android.app.Activity; 
    import android.content.BroadcastReceiver; 
    enter code here 
    import android.os.Bundle; 


    public class MainActivity extends Activity 
{ 


    private static final BroadcastReceiver Smsreciv = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     }  
} 

--------------------------------- 

我收到的短信reciver: ------------------- ---------

package com.example.sms_rec; 
    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 abstract class Smsreciv 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(); 
     }       
    }  


    } 

回答

1

這是一個短信接收器,但不工作

未來,請通過提供具體症狀來解釋「不工作」的含義。

有什麼問題嗎?

有一點肯定是錯誤的,那就是Smsreciv的類定義上有abstract關鍵字。這將阻止Android創建此類的實例。您應該在LogCat中看到關於此的警告或錯誤。請刪除abstract,因爲你不需要它。

This sample project演示了一個可以監控SMS消息的應用程序。

+0

我刪除了抽象,但沒有吐司顯示! – user3185049