【发布时间】: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