我有一个看似简单的问题,但我似乎无法让它正常工作.
我的'assets'文件夹中有一个zip文件,我需要解压缩ProgessBar,我想在其中向用户显示进度如何.
我有一切工作,但我想将ProgessBar最大值设置为zip文件中的文件数.此文件夹中的文件数有时会发生变化,因此我希望ProgessBar相对于zip中包含的文件数量.
我正在使用ZipInputStream-API,但似乎没有办法获取zip文件中的文件数量.我能想到的唯一方法是这样做:
ZipInputStream zin = new ZipInputStream(getAssets().open(
"myFile.zip"));
int numFiles = 0;
int increment = 0;
while (zin.getNextEntry() != null) {
numFiles++;
}
ZipEntry ze = null;
//Set the Max..value here..
progessBar.setMax(numFiles);
while ((ze = zin.getNextEntry()) != null) {
increment++;
progessBar.setProgress(increment);
}
Run Code Online (Sandbox Code Playgroud)
这有效,但有两个while循环似乎有点多余,基本上做同样的事情.
我知道有一个ZipFile-API有一个size()-method,但它需要一个文件的路径,因为我的文件位于'assets'文件夹中,我很确定从这个目录中读取的唯一方法是通过流式传输.
有没有办法让我做到这一点?
如何从byte[](Blob)获取文件类型扩展名.我正在从DB读取文件,byte[]但我不知道如何自动检测文件扩展名.
Blob blob = rs.getBlob(1);
byte[] bdata = blob.getBytes(1, (int) blob.length());
Run Code Online (Sandbox Code Playgroud) 我想问一下,使用两个或多个嵌套的RelativeLayout.第一个代码看起来像图片1,当我添加android:layout_alignParentRight="true"第二个RelativeLayout时,这看起来像图片2.我想将文本对齐到第二个RelativeLayout.我的错在哪里?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_height="wrap_content"
android:id="@+id/testRL">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:id="@+id/testRotateLL"
android:background="@drawable/picture_border_offer_first_page">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="7000TL"
android:textSize="15sp"
android:textColor="@android:color/white"
android:id="@+id/amountLayoutTV" />
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
图片1:

我补充说 android:layout_alignParentRight="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_height="wrap_content"
android:id="@+id/testRL">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:id="@+id/testRotateLL"
android:background="@drawable/picture_border_offer_first_page">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="7000TL"
android:layout_alignParentRight="true"
android:textSize="15sp"
android:textColor="@android:color/white"
android:id="@+id/amountLayoutTV" />
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
图2:

我最近在一次关于Android的采访中被问了一些问题.我搜索了其中一些,但我找不到合适的资源和答案.所以我想在这里与大家分享.
在Android中使用的首选布局是什么(为了更好的内存消耗等)?我没有答案,面试官告诉我这是相对布局.这是真的 ?对此有何解释?
告诉我你为更好的内存消耗做的一些做法?我看了一下,但似乎还有其他东西.因为面试官提到一些与静态变量相关的事情比较好.
如果Android需要内存,它会杀死服务还是活动?优先问题.我也没有找到任何人讨论这个问题.面试官说服务的一些事情比活动具有高优先级(??),因此活动是被杀死的组成部分.这是真的 ?任何进一步的资源或解释?
请分享您对此问题的所有知识或资源.
根据'帮助'目标文档:
这是我发现的,这与我的预期有点不同:
debug:忽略密钥库定义,build.properties无论您是否指定它们.它使用哪个kesystore文件?与Eclipse相同:debug.keystore文档和设置中的默认文件?
它创建了两个文件:
发布: 'help'表示它没有签名.它创建这些文件:
仅当您具有以下指定的值时,接下来的两个才会出现build.properties:
任何有用的评论/验证将不胜感激.
我在所有布局中都有一个带有公共标题的应用程序.我希望每当用户点击带有id的ImageView时btn_home,应用程序将返回到特定的活动,例如我的"Main".
最好的方法是什么?
我知道我可以onClick(View v)为每个活动定义,但也许有更好的方法来做到这一点.甚至让每一项活动都成为一些(通过传统)其他具有onClick(View v)定义的声音的活动.
header.xml
<RelativeLayout ...>
<RelativeLayout android:id="@+id/relativeLayout1" ...>
<ImageView android:id="@+id/logo_cats"></ImageView>
<ImageView android:id="@+id/btn_home" ...></ImageView>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
每个布局
...
<include layout="@layout/header" android:id="@+id/header"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
...
Run Code Online (Sandbox Code Playgroud) 我为我的应用程序编写了一个AppWidget,您可以选择自己喜欢的地方名称,并向您显示该地点的图片.
它有一个配置Activity(我使用了与app本身相同的配置活动),当第一次添加时,小部件和它的配置handeling工作正常:我选择一个我喜欢的地方,我看到了图片;
问题是每当我重新启动设备(或者它退出睡眠模式)时,当我点击它(小部件)时,我进入配置活动 - 更改为所需的图片,当我调试时没有任何反应我可以看到:
appWidgetManager.updateAppWidget(mAppWidgetId, views);
mAppWidgetId =
Run Code Online (Sandbox Code Playgroud)
有一个id,views有没有null.黑客正在发生什么?我的想法是,在重新启动设备时,widgetId会发生变化,并且我不会像我应该那样处理它.顺便说一句,如果在模拟器上尝试相同,我没有问题一切正常
这是我的代码:
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.dryrun" android:versionCode="3"
android:versionName="1.1" android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher_test"
android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="true"><!-- different< android:theme="@style/Theme.NoBackground" -->
<!-- Main Activity -->
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Preferences -->
<activity android:name=".Preferences.EditPreferences">
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</activity>
<!-- Widgets -->
<!-- Widget-->
<receiver android:name=".Widget.testWidget" android:label="@string/app_widget_">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!--action
android:name="com.test.dryrun.Widget.testWidget.PREFENCES_WIDGET_CONFIGURE" /-->
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/test_widget__provider" …Run Code Online (Sandbox Code Playgroud) 我正在尝试为自己的框架创建一个插件架构的方法.我已经阅读了很多主题,并在这里和其他网站发布.基本上我来到以下解决方案,似乎是PHP(目前)唯一的好选择.
这个想法是每个类都像类一样扩展了一种观察者.所以Template类,BaseController等总是扩展一个Plugin类.
class BaseController extends Plugin
{
public function __construct()
{
// Plugin check, notify all loaded plugins
$this->checkForEarlyHooks();
// Init some standard stuff
$this->view = new Template();
$this->baseLayout = 'layout.html';
$this->something = new Something();
// Plugin check, notify all loaded plugins
$this->checkForLateHooks();
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里基本上发生的是当indexController扩展baseController时,插件检查就完成了.在这种情况下为构造函数.在实际调用Action方法之前,如果要使用插件进行某种登录检查,这可能很方便.
Plugin类可以解析从哪个类调用,并知道在加载的插件中要查找的函数.
另请注意,它会检查加载的插件列表2次.在构造函数中加载(早期)之前的一个,并且在加载所有变量(晚期)时加载一个.
我还可以在"checkForLateHooks()"函数中添加变量.因此钩子函数也可以操作它们,比如'baseLayout'变量.
钩子函数看起来像这样:
public function hookConstruct ( &$baseLayout )
{
$baseLayout = 'login.html';
}
Run Code Online (Sandbox Code Playgroud)
现在基本上我的问题是,这种方法有什么好处吗?我知道也有很多其他方法可以做到这一点.但我主要不想在以后遇到设计问题.现在看来这是一个好主意,但你永远不会知道事情会如何发挥作用......
如果我记得正确(从我读过的所有帖子中),这有点像WordPress(以及其他一些框架).
我试图用nfc功能制作应用程序.问题是当发现nfc标记时,挂起的意图总是会创建一个已经存在的新活动.我正在使用标签主机.如何在不进行新活动的情况下制作pendingintent.非常感谢.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
protected void onResume() {
super.onResume();
mResumed = true;
// Sticky notes received from Android
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] messages = getNdefMessages(getIntent());
byte[] payload = messages[0].getRecords()[0].getPayload();
try { cekNfc(new String(payload)); }
catch (SQLException e) { e.printStackTrace(); }
catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
catch (UnsupportedEncodingException e) { e.printStackTrace(); }
setIntent(new Intent());
}
enableNdefExchangeMode();
}
private void enableNdefExchangeMode() { mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mNdefExchangeFilters, null); …Run Code Online (Sandbox Code Playgroud) 我创建了一个Java桌面应用程序(使用Swing),现在我尝试通过使用JNLP从网络启动它来使其工作.当我从终端启动它时,应用程序工作正常,但是一旦我从JNLP启动它,它就不会关闭.我每次都要手动杀死进程.
我读到如果我JFrame将其DISPOSE_ON_CLOSE用作默认关闭操作可能会出现问题,但事实并非如此.它使用DO_NOTHING_ON_CLOSE(隐含地).此外,我System.exit(0)在释放所有对象后明确调用:
f = new JFrame("Pacman");
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Terminate the Game-loop:
GameLoop.INSTANCE.stopLoop();
// Close the application:
System.exit(0);
}
});
Run Code Online (Sandbox Code Playgroud)
我想在关闭应用程序时可能会抛出一个异常,但我无法找到一种方法来获取正在运行的应用程序的控制台输出(例如Stack-Trace)以JNLP启动.这是我试过的:
javaws调试参数开始并连接jconsole(工作但我找不到任何异常或控制台输出).javaws调试参数开始,并将IntelliJ调试器附加到它(也可以,但不会给我任何输出)那么,我如何使用JNLP启动应用程序并获取输出(写入默认的输出和错误流),就好像我会使用普通的桌面应用程序一样?