小编Ant*_*hyn的帖子

Java 8中"功能接口"的精确定义

最近我开始探索Java 8,我无法理解Java功能实现lambda表达式所必需的"功能接口"的概念.有一个非常全面的 Java函数中的lambda函数指南,但我仍然坚持用于定义函数接口概念的章节.定义如下:

更确切地说,功能接口被定义为具有恰好一个抽象方法的任何接口.

然后他进入示例,其中一个是Comparator界面:

public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
} 
Run Code Online (Sandbox Code Playgroud)

我能够测试我可以使用lambda函数代替Comparator参数,它可以工作(即Collections.sort(list, (a, b) -> a-b)).

但是在Comparator接口中,两者compareequals方法都是抽象的,这意味着它有两个抽象方法.那么如果定义要求接口只有一个抽象方法,那么它如何工作呢?我在这里错过了什么?

lambda abstract java-8 functional-interface

66
推荐指数
2
解决办法
7707
查看次数

Android:分组通知和摘要仍在4.4及以下单独显示

我想在Android Wear上实现堆叠通知为此,我为每个"项目"创建了1个摘要通知和N个单独通知.我只想在手机上显示摘要.这是我的代码:

private void showNotifications() {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    showNotification1(notificationManager);
    showNotification2(notificationManager);
    showGroupSummaryNotification(notificationManager);
}

private void showNotification1(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 1", "message 1", 1);
}

private void showNotification2(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 2", "message 2", 2);
}

protected void showSingleNotification(NotificationManager notificationManager,
                                      String title,
                                      String message,
                                      int notificationId) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setGroupSummary(false)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(notificationId, notification);
}

private void showGroupSummaryNotification(NotificationManager notificationManager) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); …
Run Code Online (Sandbox Code Playgroud)

android android-notifications android-wear-notification

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

Android:如何获取已安装活动的列表,因为它们出现在启动器中,没有重复项

我正在编写一个应用程序,允许用户查看已安装的应用程序列表,选择其中一个,然后按计划启动它.使用stackoverflow中的教程,我设法弄清楚如何获取已安装活动的列表,它们的包名和图标(即这里 - 几种方法).以防万一,这就是我开始活动的方式,它完美无瑕,没问题:

Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
Run Code Online (Sandbox Code Playgroud)

问题是检索已安装的应用程序列表.我找到了两种方法来获取已安装的应用程序列表:

1)使用

PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplication(PackageManager.GET_META_DATA) 
Run Code Online (Sandbox Code Playgroud)

apps您的每个元素中可以获得它的包名和包标签(应用程序名称).

2)使用

PackageManager pm = getPackageManager();    
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mainIntent, 0);
for(ResolveInfo info : resolveInfos) {
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
    //...
    //get package name, icon and label from applicationInfo object    
}
Run Code Online (Sandbox Code Playgroud)

一种方法存在问题:它返回所有已安装的软件包,包括系统服务,这些软件包可能不包含任何活动,因此无法启动.这是一个带有示例的屏幕截图: 带包的应用列表

上面没有图标的所有项目都无法启动.

第二种方法也存在问题:列表中的几个项目有重复项: 带有重复项的应用列表

当我在调试器中设置断点时,我看到这些"地图"项目具有不同的活动名称("com.google.android.maps.MapsActivity","com.google.android.maps.LatitudeActivity","com.google" .android.maps.PlacesActivity"等).

我决定使用第二种方法,因为它提供了一个更适合我需要的列表,但我找不到一种方法来过滤掉重复项,只显示应用程序的默认活动,因为它们出现在启动器中(您只能在手机的应用列表中看到一个"地图",而不是四个).我尝试过滤掉系统应用ApplicationInfo.FLAG_SYSTEM,但这会删除我想要的许多应用,包括地图和其他预装的应用.我PackageManager.MATCH_DEFAULT_ONLY在执行queryIntentActivities时尝试使用flag,但这也会过滤掉许多应用程序,只留下一些.

我有点迷失在这里,我不知道该怎么做.我已经阅读了关于检索已安装应用列表的stackoverflow的所有问题,但这个问题从未提出过.请帮助任何人?如何检索没有重复项的已安装可启动应用程序列表?

android android-package-managers android-activity

28
推荐指数
4
解决办法
3万
查看次数

如果由android设置,则getWidth()返回0:layout_width ="match_parent"

我有一个名为FractalView的类,它扩展了ImageView.我的目标是用这个具有整个屏幕大小的类来绘制分形.它是我活动的布局文件中唯一的View,而不是顶级的LinearLayout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
    android:background="#FFFFFFFF">

    <com.antonc.fractal_wallpaper.FractalView
        android:id="@+id/fractal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,我对FractalView和LinearLayout的layout_width和layout_height都使用"match_parent".此时我假设FractalView的大小设置为某个值,因为它匹配它的父级,它匹配它的父级(这是整个屏幕).然后我尝试使用getWidth()和getHeight()检测FractalView可用的大小:

public class FractalView extends ImageView {

private Context mContext;
private Handler mHandler;

private int mScrWidth;
private int mScrHeight;

public FractalView(Context context) {
    super(context);
    mContext = context;
    init();
}

private void init() {

    mScrWidth = getWidth();
    mScrHeight = getHeight();
    // Breakpoint
}

@Override
protected void onDraw(Canvas canvas) {
    [...]
}

}
Run Code Online (Sandbox Code Playgroud)

但是当我的代码到达断点时,调试器显示mScrWidth和mScrHeight为零.我尝试过使用getMeasuredWidth()和getMaxWidth(),但它们也返回了不正确的值.我已经阅读了类似问题的几个答案,即这一个,它们都解释了getWidth()在绘制返回图像的大小,而我实际需要的是该视图可用的区域宽度,我开始绘画之前.

有没有办法检索宽度和高度的这些值?

android android-layout

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

Android:我可以在服务中使用Google Analytics吗?

我有一个应用程序,大部分时间在后台工作,作为服务.有很多的例子和在线教程关于如何使用谷歌分析API与EasyTracker库,它跟踪多个活动,但没有一个解释,如何使用谷歌Analytics(分析)API的服务.它甚至可能吗?

service android google-analytics

10
推荐指数
1
解决办法
3543
查看次数

有一个按钮在纹波动画结束时执行OnClickListener(材质主题)

我正在观察Android中按钮上的涟漪动画错误.基本上,我有一个ViewGroup,称它为ButtonContainer,包含2个按钮,称之为OkButton和CancelButton.当用户点击CancelButton时,我想通过将其可见性设置为GONE来隐藏ButtonContainer.但这会导致副作用,即CancelButton上的波纹动画排队,但从未播放,并且下次ButtonContainer变为可见时播放.这似乎发生是因为按钮的OnClickListener在波纹动画有机会播放之前被触发.本文描述了这个确切的错误,并显示了视图的确切行为:链接

笔者提出一个解决办法,但我想知道,有没有办法有OnClickListener被解雇上的按钮波纹动画的播放?

animation android android-5.0-lollipop

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

Mockito - 检查是否在对象上调用了任何方法(访问了对象)

我想编写一个测试,将模拟对象A传递给测试B下的对象,并检查是否调用了A的任何方法.为了给出一些上下文,B类被设计为基于一组参数以特定方式操纵A,并且在某些条件下它根本不应对它做任何事情.所以我的目标是测试那个场景.我知道如何测试是否调用了特定方法:

verify(A, never()).myMethod();
Run Code Online (Sandbox Code Playgroud)

但是我无法找到确保方法A的NONE被调用的方法.有没有办法做到这一点?

java junit unit-testing mockito

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

带有x86映像的Android 2.3.3模拟器无法使用"使用主机GPU"运行

我注意到如果在创建具有Android 2.3.3和Intel x86系统映像的新虚拟设备时选中"使用主机GPU"复选框,则模拟器将无法运行,它显示黑屏,没有生命迹象,除了logcat中的错误.根据我在网上找到的一些信息,在主机GPU上运行从来没有打算用于2.3.3图像,只有4.0.3+.但后来我找到了这个答案,作者声称在安装后成功运行它ironhide.但是,我从来没有机会使用并且不太了解实际情况的铁皮只能在Linux上使用.有没有办法在Windows上运行x86系统映像和主机GPU运行2.3.3?

android android-virtual-device android-emulator

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

Intent Filter with android:autoVerify ="true" - 从未在安装时验证,默认的应用程序链接不起作用

我用我的Android应用branch.io SDK,并希望让我的应用程序在Android上6支链的默认处理程序描述这里(安卓指南)和这里(Branch.io指南)

这是我在AndroidManifest.xml中的活动声明:

    <activity android:name="com.mypackage.MyActivity"
              android:launchMode="singleTask">
        <intent-filter tools:node="merge" android:autoVerify="true">
            <data android:scheme="@string/url_scheme" android:host="open"/>
            <data android:scheme="https"
                  android:host="@string/branch_io_host"
                  android:pathPrefix="@string/branch_io_path_prefix"/>
            <data android:scheme="http"
                  android:host="@string/branch_io_host"
                  android:pathPrefix="@string/branch_io_path_prefix"/>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

但是,当我在我的设备上安装构建时,当我点击具有正确主机和路径的链接时,我仍然会看到选择器对话框.阅读完这篇关于应用程序链接的广泛指南后,我相信这种情况正在发生,因为我的设备永远不会验证我的应用程序的意图过滤器.例如,当我从Play商店安装Twitter应用程序时,我在LogCat中看到这些消息:

03-24 15:04:27.231: D/IntentFilterVerificationReceiver(16965): Received ACTION_INTENT_FILTER_NEEDS_VERIFICATION.
03-24 15:04:27.248: I/IntentFilterIntentService(16965): Verifying IntentFilter. verificationId:2 scheme:"https" hosts:"twitter.com www.twitter.com ads.twitter.com" package:"com.twitter.android".
03-24 15:04:30.134: I/IntentFilterIntentService(16965): Verification 2 complete. Success:true. Failed hosts:.
Run Code Online (Sandbox Code Playgroud)

但是当我安装我的应用程序时,我没有看到这样的消息.我尝试了发布和调试版本,尝试将其上传到Play商店中的Alpha测试并从那里安装,结果相同.为什么Android不验证我的Intent过滤器?

android deep-linking branch.io

7
推荐指数
2
解决办法
6334
查看次数

SimpleDateFormat.getTimeInstance忽略24小时格式

在我的应用程序中,我可以选择暂停执行一段时间.我想显示执行将恢复的时间,包括秒,我希望时间字符串根据设置进行格式化.这是我提出的代码:

long millis = getResumeTime();
String timeString;
timeString = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM).format(millis);
Run Code Online (Sandbox Code Playgroud)

这确实产生了一个带有秒的格式化字符串,但它返回AM/PM格式的时间,即使我在设置中设置了24小时时间格式.它甚至更有趣,因为系统托盘中的时间使用24小时格式正确格式化.

我尝试使用DateFormat.getTimeFormat,如下所示:

long millis = getResumeTime();
String timeString;
java.text.DateFormat df = android.text.format.DateFormat.getTimeFormat(this);
timeString = df.format(millis);
Run Code Online (Sandbox Code Playgroud)

但结果字符串不包含秒,我没有看到包含它们的方法.

我在Android 4.2模拟器上运行此代码.我在这里错过了什么吗?SimpleDateFormat不知道12/24小时设置吗?如果没有,我如何以系统格式获得时间(包括小时,分钟和秒)的字符串表示?

java time android time-format

6
推荐指数
3
解决办法
9685
查看次数