【问题标题】:Android context choiceAndroid 上下文选择
【发布时间】:2014-04-08 20:34:14
【问题描述】:

我不明白我应该在哪个上下文中使用

(来自 onRecieve 方法的 mApplicationContext 或上下文参数)。拜托,你能给我一些解释我应该使用什么上下文参数以及为什么(我读到了内存泄漏,这种方法的文档)

final PendingIntent pendingIntent = PendingIntent.getActivity(**mApplicationContext**, <smth>);

Notification.Builder notificationBuilder = new Notification.Builder(                    **mApplicationContext**).<smth>;

NotificationManager notificationManager = (NotificationManager) **mApplicationContext**.getSystemService(Context.NOTIFICATION_SERVICE);

// Constructor
    public DownloaderTask(MainActivity parentActivity) {
        super();

        mParentActivity = parentActivity;
        mApplicationContext = parentActivity.getApplicationContext();

    }

mApplicationContext.sendOrderedBroadcast(new Intent(
                MainActivity.DATA_REFRESHED_ACTION), null,
                new BroadcastReceiver() {

                    final String failMsg = "Download has failed. Please retry Later.";
                    final String successMsg = "Download completed successfully.";

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

                        if (getResultCode() != Activity.RESULT_OK) {

                            final PendingIntent pendingIntent = PendingIntent
                                    .getActivity(mApplicationContext, <smth>);

                            RemoteViews mContentView = new RemoteViews(
                                    mApplicationContext.getPackageName(),
                                    R.layout.custom_notification);

                            if(success){
                            mContentView.setTextViewText(R.id.text,
                                    successMsg);
                            } else {
                                mContentView.setTextViewText(R.id.text, failMsg);
                            }

                            Notification.Builder notificationBuilder = new Notification.Builder(
                                    mApplicationContext).<smth>;
                            notificationBuilder.setContentIntent(pendingIntent);

                            NotificationManager notificationManager = (NotificationManager) mApplicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
                            notificationManager.notify(MY_NOTIFICATION_ID, notificationBuilder.build());
                            log("Notification Area Notification sent");
                        }
                    }
                }, null, 0, null, null);

    }

【问题讨论】:

    标签: java android android-context


    【解决方案1】:

    一般来说,如果你在一个活动中,'this' 是你的上下文(记得 import android.content.Context; ),你也可以将上下文传递给你的片段。

    在其他情况下,getApplicationContext 是个好主意,例如在服务启动警报等中,但您最好将您正在使用的活动视为上下文,然后您会了解其他情况例外情况。

    保持这种模式,当你认为你可以访问上下文但你意识到你没有时,它总是会提供信息;考虑一下您到底想对那个对象做什么,以及它“属于”程序的哪个部分。

    所以,推断是什么构造了这个对象,另一个活动做了 DownloaderTask(this) 或 DownloaderTask(this.context) 或 DownloaderTask(getApplicationContext);我会采用第一种方式,并在此处使用 public void DownloaderTask(Context context) =]

    形成构造函数

    【讨论】:

      【解决方案2】:

      始终使用您拥有的最具体的上下文。在活动中使用“this”,以及通过方法提供给您的上下文。

      将应用程序上下文留给您无法访问活动上下文的情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-12
        • 2021-03-18
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 2012-05-25
        • 2010-11-20
        相关资源
        最近更新 更多