Broadcast Receiver – MainActivity is not an enclosing class

huangapple go评论87阅读模式
英文:

Broadcast Receiver - MainActivity is not an enclosing class

问题

我知道这个问题已经被问了很多次,但我找不到适合我的答案。
我只是在攻读学士学位的第三学期,所以知识还不是很多。

在完成初学者Java课程后,我目前正在学习我的第一个Android课程。

由于疫情,几乎没有导师的支持,而且课程内容也缺乏详细的解释。

我正在努力实现我的第一个广播接收器。它应该在插入电源线时显示一个Toast消息。

我尝试了一个动态接收器,所以我在我的MainActivity中注册了接收器,代码如下:

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {
[...]
private PowerConnectedReceiver mPowerConnectedReceiver;

    public void onResume() {

        super.onResume();

        IntentFilter powerFilter = new IntentFilter(Intent.ACTION_POWER_CONNECTED );

        mPowerConnectedReceiver = new PowerConnectedReceiver();

        getApplicationContext().registerReceiver(mPowerConnectedReceiver , powerFilter);

    }

    @Override

    protected void onPause() {

        getApplicationContext().unregisterReceiver(mPowerConnectedReceiver);

        super.onPause();

    }

广播接收器代码如下,这里出现了错误。它说Toast的上下文MainActivity不是一个封闭类。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;

public class PowerConnectedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG ).show();
    }
}

这是在脚本中教授的实现方式。

正如我提到的,我对编程还比较新,这可能是一些非常明显的东西,但我看不到它。

我唯一尝试过的是将其更改为MainAcivity.class,但没有任何作用。

感谢支持。

英文:

I know this was asked numerous times, but I couldn't find the right answer for me.
I'm only in my third semester of my bachelor degree, so not that much knowledge yet.

Currently having my first Android course after I finished my beginner Java course (emphasis on beginner).

Due to Corona there is next to no Tutor support and the script is lacking decent explanations.

Im struggling with implementing my first Broadcast receiver. It's supposed to make a toast when a power cord is plugged in.

I tried a dynamic receiver so I registered the Receiver in my MainActivity as follows:

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;


public class MainActivity extends AppCompatActivity {
[...]
private PowerConnectedReceiver mPowerConnectedReceiver;

    public void onResume() {

        super.onResume();

        IntentFilter powerFilter = new IntentFilter(Intent.ACTION_POWER_CONNECTED );

        mPowerConnectedReceiver = new PowerConnectedReceiver();

        getApplicationContext().registerReceiver(mPowerConnectedReceiver , powerFilter);

    }

    @Override

    protected void onPause() {

        getApplicationContext().unregisterReceiver(mPowerConnectedReceiver);

        super.onPause();

    }

With the Broadcast Receiver looking as follows and this is where the error pops up. It says for the Context of the Toast that MainActivity is not an enclosed class.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;

public class PowerConnectedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG ).show();
    }
}

This is the way of implementing that is taught in the script.

As I mentioned I am fairly new to programming and this might be something totally obvious, but I'm not seeing it.

The only thing I tried was changing it to MainAcivity.class, but that did nothing.
Thanks for support.

答案1

得分: 1

Update your toast message from:

Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG).show();

to

Toast.makeText(context, "POWER CONNECTED received", LENGTH_LONG).show();
英文:

Your code is correct, but in the toast message you have to use context of the receiver i.e the first parameter of the onReceive method.

Update your toast message from:

Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG ).show();

to

Toast.makeText(context, "POWER CONNECTED received", LENGTH_LONG ).show();

huangapple
  • 本文由 发表于 2020年7月31日 19:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定