2016-09-20 82 views
0

我有一些問題與使用BroadCastReceiver.Here而來的AndroidManifest.xml中無法實例接收器沒有零參數的構造函數

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

    <uses-permission android:name="android.permission.RECEIVE_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> 
     <activity android:name=".NotificationActivity"></activity> 
     <receiver android:name=".NotificationActivity$SMSReceiver"> 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

和我的代碼部分

公共類NotificationActivity擴展AppCompatActivity {

public TextView textViewFrom, textViewContent; 
public IntentFilter intentFilter; 
public SMSReceiver smsReceiver; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(smsReceiver); 
} 

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

    textViewContent = (TextView)findViewById(R.id.textViewContent); 
    textViewFrom = (TextView)findViewById(R.id.textViewFrom); 

    intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); 
    smsReceiver = new SMSReceiver(); 

    registerReceiver(smsReceiver,intentFilter); 
} 

public class SMSReceiver extends BroadcastReceiver{ 

    public SMSReceiver(){ 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     SmsMessage[] smsMessages = new SmsMessage[pdus.length]; 
     for (int i = 0; i < smsMessages.length; i++) 
      smsMessages[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
     String from = smsMessages[0].getOriginatingAddress(); 
     StringBuilder content = new StringBuilder(""); 
     for (SmsMessage element: smsMessages) 
      content.append(element.getMessageBody()); 
     textViewFrom.setText(from); 
     textViewContent.setText(content.toString()); 
    } 
} 

} 任何人都可以給我一些幫助,告訴我爲什麼發生了?非常感謝!

回答

2

您的SMSReceiver是一個非靜態實例類。這意味着它只能在持有類NotificationActivity的環境中構建。這很好,但是您無法在清單中註冊它,因爲系統需要構建一個NotificationActivity以便實例化您的接收器來處理廣播。

順便說一句,要麼將接收器註冊到清單中,要麼將其註冊到您的活動中,但不能同時在兩者中註冊。

+0

感謝您的幫助和解釋!很好的幫助! Wooo! –

3

SMSReceiver是一個非靜態的內部類,如果要將接收器保留在清單中,請將其設置爲靜態或將其移出NotificationActivity

當您添加BroadcastReciever到清單,您聲明,它始終是註冊(所以你不需要註冊,並在您的活動註銷)

如果你想保持它在清單,Activity和BroadcastReceiver的生命週期不同,因此按照您目前的做法訪問textViewFrom會很危險。

如果您希望只在活動處於活動狀態時觸發接收器,請將其從清單中刪除

+0

Wooo!它確實有用!我應該更深入地去更好地理解這門課。再次感謝〜 –

相關問題