这是我的代码......
String[] show = new String[2];
RemoteViews remoteViews;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
show = getString();
remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
remoteViews.setTextViewText( R.id.widget_title, show[0]);
remoteViews.setTextViewText( R.id.widget_textview, show[1]);
Intent configIntent = new Intent(context, SuitAuto.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
Intent active = new Intent(context, WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, …
Run Code Online (Sandbox Code Playgroud) 好吧,这是我遇到的一个非常奇怪的问题,而且我很确定我在某个地方搞乱了,但我无法弄清楚在哪里.
我在想的是 -
Timer
执行TimerTask
一次TimerTask
又执行一个AsyncTask
(在这种情况下,简单的休眠的第二返回AsyncTasks的数目的静态计数之前).当然,已经使用适当的Handler
s和Runnable
s将异步消息从其他线程发布到UI.
此代码仅执行一次.我希望它每5秒发射一次.这是代码.
注意:我不知道如何处理Looper
.经过反复试验,我把它放在那里!
public class TimerAsyncMixActivity extends Activity {
public static final String TAG = "TimerAsyncMix";
static int executionCount = 0;
Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Timer().schedule(new MyTimerTask(this), 0, 5000);
}
class MyAsyncTask extends AsyncTask<String, Void, Integer>{
@Override
protected Integer doInBackground(String... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException …
Run Code Online (Sandbox Code Playgroud) 我有一个方法,我希望它可以安排在以后执行.调度时间和方法的参数取决于用户输入.
我已经尝试过Timers,但我有一个问题.
怎么可能将参数传递给Java TimerTask运行方法?
TimerTask timert = new TimerTask()
{
@Override
public void run()
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为Android编写一个应用程序,用于从SHOUTcast mp3流中获取元数据.我正在使用我在网上找到的一个非常漂亮的课程,我稍微修改了一下,但我仍然有两个问题.
1)我必须不断ping服务器以使用TimerTask更新元数据.我不喜欢这种方法,但这是我能想到的.
2)当我的应用程序运行时,有一吨公吨的垃圾收集.删除TimerTask摆脱了垃圾收集问题所以我不确定我是做错了还是这是正常的.
这是我正在使用的课程:
public class IcyStreamMeta {
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
public IcyStreamMeta(URL streamUrl) {
setStreamUrl(streamUrl);
isError = false;
}
/**
* Get artist using stream's title
*
* @return String
* @throws IOException
*/
public String getArtist() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
return title.trim();
}catch (StringIndexOutOfBoundsException e) {
return "";
}
}
/** …
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序从用户获取开始和结束时间并启动特定进程(开始时间)运行到(结束时间),对于示例我使用TimerTask实用程序,以防它只从当前时间开始进程和运行到(结束时间)我无法设置开始时间如何在java中共同设置用户时间(开始时间)和系统时间
//my sample program
import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
public static void main(String[] argv) throws Exception {
int numberOfMillisecondsInTheFuture=1000;
// start time= dynamically set by user
// end time =dynamically set by user
Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);//here is the problem.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//System.out.println("doing");
//doing some task not related to this question
}
}, timeToRun);
}
Run Code Online (Sandbox Code Playgroud) 在以下代码中
t.schedule(timertask,d.getDate(),1000); 抛出
NullPointer异常
帮助我
目标:
我的基本目标是运行一个方法(每次在一个固定的时间间隔后),将一些数据从我的Android设备发送到webservice
Date d = new Date();
d.getDate();
timertask = new TimerTask() {
@Override
public void run() {
new Thread() {
public void run() {
try {
ProDialog = ProgressDialog.show(Home.this,
"Sending Data",
"Please wait while sending data...");
Looper.prepare();
sendLocation();
handler.sendEmptyMessage(0);
quit();
Looper.loop();
} catch (Exception e) {
ProDialog.dismiss();
}
}
public void quit() {
ProDialog.dismiss();
Looper.myLooper().quit();
}
}.start();
}
};
try {
t.schedule(timertask, d.getDate(), 1000);
} catch …
Run Code Online (Sandbox Code Playgroud) 我开始开发一个记录视频的Android应用程序,我需要每1分钟收集一次GPS位置
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mPreview);
private boolean isRecording = false;
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); …
Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序使用计时器每5-10秒调用一次AsyncTask(使用java.util.Timer和java.util.TimerTask).AsyncTask从Amazon AWS S3数据库获取图像数据,并为主UI活动加载ImageView.
这工作正常一两个小时,但后来我得到一个神秘的错误消息,应用程序被杀死.错误消息来自"Looper"并说:
Could not create epoll instance. errno=24
Run Code Online (Sandbox Code Playgroud)
要么
Could not create wake pipe
Run Code Online (Sandbox Code Playgroud)
在网上搜索似乎表明问题可能与文件描述符有关(太多的打开文件描述符?).我已经浏览了代码,但没有看到任何文件,流或连接未关闭的地方.
当应用程序被杀死时,logcat会从AndroidRuntime发出一条消息:
FATAL EXCEPTION: main
Run Code Online (Sandbox Code Playgroud)
有没有人知道这些消息,或如何解决?谢谢!
我想制作一个简单的程序,使用 Java.Util.Timer 计算秒数直到 100 下面的代码是我正在使用的代码,但是它只是立即打印所有数字,而无需在每个数字之间等待一秒钟。我该如何解决这个问题?(通常我会使用 thread.sleep 但这只是概念证明。)
import java.util.Timer;
import java.util.TimerTask;
public class Main {
static Timer timer = new Timer();
static int seconds = 0;
public static void main(String[] agrs) {
MyTimer();
}
public static void MyTimer() {
TimerTask task;
task = new TimerTask() {
@Override
public void run() {
while (seconds < 100) {
System.out.println("Seconds = " + seconds);
seconds++;
}
}
};
timer.schedule(task, 0, 1000);
}
}}
Run Code Online (Sandbox Code Playgroud) 运行此代码,我希望它将测试变量增加5秒然后完成.
import java.util.Timer;
import java.util.TimerTask;
public class Test {
private static boolean running;
public static void main( String[] args ) {
long time = 5 * 1000; // converts time to milliseconds
long test = Long.MIN_VALUE;
running = true;
// Uses an anonymous class to set the running variable to false
Timer timer = new Timer();
timer.schedule( new TimerTask() {
@Override
public void run() { running = false; }
}, time );
while( running ) {
test++;
}
timer.cancel();
System.out.println( …
Run Code Online (Sandbox Code Playgroud)