我有一些問題與使用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());
}
}
} 任何人都可以給我一些幫助,告訴我爲什麼發生了?非常感謝!
感謝您的幫助和解釋!很好的幫助! Wooo! –