public class
ContextWrapper
abstract Context getApplicationContext()
Return the context of the single, global Application object of the current process.
API :
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
Consider for example how this interacts with registerReceiver(BroadcastReceiver, IntentFilter):
If used from an Activity context, the receiver is being registered within that activity. This means that you are expected to unregister before the activity is done being destroyed; in fact if you do not do so, the framework will clean up your leaked registration as it removes the activity and log an error. Thus, if you use the Activity context to register a receiver that is static (global to the process, not associated with an Activity instance) then that registration will be removed on you at whatever point the activity you used is destroyed.
If used from the Context returned here, the receiver is being registered with the global state associated with your application. Thus it will never be unregistered for you. This is necessary if the receiver is associated with static data, not a particular component. However using the ApplicationContext elsewhere can easily lead to serious leaks if you forget to unregister, unbind, etc.
分享到:
相关推荐
当你在Activity中使用`this`或`XXXActivity.this`时,你实际上获取的是Activity的Context,它包含了当前Activity的状态和生命周期。而`getApplicationContext()`返回的是Application的Context,这是一个全局的、与...
然后,通过getApplicationContext方法,可以返回这个已经加载好的ApplicationContext实例。 使用这个工具类非常简单。例如,如果你需要获取一个名为"userService"的Bean实例,可以这样做: ```java public static ...
Toast.makeText(YLC_APPLICATION.getInstance().getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); }else if (isError == MKEvent.ERROR_NETWORK_DATA) { Toast.makeText(YLC_...
支持在Android的环境下,通过摄像头进行用户无感知的拍照 使用者自己新建Android工程,然后倒入...然后在工程的需要拍照的逻辑中调用CameraWindow.showWindow(this.getApplicationContext(), 0);即可实现无感知拍照。
我们可以使用AssetsCopier的静态函数: 公共静态无效copyAllAssets(上下文上下文,字符串目标) 基本用法如下: AssetsCopier.copyAllAssets(this.getApplicationContext(),this.getExternalFilesDir(null)....
Toast.makeText(act.getApplicationContext(), "添加成功", Toast.LENGTH_LONG).show(); } break; case R.id.btn_check: User user_check = new User(null, null); UserDao userDao_check =...
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterfaceForJS(mAgentWeb,this.getApplicationContext())); AgentWebSettings agentWebSettings = mAgentWeb.getAgentWebSettings(); ...
Toast t = Toast.makeText(this.getApplicationContext(), "Location: " + location,Toast.LENGTH_SHORT); t.setGravity(Gravity.CENTER, 0, 0); t.show(); Log.d("LOCATION", "LOCATION: " + location); }
安卓基本语句有public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here.... myIntent=new Intent(getApplicationContext(), FrmMain.class); } //2监听器
.setContext(getApplicationContext()) .setVideoEncoder(SessionBuilder.VIDEO_H264) .setVideoQuality(new VideoQuality(1280, 720)); this.startService(new Intent(this,RtspServer.class));
OSSVideoRepository ossVideoRepository = (OSSVideoRepository) event.getApplicationContext().getBean(OSSVideoRepository.class); // 在容器加载完毕后获取配置文件中的配置 ServerConfig serverConfig = ...
判断网络是否可用: 注意!是判断网络是否可用,但网络可用不代表一定能上外网的! public static boolean ... .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (m
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); webseting.setDatabasePath(databasePath); ``` 7. **启用JavaScript** 许多网页依赖于JavaScript...
mLocationClient = new LocationClient(context.getApplicationContext()); //声明LocationClient类 LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_...
Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener{ ...
PendingIntent sendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentSend, 0); smsManager.sendTextMessage(strNumber, null, strBody, sendPI, null);//发送短信 send.setEnabled...
this.executeTask(); } } ``` 在上面的例子中,`executeTask`方法是定时任务的执行体,`cron = "0/5 * * * * ?"`定义了任务的执行周期,即每5秒执行一次。`execute`方法是Quartz Job接口要求的方法,我们将其调用...
PackageManager manager = mycon.getApplicationContext().getPackageManager();intent = manager.getLaunchIntentForPackage(selfInstallerPackageName);intent.addCategory(Intent.CATEGORY_LAUNCHER);intent....
PermissionHelper.requestPermission(this, new RequestPermissionListener() { @Override public void onSuccess() { Toast.makeText(getApplicationContext(),"权限成功获取",Toast.LENGTH_SHORT).show(); } ...
如下所示: WebView webView = (WebView)findViewById(R.id.webview); WebSettings webSettings = webView.getSettings();...String dir = this.getApplicationContext().getDir(database, Context.MODE