相关疑难解决方法(0)

什么是Android上的"上下文"?

在Android编程中,究竟是什么Context类以及它用于什么?

我在开发者网站上看过它,但我无法理解它.

android android-context

1872
推荐指数
26
解决办法
65万
查看次数

getApplication()与getApplicationContext()

我找不到令人满意的答案,所以我们在这里:有什么处理Activity/Service.getApplication()Context.getApplicationContext()

在我们的应用程序中,都返回相同的对象.ActivityTestCase然而,在一个getApplication()模拟应用程序将使用模拟getApplicationContext返回,但仍将返回一个不同的上下文实例(一个由Android注入).那是一个错误吗?这是故意的吗?

我甚至不了解首先的区别.测试套件外是否存在两个呼叫可能带有不同对象的情况?何时以及为何?此外,为什么getApplication定义上ActivityService,但不是Context?不应该随处可用的有效应用程序实例吗?

android android-service android-context android-activity

407
推荐指数
3
解决办法
30万
查看次数

获取上下文的各种方法之间有什么区别?

在我看过的各种Android代码中:

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activity extends Context
       mContext = getApplicationContext();
       mContext = getBaseContext();
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,我找不到任何合适的解释,哪些更好,以及在什么情况下应该使用.

关于这方面的文件的指示,以及关于如果选择了错误的可能会破坏的指导,将不胜感激.

android android-context

387
推荐指数
4
解决办法
9万
查看次数

如何检测我是处于发布模式还是调试模式?

如何在我的代码中检测到我处于发布模式或调试模式?

android android-build-type

336
推荐指数
8
解决办法
11万
查看次数

差异以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this

我是新来的Android和我想明白之间的差别getApplication(),getApplicationContext()getBaseContext(),getContext()以及someClass.this特别是当使用这些方法在下面的代码行:

当我发起祝酒时,这些和我使用它们之间有什么区别?

Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

与意图相同:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
Run Code Online (Sandbox Code Playgroud)

android this toast android-intent android-context

116
推荐指数
3
解决办法
5万
查看次数

Android:如何安全地取消绑定服务

我有一个绑定到应用程序上下文的服务,如下所示:

getApplicationContext().bindService(
                    new Intent(this, ServiceUI.class),
                    serviceConnection,
                    Context.BIND_AUTO_CREATE
            );

protected void onDestroy() {
            super.onDestroy();                  
            getApplicationContext().unbindService(serviceConnection);
        }
Run Code Online (Sandbox Code Playgroud)

出于某种原因,有时只有应用程序上下文没有正确绑定(我无法修复该部分),但是在onDestroy()中我做了unbindservice哪个会抛出错误

java.lang.IllegalArgumentException: Service not registered: tools.cdevice.Devices$mainServiceConnection.
Run Code Online (Sandbox Code Playgroud)

我的问题是:unbindservice在解除绑定之前,有没有办法安全地打电话或检查它是否已绑定到服务?

提前致谢.

android

41
推荐指数
4
解决办法
6万
查看次数

基本上下文,应用程序上下文和活动上下文之间

getBaseContext(),getApplicationContext()和ActivityName.this获取的活动上下文之间的实际区别是什么?

android

20
推荐指数
0
解决办法
2万
查看次数

在主发射器强制停止后未收到Android AppWidget的按钮单击事件

我有一个应用程序小部件,可以onUpdate()在我的提供程序中接收click事件.
但是,当我尝试强制关闭主启动器时,单击事件将丢失.
我甚至把断点在所有的onEnabled(),onReceived()...等:连接似乎丢失.

因此,我如何"重新连接"按钮事件?

WidgetProvider扩展AppWidgetProvider:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate()");
        Log.d(TAG, "isLoading: " + CONSTANT.isLoading);
        // update each of the widgets with the remote adapter from updateService
        // Get all ids
        ComponentName thisWidget = new ComponentName(context, ScoreWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent); …
Run Code Online (Sandbox Code Playgroud)

java android android-widget

12
推荐指数
1
解决办法
412
查看次数

什么是getApplicationContext()方法?

我正忙着学习android,我遇到了这个getApplicationContext方法.我真的不明白它的用途和目的是什么.

methods android

9
推荐指数
1
解决办法
3万
查看次数

Android:getContext().getContentResolver()有时会获得NullPointerException

我想问为什么我们得到这个注释:

方法调用getContext.getContentResolver()可能会产生NullPointerException

为什么它在那里而不是在程序碎片/活动的其他部分?这种方法已在Google制作的教程中使用 - 这里是ContentProvider代码的链接https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/java/com/example/android /sunshine/app/data/WeatherProvider.java即使您只使用空白活动创建一个应用程序,并将该方法放在新创建的ContentProvider中,它就在那里.

我们应该使用getContext().getContentResolver().notifyChange(uri, null);外部ContentProvider获取uri传递,然后更新/插入/删除完成notifyChange?或者我们可以以某种方式解决它?

java android nullpointerexception android-contentresolver

9
推荐指数
2
解决办法
9851
查看次数