Android Studio:无法在AndroidManifest中将类添加到<receiver>

时间:2016-09-02 22:01:56

标签: java android android-studio broadcastreceiver receiver

我已完成所有内容,正如教程所示,但我仍然遇到一个错误...我已经创建了新的活动SmsReceiver.java,但是当我尝试添加此内容时,一切正常但是在清单中

<receiver android:name=".SmsReceiver">  // <-- this is problem
        <intent-filter android:priority="1000">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>

它不想接受该类.SmsReceiver ....

我已经尝试了一切,请帮忙。当我将鼠标悬停在它上面时说&#34; &#39;类&#39;或者&#39;界面&#39;预期&#34;。

Here is package view and manifest full code

这是SmsReceiver.java代码

package com.dreamdev.matko.smsarduino;

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) {
    Bundle bundle = intent.getExtras();

    if (bundle != null) {

        Object[] pdus = (Object[]) bundle.get("pdu");

        for (int i = 0; i < pdus.length; i++) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[i]);

            String from = sms.getOriginatingAddress();

            String msg = sms.getDisplayMessageBody();

            Toast.makeText(context, "Prišla správa", Toast.LENGTH_LONG).show();
        }

    }

  }

}

2 个答案:

答案 0 :(得分:0)

你说:

  

我创建了新活动SmsReceiver.java

如果您创建了Activity,则无法在清单中将其声明为BroadcastReceiver。如果您想要BroadcastReceiver,那么您的课程需要extend BroadcastReceiver,然后将其添加到清单中,如下所示:

<receiver android:name=".SmsReceiver">

答案 1 :(得分:0)

请将类扩展为.java文件中的BroadcastReceiver。如下所示

public class SmsReceiver extends BroadcastReceiver {
    ......
}