如果我有以下代码:
Intent intent = new Intent(this,DownloadService.class);
for(int i=0;i<filesArray.length;i++){
startService(intent);
}
Run Code Online (Sandbox Code Playgroud)
在此代码中DownloadService
扩展IntentService
.
所以现在,当我打电话时startService(intent)
,这意味着每次调用我都会启动一项新服务,startService(intent)
或者这意味着它DownloadService
会运行一次,然后每次调用startService(intent)
它时,只会使用不同的startId传递不同的意图.
这是否有意义,以及其中哪一个是这样的?
我正在将一个复合drawable设置为TextView,就像这样
txtView1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0,0);
Run Code Online (Sandbox Code Playgroud)
drawable被绘制到TextView的最左侧位置.但是我想稍微移动它,使它不会位于TextView的左边界,但它就像10dip里面.我使用以下方法做了一些填充:
android:paddingLeft="10dip"
Run Code Online (Sandbox Code Playgroud)
但这会推动文本与10dip右侧的复合画面一起移动.
如何在不影响文本的情况下仅将复合可绘制的10dip移动?
我将从服务广播发送到活动时遇到问题.
这就是我在Service类中的内容:
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
我有很多活动,在我的一项活动中我有这个:
class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context ctxt, Intent i) {
System.out.println("received");
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是我的广播接收器没有收到任何东西!
救命!
编辑:
如果我有很多活动,怎么可以向所有人发送广播消息.换句话说,我可以将相同的广播接收器应用于所有活动!
我知道如何使用xml将drawable设置为文本的颜色,但我不知道它是如何在Java中完成的.
在xml中是这样的:
android:textColor="@drawable/selected_color"
Run Code Online (Sandbox Code Playgroud)
在JAVA?
可能重复:
Android:"意外的流结束"异常下载大文件
我正在低估一个大约的档案.5MB使用HttpURLConnection但是在downlod期间的一半我在我的代码的这一行得到了"意外的流结束"错误:
while ((count = input.read(data)) > 0) {
Run Code Online (Sandbox Code Playgroud)
这是LOG:
11-20 16:05:55.749: ERROR/PRINTSTACK(3425): STACK:unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425): java.io.IOException: unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425): at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:47)
11-20 16:05:55.749: WARN/System.err(3425): at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
11-20 16:05:55.749: WARN/System.err(3425): at java.io.FilterInputStream.read(FilterInputStream.java:133)
11-20 16:05:55.759: WARN/System.err(3425): at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:265)
11-20 16:05:55.759: WARN/System.err(3425): at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:193)
11-20 16:05:55.759: WARN/System.err(3425): at java.lang.Thread.run(Thread.java:1019)
Run Code Online (Sandbox Code Playgroud)
帮助!! 1
编辑:有关设置输入流的代码的更多信息:
HttpURLConnection conexion = (HttpURLConnection)url.openConnection();
conexion.setRequestMethod("GET");
conexion.setReadTimeout(20000);
conexion.connect();
File file = new File(root.getAbsolutePath()+"/", fileName);
int lenghtOfFile = conexion.getContentLength();
InputStream input …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用IntentService下载多个文件.IntentService按预期的方式逐个加载它们,唯一的问题是当Internet关闭时,意图服务不会停止下载,而是会卡在当前线程上.如果我设法停止当前线程,它将继续运行存储在其队列中的其他线程,即使互联网连接已关闭.
在另一篇文章中建议我使用LinkedBlockingQueue并创建我自己的Worker线程,该线程不断检查此队列中的新线程.现在我知道在创建和销毁线程时会有一些增加的开销和性能问题但在我的情况下这不是一个问题.
在这一点上,我想要做的就是理解IntentService是如何工作的,但是我没有(我已经查看了代码),然后使用由Worker线程控制的LinkedBlockingQueue为它提出了我自己的实现.有没有人这样做过?可以提供一个工作示例,如果您觉得提供源代码感觉不舒服,我可以使用伪代码.谢谢!
更新:我最终使用一个带有looper的线程实现了我自己的Intent Service,该looper检查队列,队列又存储从startService(intent)传递的意图.
public class MyIntentService extends Service {
private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>();
public MyIntentService(){
super();
}
@Override
public void onCreate() {
super.onCreate();
new Thread(queueController).start();
Log.e("onCreate","onCreate is running again");
}
boolean killed = false;
Runnable queueController = new Runnable() {
public void run() {
while (true) {
try {
Download d =queue.take();
if (killed) {
break;
}
else {
d.downloadFile();
Log.e("QueueInfo","queue size: " + queue.size());
}
}
catch (InterruptedException e) {
break;
}
}
Log.e("queueController", …
Run Code Online (Sandbox Code Playgroud) 我在Android中遇到了一个奇怪的HashMap问题.我正在将值放入形式的hashmap中
HashMap <String,String> sample = new HashMap<String,String>();
Run Code Online (Sandbox Code Playgroud)
但是,假设我按以下顺序放置以下值:
sample.put("ifi1", "video1");
sample.put("ifi2", "video2");
sample.put("ifi3", "video3");
sample.put("ifi4", "video4");
sample.put("ifi5", "video5");
sample.put("ifi6", "video6");
sample.put("ifi7", "video7");
sample.put("ifi8", "video8");
sample.put("ifi9", "video9");
Run Code Online (Sandbox Code Playgroud)
这只是一个与我的相似的简单示例.我的实际代码中只有一个更大的列表.但是,当我现在尝试仅打印值时,我得到一个无序列表,如下所示:
VIDEOS: video1
VIDEOS: video3
VIDEOS: video2
VIDEOS: video5
VIDEOS: video4
VIDEOS: video7
VIDEOS: video6
VIDEOS: video9
VIDEOS: video8
Run Code Online (Sandbox Code Playgroud)
事实上,我希望它产生以下列表:
VIDEOS: video1
VIDEOS: video2
VIDEOS: video3
VIDEOS: video4
VIDEOS: video5
VIDEOS: video6
VIDEOS: video7
VIDEOS: video8
VIDEOS: video9
Run Code Online (Sandbox Code Playgroud)
为什么这个,任何想法?
我目前正在增加两个浮点数:0.0004*0.0000000000012 = 4.8e-16
如何以正常格式获得结果,即没有科学记数法,如0.0000000000324,然后将其四舍五入说5个数字.
我正在尝试使用包含用于执行后台工作的 AsynTask 的服务下载多个二进制文件。我能够成功运行服务,我已经在清单文件中注册了它。我可以下载文件,但是我想在通知栏中显示一个进度条。我正在 onPreExecute() 方法内创建通知栏并设置进度条并在 onProgressUpdate() 方法内通知 NotifactionManager。
private class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Downloading...", System.currentTimeMillis());
contentView = new RemoteViews(getPackageName(), R.layout.progress_layout);
contentView.setProgressBar(R.id.status_progress, 10, 0, false);
contentView.setTextViewText(R.id.status_text,currentFile);
notification.contentView = contentView;
// Toast.makeText(DownloadService.this, "Downloading...!",Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
//Downloading code goes here
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
notification.contentView.setProgressBar(R.id.status_progress, 100, Integer.parseInt(progress[0]), …
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个一个下载多个文件(文件已下载,我们开始下载下一个文件)。使用这种方法,我可以跟踪正在下载的文件。问题是我正在使用以下代码来执行任务:
File file = null;
for(int i=0; i< videos.length; i++) {
file = new File(root.getPath() + videos[i]);
boolean exists = file.exists();
if(exists){
//tv.append("\n\n"+fileNames[i]+" already exists");
continue;
}
else {
currentFile = videos[i];
new DownloadFileAsync().execute(videoURL+videos[i],videos[i]);
}
file = null;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我调用 new DownloadFileAsync().execute(videoURL+videos[i],videos[i]); 在一个循环中,显然为每个文件启动一个任务并同时下载它们。
我的问题是:如何为特定文件运行执行任务,检查它是否已下载 - 如果是,则通过为其执行任务来继续下一个文件?