传递到BroadcastReceiver的onReceive()中的上下文是什么? [英] What is the Context passed into onReceive() of a BroadcastReceiver?

查看:224
本文介绍了传递到BroadcastReceiver的onReceive()中的上下文是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BroadcastReciveronReceive方法中传递的上下文是什么:

What is the context that is passed int the onReceive method of a BroadcastReciver:

public void onReceive (Context context, Intent intent)

根据

运行接收方的上下文.

The Context in which the receiver is running.

推荐答案

一些研究得出了以下结果...

A little research gives below result...

用于静态接收器

For static receiver

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("PANKAJ", "Context class " + context.getClass().getName());
        Log.e("PANKAJ", "Application Context class "
                + context.getApplicationContext().getClass().getName());
    }
}

我低于日志

08-05 06:51:33.448: E/PANKAJ(2510): Context class android.app.ReceiverRestrictedContext
08-05 06:51:33.448: E/PANKAJ(2510): Application Context class android.app.Application

对于像这样的bynamic接收者(已注册到Activity MainActivity中)

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    public void onReceive(android.content.Context context, Intent intent) {
        Log.e("PANKAJ", "Context class " + context.getClass().getName());
        Log.e("PANKAJ", "Activity Context class "
            + MainActivity.this.getClass().getName());
        Log.e("PANKAJ", "Application Context class "
            + context.getApplicationContext().getClass().getName());
    }
};

我低于日志

08-05 06:53:33.048: E/PANKAJ(2642): Context class com.example.testapp.MainActivity
08-05 06:53:33.048: E/PANKAJ(2642): Activity Context class com.example.testapp.MainActivity
08-05 06:53:33.048: E/PANKAJ(2642): Application Context class android.app.Application

因此,当实现文档中给出的声明

So when it make true the statement given into documentation that The Context in which the receiver is running.

这篇关于传递到BroadcastReceiver的onReceive()中的上下文是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆