小编Che*_*eng的帖子

将Activity视为单身人士是否安全

大多数时候,在设计桌面应用程序时,我喜欢将主应用程序设为单例以方便使用.我可以轻松访问应用程序的数据和方法,而无需遍布主应用程序引用.

public class MainFrame extends javax.swing.JFrame {
    // Private constructor is sufficient to suppress unauthorized calls to the constructor
    private MainFrame()
    {
    }

    /**
     * MainFrameHolder is loaded on the first execution of Singleton.getInstance()
     * or the first access to MainFrameHolder.INSTANCE, not before.
     */
    private static class MainFrameHolder {
        private final static MainFrame INSTANCE = new MainFrame();
    }

    /**
     * Returns MainFrame as singleton.
     * 
     * @return MainFrame as singleton
     */
    public static MainFrame getInstance() {
        return MainFrameHolder.INSTANCE;
    }
} …
Run Code Online (Sandbox Code Playgroud)

android design-patterns

4
推荐指数
1
解决办法
143
查看次数

为什么在大多数Android日志代码中使用TAG

我可以看到这是Android开发人员的常见做法.

public final class TasksSample extends ListActivity {
    private static final String TAG = "TasksSample";
    private void method() {
        Log.i(TAG, "message");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做会更容易吗?我不需要为每个新类声明TAG.

public final class TasksSample extends ListActivity {
    private void method() {
        Log.i(getClass().getName(), "message");
    }
}  
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
2
解决办法
9938
查看次数

Windows :: Storage :: ApplicationData ::当前在C++中找不到

我的C++代码,它是Media Foundation Transform的一部分,往往能够在Windows应用商店应用(Metro)中运行

我修改了C++ GrayscaleTransform包含以下代码.

但是,我的C++代码无法找到命名空间Windows::Storage.

LPCWSTR zPath = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path->Data();
Run Code Online (Sandbox Code Playgroud)

我需要做任何其他设置吗?

我可以通过打开Consume Windows Runtime Extension来编译它.

在此输入图像描述

但通过这样做,它将给我额外的链接错误和警告.

warning LNK4197: export 'DllGetActivationFactory' specified multiple times; using first specification 
warning LNK4197: export 'DllCanUnloadNow' specified multiple times; using first specification
warning LNK4197: export 'DllGetActivationFactory' specified multiple times; using first specification 
warning LNK4197: export 'DllCanUnloadNow' specified multiple times; using first specification
error LNK2005: _DllCanUnloadNow@0 already defined in dllmain.obj 
error LNK1169: one or more multiply defined symbols found …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio visual-c++ windows-8 windows-runtime

4
推荐指数
1
解决办法
1794
查看次数

每当我们想要在应用程序中更改字体时,我们是否必须复制字体TTF

以前,为了让我的应用程序在Gingerbread设备及以上版本中可行,我必须将Robotto字体资源复制到资产文件夹中.这是因为Gingerbread没有自带Robotto字体.

在此输入图像描述

但是,让我说,我决定只将我的应用程序部署到Jelly Bean设备.

  1. 我是否仍需要手动将字体资源复制到我的资产文件夹中?我可以使用系统本身的字体资源吗?这是鼓励的吗?我在想,不提供我自己的字体文件,我可以让我的应用程序更小.
  2. 这是TypeFace从资产文件夹获取的代码.

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Regular.ttf");

    如果我想TypeFace直接从系统本身获取,怎么样?

android

4
推荐指数
1
解决办法
1924
查看次数

ConcurrentHashMap类似于CopyOnWriteArrayList

我用的CopyOnWriteArrayList很多.当这个时候尤其如此

  • 线程执行大量读取
  • 线程执行一些写操作

但是,我会Collections.synchronizedList()在什么时候使用

  • 线程执行一些读取
  • 线程执行大量写操作

这是因为根据CopyOnWriteArrayList Java Doc

ArrayList的线程安全变体,其中通过创建底层数组的新副本来实现所有可变操作(添加,设置等).

这通常太贵了......

当涉及到ConcurrentHashMap,我不知道我是否还能申请在选择相同的逻辑ConcurrentHashMapCollections.synchronizedMap()

是否ConcurrentHashMap每次执行写操作时都会制作基础数据结构的新副本?如果写入操作多于读取操作,它会比Collections.synchronizedMap执行得差吗?

java

4
推荐指数
1
解决办法
3116
查看次数

如何在Android中自定义共享意图(这是可能的)

一直以来,我认为不可能在Android中自定义共享意图:如何在Android 中自定义共享意图?

但是,这个做得很好的应用程序(Timely)改变了我的观点.

在此输入图像描述

我在想,他们是怎么做的

  • 自定义标题
  • 定制的身体信息
  • More 按键
  • 图标上一个水龙头将推出合适的应用程序,不再有AlwaysJust once按钮.

android

4
推荐指数
1
解决办法
741
查看次数

在AppWidgetProvider和RemoteViewsService.RemoteViewsFactory之间共享数据的正确方法是什么

目前,我AppWidgetProvider有一个静态数据.它用于传递信息AppWidgetProviderRemoteViewsService.RemoteViewsFactory

public class MyAppWidgetProvider extends AppWidgetProvider {
    // Key will be widget id
    private static Map<Integer, Holder> holderMap = new java.util.concurrent.ConcurrentHashMap<Integer, Holder>();

    public static int getClickedColumn(int appWidgetId) {
        Holder holder = holderMap.get(appWidgetId);  
        if (holder == null) {
            return -1;
        }
        return holder.clickedColumn;
    }
Run Code Online (Sandbox Code Playgroud)
public class AppWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    @Override
    public void onDataSetChanged() {
        int clickedColumn = MyAppWidgetProvider.getClickedColumn(mAppWidgetId);
Run Code Online (Sandbox Code Playgroud)

调用AppWidgetProvider静态方法在大多数情况下都能正常工作.

但是,有时候,如果我将小部件放在主屏幕上,那就让它在那里待几个小时.当我回来并且scoll the ListView,我可能会随机得到以下错误.

java.lang.ExceptionInInitializerError
    at org.yccheok.project.gui.widget.AppWidgetRemoteViewsFactory.onDataSetChanged(AppWidgetRemoteViewsService.java:390)
    at android.widget.RemoteViewsService$RemoteViewsFactoryAdapter.onDataSetChanged(RemoteViewsService.java:142)
    at com.android.internal.widget.IRemoteViewsFactory$Stub.onTransact(IRemoteViewsFactory.java:49)
    at android.os.Binder.execTransact(Binder.java:367)
    at …
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
1
解决办法
2023
查看次数

我应该在onOpen或onConfigure中启用外键约束

我遇到了不同的代码片段,关于启用外键约束SQLiteHelper.我在想,我应该启用外键约束onOpen或者onConfigure,如果我想支持API <16为好.

onOpen在API 16之前,这个讨论建议是正确的地方:使用SQLite的Android中的外键约束?在删除级联

但是,自API 16以来,官方文件确实提到的onConfigure是正确的地方.

public void setForeignKeyConstraintsEnabled (boolean enable)
...
A good time to call this method is right after calling openOrCreateDatabase(File, SQLiteDatabase.CursorFactory) or in the onConfigure(SQLiteDatabase) callback.
Run Code Online (Sandbox Code Playgroud)

我可以知道API 16和<16的单一入口点是什么?

@Override 
public void onOpen(SQLiteDatabase database) {
    super.onOpen(database);
    if (!database.isReadOnly()) {
        // Enable foreign key constraints 
        db.execSQL("PRAGMA foreign_keys=ON;");
    } 
} 
Run Code Online (Sandbox Code Playgroud)

要么

// https://stackoverflow.com/questions/13641250/sqlite-delete-cascade-not-working
@SuppressLint("NewApi")
@Override
public void onConfigure(SQLiteDatabase database) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        database.setForeignKeyConstraintsEnabled(true);
    } else { …
Run Code Online (Sandbox Code Playgroud)

sqlite android

4
推荐指数
1
解决办法
2896
查看次数

如何突出显示通知中每一行的左侧文本

目前,我已经实施了多行通知

在此输入图像描述

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this.getApplicationContext())
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(contentTitle)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setTicker(ticker)
            .setContentText(contentText);

    // Need BIG view?
    if (size > 1) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle(contentTitle);
        for (String notificationMessage : notificationMessages) {
            inboxStyle.addLine(notificationMessage);
        }
        mBuilder.setStyle(inboxStyle);
    }

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

但是,这并不是我想要的.当我看看GMail的实现时,我意识到他们左手边的文字(严成澈)的每一行,都在被凸显出来.

在此输入图像描述

我想知道,实现这种效果的技术是什么?我想突出我的股票名称.

android

4
推荐指数
1
解决办法
1916
查看次数

具有O(1)性能的数据结构get(int index)和避免重复的能力

Set 如果我不想在我的数据列表上重复,那么这是一个显而易见的选择.

但是,Set没有get(int index)方法:为什么java.util.Set没有get(int index)?

实现伪的几个建议get(int index)并没有一个是有效的.

  1. toArray 并通过索引访问新数组.
  2. 获取迭代器,并使用for循环按count访问索引元素.

是否有任何高级数据结构,这使我能够

  1. 避免重复.
  2. 有O(1)表现get(int index).

java data-structures

4
推荐指数
1
解决办法
124
查看次数