简体   繁体   English

不会调用BroadcastReceiver的onReceive方法

[英]onReceive method of BroadcastReceiver does not get called

I am using AlarmManager to create an alarm by playing a sound. 我正在使用AlarmManager通过播放声音来创建警报。 To do this, I first create a PendingIntent , for which I have to create a class called AlarmReceiver , which extends BroadcastReceiver . 为此,我首先创建一个PendingIntent ,为此必须创建一个名为AlarmReceiver的类,该类扩展了BroadcastReceiver In this new class, I override the onReceive method, in which I also start the sound. 在这个新类中,我重写了onReceive方法,在该方法中,我还开始播放声音。 However, from what I've tested, the onReceive method is not even called from the MainActivity. 但是,从我的测试来看,MainActivity甚至没有调用onReceive方法。

After some research, I found out that I should declare the receiver in the manifest file. 经过研究,我发现应该在清单文件中声明接收方。 Thus, I declare it, but it doesn't recognize the name of the class, AlarmReceiver , it shows it in red. 因此,我声明了它,但它无法识别类的名称AlarmReceiver ,它以红色显示。 I don't fully understand how to properly declare in the manifest file. 我不完全了解如何在清单文件中正确声明。 I know there are other similar SO questions and I've checked them all, but I am still not able to get it work. 我知道还有其他类似的问题,我已经检查了所有问题,但仍然无法解决。

The code for the MainActivity is: MainActivity的代码是:

package com.example.alarmsound;
public class MainActivity extends AppCompatActivity {

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final MediaPlayer mp = MediaPlayer.create(context, R.raw.music);
        Log.d("Music", "It went here.");
        mp.start();

        Button stop = (Button) findViewById(R.id.stopAlarm);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });
    }
}

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

    Calendar t = Calendar.getInstance();
    t.add(Calendar.SECOND, 5);

    Context context = this;
    AlarmManager alarmMgr;
    alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), pendingIntent);
}
}

And the declaration in Manifest is: 清单中的声明是:

<receiver android:name="com.example.alarmsound.AlarmReceiver">
        <intent-filter>
            <action android:name="com.example.alarmsound.MainActivity" />
        </intent-filter>
</receiver>

I could also be doing something wrong in the MainActivity, even though I think I'm doing everything right there. 即使我认为自己正在做的所有事情,我在MainActivity中也可能做错了什么。 I would appreciate any help. 我将不胜感激任何帮助。

Change the first line of the receiver declaration to: <receiver android:name="com.example.alarmsound.MainActivity$AlarmReceiver"> . 将接收者声明的第一行更改为: <receiver android:name="com.example.alarmsound.MainActivity$AlarmReceiver"> That should let Android detect your class through the manifest. 那应该让Android通过清单检测到您的类。

The $ symbol is used to reference inner classes in the Android Manifest. $符号用于引用Android Manifest中的内部类。

Okay. 好的。 So as you mentioned the the AlarmReceiver class cannot be recognized in the AndroidManifest.xml . 因此,正如您提到的, AndroidManifest.xml无法识别AlarmReceiver类。 So I tried it out in Android Studio and it seems the format should be something like this: 因此,我在Android Studio中进行了尝试,看起来格式应该是这样的:

<receiver android:name=".MainActivity$AlarmReceiver">
            <intent-filter>
                <action android:name="com.example.alarmsound.MainActivity" />
            </intent-filter>
        </receiver>

Tried running the code but it returns an error where the app crashes. 尝试运行代码,但在应用崩溃时返回错误。 Anyways, I think that's a different concern now. 无论如何,我认为现在是另外一个问题。 Good luck. 祝好运。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM