小编Muh*_*lib的帖子

xxhdpi android的比例因子?

根据http://developer.android.com/training/multiscreen/screendensities.html

提到了以下比例因子

xhdpi:2.0 hdpi:1.5 mdpi:1.0(基线)ldpi:0.75

我想知道xxhdpi的比例因子是什么?

android android-screen-support

75
推荐指数
2
解决办法
5万
查看次数

android中的WeakReference/AsyncTask模式

我对android中这种简单的常见情况有疑问.

我们有一个主要的活动,我们调用AsyncTask以及mainactivity的引用,以便AsyncTask可以更新MainActivity上的视图.

我将把事件分解为几步

  • MainActivity创建一个AyncTask,将其引用传递给它.
  • AysncTask,启动它的工作,例如下载十个文件
  • 用户更改了设备的方向.这会在AsyncTask中生成一个孤立指针
  • 当AsyncTask完成并尝试访问活动以更新状态时,由于空指针,它会崩溃.

上面的解决方案是按照"Pro Android 4"一书的建议在AsyncTask中保留WeakReference.

WeakReference<Activity> weakActivity;

in method onPostExecute

Activity activity = weakActivity.get();
if (activity != null) {
   // do your stuff with activity here
}
Run Code Online (Sandbox Code Playgroud)

这是如何解决这种情况的?

我的问题是,如果我的asynctask正在下载10个文件,并且在完成5后重新启动活动(因为方向改变),那么我的FileDownloadingTask会再次被调用吗?

最初调用的先前AsyncTask会发生什么?

谢谢,我为问题的长度道歉.

java android weak-references android-asynctask

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

WCF SOAP服务不能将JSON作为输出返回吗?

历史


我基本上是一个前端(android)开发人员,从来没有必要创建一个Web服务而是我在消费端.现在这个WCF业务势不可挡,我认为学习曲线非常复杂.

任务


我需要创建一个简单的SOAP服务,hello world暂时需要输入XML,但是返回JSON.

我不知道为什么我们在互联网上没有类似的问题,有点让我想知道它是否根本不可能?

这就是我到目前为止所拥有的.

目前的进展


我的合同

[ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        String GetMessage(String name);
    }
Run Code Online (Sandbox Code Playgroud)

我的合同Impl

public string GetMessage(string name)
        {
            return "Hello World from " + name + "!";
        }
Run Code Online (Sandbox Code Playgroud)

我的服务配置(在Web.Config中.我已经在asp.net网站上托管了这项服务)

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="MyWcfServices.HelloWorldService"          behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="wsHttpBinding"       behaviorConfiguration="WebBehavior"     contract="MyWcfServices.IHelloWorldService"/>
        <endpoint contract="IMetadataExchange"            binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

问题


  1. SOAP是否与XML密切相关?这样你就不能发送JSON作为输出?
  2. 将JSON作为输出发送是否真的需要转到REST范例?
  3. 我是否每次都获得XML,因为我使用带有HTTP头的WCFStorm fiddles并默认发送XML mime类型? …

c# rest wcf soap web-services

8
推荐指数
1
解决办法
7557
查看次数

用room(rxjava)执行删除

在室内,@Delete注释不会发出任何东西.这就是它的dao样子

@Dao
public interface UserDao {
    @Delete
    void deleteUser(User user);
    //We can't use Maybe or Single or anything here

}
Run Code Online (Sandbox Code Playgroud)

这使得在做类似事情时出现问题

userRepository.deleteUser().subscribeOn因为我们没有排放dao.我使用以下代码在后台线程上调用deleteUser.

Observable.just(appDatabase).
            subscribeOn(SchedulerProvider.getInstance().computation()).

            subscribe(db -> {
                userRepository.logoutUser(loggedUser.getLoggedInUser());
                loggedUser.setLoggedInUser(null);


            }, this::handleError);
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,在订阅方法中,我现在需要访问Android UI以显示宣布成功删除的Toast.当然,我得到这个例外(因为链中缺少observeOn)

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Run Code Online (Sandbox Code Playgroud)

但是,当我把observeOn这样的

Observable.just(appDatabase).
        subscribeOn(SchedulerProvider.getInstance().computation()).
        observeOn(SchedulerProvider.getInstance().ui()).
        subscribe(db -> {
            userRepository.logoutUser(loggedUser.getLoggedInUser());
            loggedUser.setLoggedInUser(null);

            Message message = new Message(R.string.user_logged_out_msg);
            message.setMessageType(Message.MessageType.SUCCESS_MESSAGE);
            view.showMessages(Arrays.asList(message)); //this leads to a taost

        }, this::handleError);
Run Code Online (Sandbox Code Playgroud)

我奇怪地得到这个例外:

cannot access database …
Run Code Online (Sandbox Code Playgroud)

android rx-android rx-java2 android-room

8
推荐指数
2
解决办法
3972
查看次数

Google位置服务与Android位置服务

我们知道我们有两个替代方案可以让我们的应用程序位置知道我们使用Google的位置服务API(Google Play服务的一部分),或者我们使用Androids Location API.

我在这里遇到的问题是,使用Google的服务,我们必须使用com.google.android.gms.location.LocationListener接口为我们提供位置更新.然而,与android.location.LocationListener不同,谷歌的版本并没有'为我们提供位置提供者的状态(gps或互联网).

我们在Androids Location Listener中有以下方法

abstract void   onLocationChanged(Location location)
Called when the location has changed.
abstract void   onProviderDisabled(String provider)
Called when the provider is disabled by the user.
abstract void   onProviderEnabled(String provider)
Called when the provider is enabled by the user.
abstract void   onStatusChanged(String provider, int status, Bundle extras)
Called when the provider status changes
Run Code Online (Sandbox Code Playgroud)

.

但是在谷歌我们只有一个

abstract void   onLocationChanged(Location location)
Run Code Online (Sandbox Code Playgroud)

如果我使用谷歌服务,我如何识别提供商的变化?.例如,在应用程序使用中,用户决定关闭GPS,我想在此事件上显示祝酒词.

我在黑暗中如何实现这一点.

android android-location

7
推荐指数
1
解决办法
2667
查看次数

选择器可在 MaterialButton android 上绘制

我一直在使用新的 MaterialButton 类。当用户单击按钮时,我希望按钮上有不同的颜色。

从一开始我就一直为此目的使用选择器可绘制对象,但是它似乎不适用于 MaterialButton。

<com.google.android.material.button.MaterialButton android:layout_width="0dp"
                                                       android:text="@string/login"
                                                       style="@style/Widget.Mohre.Button"
                                                       android:layout_height="wrap_content"
                                                       app:cornerRadius="@dimen/card_corner_radius"
                                                       android:padding="@dimen/unit_large"
                                                       android:id="@+id/loginBtn"
Run Code Online (Sandbox Code Playgroud)

/>

我的 Widget.Mohre.Button 样式

<style name="Widget.Mohre.Button" parent="Widget.MaterialComponents.Button">
        <item name="android:textColor">@color/textColorWhite</item>
        <item name="android:background">@drawable/mohre_button_selector</item>
        <item name="android:textAppearance">@style/TextAppearance.Button</item>
        <item name="android:stateListAnimator" tools:ignore="NewApi">@animator/button_state_list_anim</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

我的选择器可绘制

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/mohre_button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/mohre_button_selected" android:state_enabled="false" android:state_pressed="false" />
<item android:drawable="@drawable/mohre_button_normal" />
Run Code Online (Sandbox Code Playgroud)

我的个人可绘制对象只是具有不同颜色的矩形形状,例如

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <corners android:radius="30dp"></corners>
    <solid android:color="#3a516a"></solid>
</shape>
Run Code Online (Sandbox Code Playgroud)

该按钮根本不采用选择器可绘制的颜色。它只显示应用程序的默认强调色

android material-design

7
推荐指数
1
解决办法
1162
查看次数

为什么引用类型存储在堆中

我确实知道在Java中,(也许在.net中),原语存储在堆栈中,其中引用类型存储在堆上.

我的问题是,我不理解这种行为的过程/缺点.为什么我们不能在堆栈中引用内存位置呢?.我用谷歌搜索时找不到合适的解释(也许我很蠢),但如果你能提供一些见解,我将不胜感激

谢谢.

java heap jvm

6
推荐指数
2
解决办法
3233
查看次数

为什么 ImmutableList 会导致 Jetpack Compose 中的重组?

这篇文章中,它说如果可组合项的参数类型是ImmutableList,则它被认为是稳定的,这意味着如果列表没有更改,则可组合项将不会被重组。

@Immutable
data class Contact(val name: String, val age: Int)


@Composable
fun ContactRow(contacts: ImmutableList<Contact>, modifier: Modifier = Modifier) {
  var selected by remember { mutableStateOf(false) }
  Row(modifier) {
    ContactDetails(contacts)
    Checkbox(selected, onCheckedChange = {
      selected = !selected
    })
  }
}

@Composable
fun ContactDetails(contacts: ImmutableList<Contact>) {
  Text(text = contacts[0].name)
}
Run Code Online (Sandbox Code Playgroud)

在这里,每次我选择该复选框时,ContactDetails即使我使用的ImmutableList是 KotlinX 集合中的内容,可组合项也会重新组合。

我的compose版本也是1.2.0

我的编译器报告也将其标记为不稳定

android kotlin android-jetpack-compose

6
推荐指数
1
解决办法
3653
查看次数

具有共享视图模型的Android导航组件

视图模型与活动或与其相连的片段一起生存和死亡。这具有一定的影响,这超出了我的范围,为什么没有人问(如果我们将Navigation体系结构引入图片中)。

根据最新的android博客和导航框架的工作方式,建议我们使用Single Activity Multiple Fragments一节。

据说我有以下应用程序设计。

Activity A (Application Entry Point)
----------
Fragment (A) (Uses ViewModel AB)
Fragment (B) (Uses ViewModel AB)
Fragment (C) (Uses ViewModel CDE)
Fragment (D) (Uses ViewModel CDE)
Fragment (E) (Uses ViewModel CDE)
Run Code Online (Sandbox Code Playgroud)

现在,由于我使用共享的视图模型,这意味着我的视图模型将附加到活动中。但是,这似乎是泄漏的。就像我从A一直遍历到E,然后又从片段弹出到片段B一样,视图模型CDE应该被销毁,但是不会被破坏,因为它已连接到活动。

同样,我们将无法将视图模型连接到片段,因为我们将共享它们的数据。

只有我提出这个问题的事实使我相信,根据我的理解,我在这里是错误的。如果我能对情况有一个适当的了解,那会很高兴。

android android-architecture-components android-jetpack

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

TextInputLayout 的 colorControlActivated 不起作用

这是我应用程序的主要风格

<style name="Theme.RoundRobin" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorSecondaryVariant</item>
    <item name="colorAccent">@color/colorPrimaryVariant</item>
    <item name="colorControlActivated">@color/colorControlActivated</item>
    <item name="colorControlNormal">@color/colorControlNormal</item>
    <item name="colorControlHighlight">@color/colorControlNormal</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

据我所知,colorControlHighlight 应该为 textinputlayout 中的下划线、标签和光标着色。

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/username_til"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:layout_marginRight="24dp"
            app:hintTextAppearance="@style/TextAppearance.Subtitle1"
            app:layout_constraintBottom_toTopOf="@+id/password_til"
            app:layout_constraintEnd_toEndOf="parent"
            android:hint="Username"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">

        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/usernameEt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:textAppearance="@style/TextAppearance.Body2"/>
    </com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

没有一个 colorControls 应该与新的 MaterialCompoents 一起使用。

我在这里错过了什么吗

我给了我的 TextInput 这种样式,但它看起来好像 MaterialComponents 不能很好地与 colorControlsX 一起使用

<style name="Widget.RoundRobin.TextInputLayout.FilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="hintTextAppearance">@style/TextAppearance.Caption</item>
        <item name="android:paddingBottom">8dp</item>
        <item name="boxBackgroundColor">@color/colorWhite</item>
        <item name="android:colorControlHighlight">@color/colorSecondaryVariant</item> // this doesn't …
Run Code Online (Sandbox Code Playgroud)

android material-design android-textinputlayout material-components material-components-android

5
推荐指数
1
解决办法
1146
查看次数